Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type is not a valid type to map #347

Open
grounzero opened this issue Apr 6, 2023 · 7 comments
Open

Type is not a valid type to map #347

grounzero opened this issue Apr 6, 2023 · 7 comments
Labels

Comments

@grounzero
Copy link

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.

@Turnerj
Copy link
Member

Turnerj commented Apr 7, 2023

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).

@grounzero
Copy link
Author

grounzero commented Apr 13, 2023

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?

@Turnerj
Copy link
Member

Turnerj commented Apr 13, 2023

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.

@grounzero
Copy link
Author

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.

@Turnerj
Copy link
Member

Turnerj commented Apr 14, 2023

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.

@grounzero
Copy link
Author

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?

@grounzero
Copy link
Author

grounzero commented Apr 17, 2023

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 { }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants