Skip to content

[Question] Get details of Inherited instances  #1138

Closed
@Anthony-Michel

Description

@Anthony-Michel

I 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());

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions