[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:
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:
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:
Thank you! |