开发者

How to retrieve objects of an anonymous type from db4o

开发者 https://www.devze.com 2023-02-09 06:18 出处:网络
I\'d like to store objects of an anonymous type to a db4o database. For example: // Store an object of anonymous type to the db

I'd like to store objects of an anonymous type to a db4o database. For example:

// Store an object of anonymous type to the db
var foobar = new {Foo="Ugh", Bar="Oh!"};
using (var db = Db4oEmbedded.OpenFile("db.db40"))
{
    db.Store(foobar);
}

I'm using the following code to开发者_如何学编程 retrieve the objects:

// Retrieve it in a separate program
using (var db = Db4oEmbedded.OpenFile("db.db40"))
{
    var query=from dynamic fb in db select fb;
    query.Dump();
}

However, the properties of the object are not accessible when after retrieval: the dump gives (in Linqpad) this:

5IEnumerable<Object> (3 items)  
GenericObject 
(G) <>f__AnonymousType0`2[[System.String, mscorlib], [System.String, mscorlib]], query_vrfldn 
GenericObject 
(G) <>f__AnonymousType0`2[[System.String, mscorlib], [System.String, mscorlib]], query_oqabew 
GenericObject 
(G) <>f__AnonymousType0`2[[System.String, mscorlib], [System.String, mscorlib]], query_cfvuva 

Is this use case supported by db4o? How could I get the objects neatly out of the database?


Anonymous types are not officially supported by db4o so use it with care.

In your sample code you have two issues:

  1. Using anonymous types
  2. Accessing objects from different assemblies

Regarding to 1, unfortunately, in order to use LINQ db4o does requires you to be able to reference the type in your code (which you can't when using anonymous types). An alternative would be to use SODA (note, the sample code bellow works only if the code that store/retrieves the objects lives in the SAME assembly)

using System;
using Db4objects.Db4o;

namespace TestAnonymousTypes
{
    class Program
    {
        static void Main(string[] args)
        {
            var obj = new {Name = "Foo", Id = "Bar"};

            if (args.Length == 0)
            {
                using (var db = Db4oEmbedded.OpenFile("TestAnonymous.odb"))
                {
                    db.Store(obj);
                }
                return;
            }

            using (var db = Db4oEmbedded.OpenFile("TestAnonymous.odb"))
            {
                var query = db.Query();
                query.Constrain(obj.GetType());

                var result = query.Execute();

                var y = result[0];
                Console.WriteLine(y);
            }
        }
    }
}

Maybe it would be possible to extend db4o LINQ implementation to allow one to specify the type dynamically (but I am not sure).

Regarding 2, in your sample you tried to use the dynamic keyword. As I explained before, db4o needs the actual type to be specified in the LINQ expression, so using dynamic will not work. You can use SODA instead but since db4o stores the the assembly name (along side the class name) when it storing objects this will not work if you have two different assemblies (since the anonymous types will be defined in different assemblies).

A solution for 2 would be to have a common assembly (which defines your model) or playing with aliasing.

Best

Adriano

0

精彩评论

暂无评论...
验证码 换一张
取 消