Inheritance in models
AbdoDabbas opened this issue · comments
Hi Charles,
I would like to ask you about your opinion in a use case I have.
Consider you have class Car
which is abstract, and from it inherit multiple types (Audi, BMW ...etc).
The current structure required by Aggregates.NET doesn't seem to support this inheritance (because I need to inherit the Entity<TEntity, TState>
to be used in the context.For<TEntity>().New(Id)
for example.
If I build the Audit : Car
I can't use it for context.For<Audi>
and I don't actually want to because if have another class to link to "a" car like Person 1->* Cars
I won't be able to know the Id of the List
of cars this Person
have of which type of cars, and also both Person
and Car
are Aggregate roots, so I can't put Car
as sub entity to Person
.
Any suggestion?
Inheritance and derivative entities is not a recommended pattern in DDD - there's quite a few people asking this same question but to spare you all the design talk.. its just a better idea to do composition instead of inheritance.
As your example - the object "Audi" doesn't actually need to be a defined object. Rather its the sum of its components and a maker/model label that make it an Audi.
For instance you could have a domain entity "Car" - which inside of it contains "Engine, Chassis, Seats, Exhaust, Tires, Suspension" as well as "Maker, and Model" entities. All of which together define an "Audi" - but "Audi" is never explicitly defined in code.