Generic Insert/Select

May 8, 2013 at 6:57 AM
Edited May 8, 2013 at 7:38 AM
I have lots of classes that inherit from a base class, like:

 public class MR
    {
        public ulong ID { get; set; }
    }


    public class AMR : MR
    {
        public string Test;
    }


    public class BMR : MR
    {
        public string Something;
    }

and I insert like this:

        private static void Save<T>(string ObjectName, T o) where T : MR
        {
            using (var engine = new DBreezeEngine(@"C:\temp\DBR1"))
            {
                using (var tran = engine.GetTransaction())
                {
                    try
                    {
                        var max = tran.Max<ulong, string>(ObjectName);
                        ulong id = 0;
                        if (max.Exists)
                        {
                            var found = JsonConvert.DeserializeObject<T>(max.Value);
                            id = found.ID;
                        }
                        id++;
                        o.ID = id;
                        var s = JsonConvert.SerializeObject(o);
                        tran.Insert<ulong, string>(ObjectName, o.ID, s);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }

        }
and I select like this:

        static T Get<T>(string ObjectName, ulong ID)
        {
            using (var engine = new DBreezeEngine(@"C:\temp\DBR1"))
            {
                using (var tran = engine.GetTransaction())
                {
                    try
                    {
                        var row = tran.Select<ulong, string>(ObjectName, ID);
                        return JsonConvert.DeserializeObject<T>(row.Value);
                    }
                    catch (Exception ex)
                    {

                        Console.WriteLine(ex.ToString());
                        return default(T);
                    }
                }
            }

        }
I could write functions for every concrete classes, like this:
var amr = Get<AMR>("TableA", 1);
var bmr = Get<BMR>("TableB", 20);
But the problem is, I may not know the implement classes at compile time (like new types via plugin) and I need to do it at runtime, like this:
var amr = Get("TableA", 1);
var bmr = Get("TableB", 20);
Coordinator
May 8, 2013 at 11:14 AM
How this question correlates with DBreeze?
May 8, 2013 at 12:15 PM
sorry, you are right, it's implementation level, not storage level. thanks man
May 9, 2013 at 12:27 AM
Edited May 9, 2013 at 12:27 AM
Ok, after some thoughts again, I think it could be supported by DBreeze.

Rather than:
tran.Insert<ulong, SomeType>(TableName, Key, Object);
It could accept like this:
tran.Insert(TableName, Key, Object);
Then it can get like this:
Type type = GetTypeFromName(typeName);
var row = tran.Select(ObjectName, TypeOf(ulong), type, ID);
Coordinator
May 9, 2013 at 1:50 PM
Edited May 9, 2013 at 2:18 PM
We are not planning to make object layer inside DBreeze.dll.
Dbreeze is finished - it's a "low level" data storage library for professional usage - I hope people can understand that.
Its flexibility is the main feature.
The rest must be build on top of DBreeze.dll. It can be called ObjectDbreeze.dll (whatever) with reference on DBreeze.dll.