MongoFramework
MongoFramework copied to clipboard
Type is not a valid type to map
If the mongo db set type is an interface i.e. MongoDbSet<IMyInterfaceType> it throws an error.
Type is not a valid type to map (Parameter 'entityType')
Is there an example of how I can set this up? Some of my collection types are concrete though have properties that are interface types.
Currently you can't specify an interface on the MongoDbSet but you can use a shared base class if there are common properties you want across multiple types.
I haven't otherwise done a ton of testing how interfaces would behave through MongoFramework and the driver, particularly around discriminators (the _t property in the document).
ok great I had a play around with this and saving to a MongoDbSet of base type works with the _t property expressed as an array in the document showing the inheritance hierachy.
One sticking point is when querying the MongoDbSet it is being returned as the base type and not the inheritor. Casting directly or through a linq method throws an InvalidCastException - I wouldn't expect this to work as base type is missing certain property data. Is there a way to handle this so each document is instanced with the correct type?
This is another case of "kinda". If you were lookkng for a specific type, you can use OfType<YourInheritedType>() on your LINQ query to get it. It can have some weirdness still but you'll be hitting more edge cases of the driver itself.
This kinda deserialization control is basically what the MongoDB driver gives us. Under certain other situations, I do hijack it a bit to make it smarter (see the section in the readme about runtime type discovery) but outside of that, it is whatever the driver does.
In theory, I could try and hijack it further to use similar smartness to directly do what you're suggesting on an entity by entity basis but that's gonna take a lot of digging into.
Yes indeed I've witnessed this weirdness too though OfType<> does seem to have tamed it somewhat. This is what I was thinking WithDerivedEntity<_t> might have been for on the EntityDefinitionBuilder.
Unfortunately not, the WithDerivedEntity is basically just a short hand way of defining further mappings for derived types. In theory the data is there to use to do the smarts, it is just hooking it in on another level.
This conversation though is making me think I could do a broader implementation of runtime type discovery and then I can apply it in exactly the case you're describing.
I won't be able to do any work on this for at least a few weeks but it definitely is an interesting concept.
I will look forward to trying it out. One other scenario I've just stumbled across is when the common base is a property and not the parent MongoDbSet type. How do you specify the required type in this situation?
I will look forward to trying it out. One other scenario I've just stumbled across is when the common base is a property and not the parent MongoDbSet type. How do you specify the required type in this situation?
Ok I got around this by annotating the types to give the mongo driver a bit of guidance. It's a bit messy but does the job. This didn't work when I was using interfaces.
[BsonDiscriminator(RootClass = true)]
[BsonKnownTypes(typeof(InheritorTypeA), typeof(InheritorTypeB))]
class BaseType { }
[BsonDiscriminator(nameof(InheritorA))]
class InheritorTypeA : BaseType { }