Serialization of polymorphic collections with an interface as root type

1,442 views
Skip to first unread message

Stane

unread,
Aug 26, 2012, 3:29:50 PM8/26/12
to mongodb...@googlegroups.com
Hi,
it's me again with a new question. I am trying to serialize polymorphic collections where the root type is an interface (which is quite indispensable for my purposes).
In a follow-up to a discussion thread a year ago (e.g. http://osdir.com/ml/mongodb-user/2011-06/msg01561.html) there was an indication that the interface support would be included into version 1.2. I am using version 1.2.0.4274, but I do not have an impression that it is really included.

I am trying to serialize/deserialize collections that are declared roughly as follows (simplified):

    [BsonKnownTypes(typeof(Person), typeof(Organization))]
    [BsonDiscriminator(RootClass = true)]
    public interface ICodedValue
    {
        [BsonId]
        public    string    Code   {get;set;}
        public    string    Name   {get;set;}
    }

    public class Person : ICodedValue
    {
        [BsonId]
        public    string     Code         {get;set;}
        public    string     Name         {get;set;}
        public    Gender     Gender       {get;set;}
        public    DateTime   BirthDate    {get;set;}
    }

    public class Organization : ICodedValue
    {
        [BsonId]
        public    string    Code       {get;set;}
        public    string    Name       {get;set;}
        public    Address   Address    {get;set;}
        public    string    Phone      {get;set;}
    }

(As far as I understand, this is equivalent to the declarations via BsonClassMap.RegisterClassMap() which I also tried with no success).
The interface so declared won't even compile:
"Attribute 'BsonKnownTypes' is not valid on this declaration type. It is only valid on 'class, struct' declarations."
"Attribute 'BsonDiscriminator' is not valid on this declaration type. It is only valid on 'class, struct' declarations."

An attempt to use an abstract class is obviously no solution, since no instances can be initialized. If I declare a normal class as the root type (like in the "Animal-Cat-Tiger" example in the tutorial) everything functions fine. But I need interfaces.

Are interfaces not supported in the end or am I doing things wrongly?
In the best case i would appreciate a minimal code example with an interface as root type that functions.

Thanks in advance,
Stan


Robert Stam

unread,
Aug 26, 2012, 4:32:19 PM8/26/12
to mongodb...@googlegroups.com
Version 1.2 of the C# driver is really old. The current version is 1.5.

Here's a working program (using version 1.5 of the driver) based on your sample classes:


A few notes:

You can't use serialization attributes on interfaces or their members. They only work on classes and their members.

Since you can't use the [BsonKnownTypes] attribute you have to manually ensure that the derived classes are known to the driver. The easiest way to do that is to call LookupSerializer, like this:

    BsonSerializer.LookupSerializer(typeof(Person));
    BsonSerializer.LookupSerializer(typeof(Organization));

You would do that once as the program is starting up.

Stan.Konce

unread,
Aug 26, 2012, 4:41:13 PM8/26/12
to mongodb...@googlegroups.com
Thanks a lot Robert,
I will need a little time to look into it, eventually tomorrow (it's already night here).

But I am really very thankful!
Stan

Stane

unread,
Aug 28, 2012, 2:20:41 PM8/28/12
to mongodb...@googlegroups.com, stan....@googlemail.com
Hi Robert,
I have just had a chance to try your example.It works excellently (version 1.5 installed also).
Many, many thanks!

Stan

Won Lee

unread,
May 1, 2017, 5:33:56 PM5/1/17
to mongodb-csharp
The link is dead but the suggestion does work. It's crucial to call the method before you create your mongoDb instance; Robert does specify it and I'm just adding more emphasis.
Reply all
Reply to author
Forward
0 new messages