[CSHARP-2467] BsonSerializationException when passing concrete Parameter in constructor to fill interface property Created: 31/Dec/18  Updated: 03/Jul/20  Resolved: 25/Jun/20

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.7.2
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Simon Heiss Assignee: Wan Bachtiar
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

osx



 Description   

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. 



 Comments   
Comment by Rachelle Palmer [ 25/Jun/20 ]

Hi there, thank you for reaching out to MongoDB. As this sounds more like a support issue, I wanted to give you some resources to get this question answered more quickly:

  • our MongoDB support portal, located at support.mongodb.com
  • our MongoDB community forums, located here
  • If you are an Atlas customer, there is free support offered 24/7 in the lower right hand corner of the UI.

Thank you!
Rachelle

Generated at Wed Feb 07 21:42:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.