azure-cosmos-dotnet-repository: Cannot use GetAsync() to retrieve subclasses of T
Describe the bug Cannot retrieve subclass types.
To Reproduce
- My repository is a
DefaultRepository<Offering>
. - My project has two subclasses:
Document : Offering
Module : Offering
. - When storing them I can see that the
type
property in Cosmos DB is set toDocument
, respectivelyModule
. - I’m using
GetAsync()
to retrieve anOffering
, however I’m always getting NULL back becauseGetAsync()
refuses to return an instance if thetype
property’s value does not match (https://github.com/IEvangelist/azure-cosmos-dotnet-repository/blob/main/Microsoft.Azure.CosmosRepository/src/DefaultRepository.cs#L79).
Expected behavior Return an instance of the subtype.
Actual behavior Returns NULL.
Environment summary SDK Version: 2.8.0 OS Version: Windows 11
Additional context
I get the idea of a repository being meant for one specific type. However, there’s something odd about the logic, I think. If I can save any type to my IRepository<T>
, I should also be able to retrieve it again. A workaround for me would be to have two repositories, one for each specific sub type. However, that would break my logic to (for example) get all offerings of a specific date, which would be a mixed list of documents and modules.
To overcome the limitation, I also tried to implement my own IRepository<T>
instead of using DefaultRepository<T>
, so I could add sub type support. However, as most of the classes are internal, it’s not possible to implement your own repo, unless I’m missing something?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 35 (3 by maintainers)
This is a proof of concept: https://gist.github.com/Krumelur/a875f8d0dd6af9a27b8b51d5e4dd59d3
By adding
[JsonConverter(typeof(OfferingJsonConverter))]
, the custom converter will be executed. However, I need to add set theItem.Type = string.Empty
and must add anotherOfferingType
property to make it work.Long story short, I think if there were an option to ignore
Type
:What do y’all think?
When I first started testing the repository, I added a bunch of items and it was working nicely. Those items were subclasses of a base type. All properties of the subclasses end up in the item’s JSON. To answer your question/concern: writing is not the issue.