question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Get details of Inherited instances

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Anthony-Michelcommented, Mar 22, 2022

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

0reactions
bkoelmancommented, Sep 8, 2022

Glad to hear, thanks for the feedback!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I get all instances that inherited from the same ...
I have two models than inherited from the same abstract base class. I would expect to be able to get all instances from...
Read more >
Inheritance in Java Example
Let's write a simple test class to create a Cat object and use some of its methods. ... Cat class doesn't have getEats()...
Read more >
Python Inheritance (With Examples)
In this tutorial, we will learn about Python inheritance and its types with the help of examples.
Read more >
Inheritance - Learning the Java Language
You can declare new fields in the subclass that are not in the superclass. The inherited methods can be used directly as they...
Read more >
OOP Inheritance & Polymorphism - Java Programming ...
Java Programming Tutorial. OOP - Composition, Inheritance & Polymorphism. There are two ways to reuse existing classes, namely, composition and inheritance.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found