[Question] Get details of Inherited instances
See original GitHub issueI have a structure with multiple resources inheriting from an abstract resource, serving as main entry point. It’s using EF Core Table-per-hierarchy and discriminator configuration.
a GetAll call give me only the common fields from the abstract resource. I need to be able to retrieve the specific fields from each individual child types, and use them for filtering / sorting / selecting fieldsets.
I tried, as described in the documentation to configure specific Repositories or Services, without success. Also saw an MR from 2018 describing how to add a mapping to the configuration, but it seems this logic is now removed.
Would you get any advice on how to best implement this ?
Some code sample: EF Configuration:
{
builder.ToTable("LegalEntity");
builder.HasDiscriminator(e => e.EntityType)
.HasValue<IndividualEntity>(LegalEntityTypeEnum.Individual.ToString())
.HasValue<CompanyEntity>(LegalEntityTypeEnum.Company.ToString())
.HasValue<ListedEntity>(LegalEntityTypeEnum.PubliclyListed.ToString())
.HasValue<GovernmentEntity>(LegalEntityTypeEnum.Government.ToString());
....
Main Entity:
public abstract class LegalEntity : Identifiable<long>
{
[Attr]
public string DisplayName { get; set; }
//discriminator for the different types
[Attr]
[Column(TypeName = "varchar(24)")]
public string EntityType { get; set; }
...
public class IndividualEntity : LegalEntity
{
//basic info
[Attr]
public string Surname { get; set; }
[Attr]
public string Firstname { get; set; }
...
Controller:
public class LegalEntitiesController : JsonApiQueryController<LegalEntity, long>
{
public LegalEntitiesController(IJsonApiOptions options,
IResourceGraph resourceGraph,
ILoggerFactory loggerFactory,
IResourceService<LegalEntity, long> resourceService
) : base(options, resourceGraph, loggerFactory, resourceService)
{
}
}
JsonApi Config
services.AddJsonApi<TenantDbContext>(options =>
{
options.Namespace = "api";
options.UseRelativeLinks = true;
options.IncludeTotalResourceCount = true;
options.SerializerOptions.WriteIndented = false;
options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
#if DEBUG
options.IncludeExceptionStackTraceInErrors = true;
options.IncludeRequestBodyInErrors = true;
#endif
}, discovery => discovery.AddCurrentAssembly());
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
@bart-degreed thank you for all the work that went into that, greatly appreciated. It looks exactly like what we need and how i would have do it. I’ll be more than happy to integrate coming version with this change and test it.
Glad to hear, thanks for the feedback!