Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Serialization of polymorphic collections with an interface as root type
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Stane  
View profile   Translate to Translated (View Original)
 More options Aug 26 2012, 3:29 pm
From: Stane <stan.ko...@googlemail.com>
Date: Sun, 26 Aug 2012 12:29:50 -0700 (PDT)
Local: Sun, Aug 26 2012 3:29 pm
Subject: Serialization of polymorphic collections with an interface as root type

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Stam  
View profile   Translate to Translated (View Original)
 More options Aug 26 2012, 4:32 pm
From: Robert Stam <rob...@10gen.com>
Date: Sun, 26 Aug 2012 16:32:19 -0400
Local: Sun, Aug 26 2012 4:32 pm
Subject: Re: [mongodb-csharp] Serialization of polymorphic collections with an interface as root type

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:

http://pastie.org/4593182

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stan.Konce  
View profile  
 More options Aug 26 2012, 4:41 pm
From: "Stan.Konce" <stan.ko...@googlemail.com>
Date: Sun, 26 Aug 2012 22:41:13 +0200
Local: Sun, Aug 26 2012 4:41 pm
Subject: Re: [mongodb-csharp] Serialization of polymorphic collections with an interface as root type
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



On 2012-08-26 22:32, Robert Stam wrote:
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.

On Sun, Aug 26, 2012 at 3:29 PM, Stane <stan.konce@googlemail.com> wrote:
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





 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stane  
View profile  
 More options Aug 28 2012, 2:20 pm
From: Stane <stan.ko...@googlemail.com>
Date: Tue, 28 Aug 2012 11:20:41 -0700 (PDT)
Local: Tues, Aug 28 2012 2:20 pm
Subject: Re: [mongodb-csharp] Serialization of polymorphic collections with an interface as root type

Hi Robert,
I have just had a chance to try your example.It works excellently (version
1.5 installed also).
Many, many thanks!

Stan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »