Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2467

BsonSerializationException when passing concrete Parameter in constructor to fill interface property

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.7.2
    • Component/s: Serialization
    • Environment:
      osx

      I have a Identity class that is extended by two classes StringIdentity and GuidIdentity. I want to use them in Objects as Property and save them in a MongoDb. For Example a class would look like this:

      
       public class MyEvent : IDomainEvent
      { 
          public MyEvent(GuidIdentity entityId)
          {
             EntityId = entityId;
          }
         public Identity EntityId { get; }
      }
      

      IDomainEvent forces me to implement the Identity Property, so I can (and do not want to) change the property type to GuidIdentity.

      When I deserialize my class I get an exception like this:

      Creator map for class Microwave.Eventstores.UnitTests. MyEvent has 1 arguments, but none are configured.

      Which seems logical according to the .net driver documentation, as my property does not match the type of the parameter and therefore can not be serialized. I had the solution to use the Identity as constructor parameter or make the constructor private and instantiate the class with a static Create method that takes the GuidIdentity as parameter. I also messed around with the BsonClassMap, but I really do not want to write all this kind of duplicate code for every class that implements IDomainEvent

      So is there a way to tell the .net Driver to use the concrete class from the constructor instead of the more general PropertyType for all Types? Like in Newtonsoft, you can define a handler for a specific type (in this case GuidIdentity), that gets used when the serializer hits a specific type.

      I am very new to mongodb so I am not aware of all the tricks that come with it, maybe someone can help me out here. 

            Assignee:
            wan.bachtiar@mongodb.com Wan Bachtiar
            Reporter:
            modmoto Simon Heiss
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: