❔ Inheritance or object references ? which one is more meaningful

public class PetService : BaseEntity
{
public string Name { get; set; } = string.Empty;
public string? Image { get; set; }
public string Description { get; set; } = string.Empty;
}
public class PetService : BaseEntity
{
public string Name { get; set; } = string.Empty;
public string? Image { get; set; }
public string Description { get; set; } = string.Empty;
}
My clinic should be like this:
public class ClinicService : BaseEntity
{
public PetService PetService { get; set; } = default!;
public decimal Price { get; set; }
}
public class ClinicService : BaseEntity
{
public PetService PetService { get; set; } = default!;
public decimal Price { get; set; }
}
or
public class ClinicService : PetService
{
public decimal Price { get; set; }
}
public class ClinicService : PetService
{
public decimal Price { get; set; }
}
4 Replies
JakenVeina
JakenVeina2y ago
if this is just a data model, neither is really more correct than the other best I would say is "Is it that a ClinicService IS a PetService or does it own/manage a PetService"? if this is a behavioral entity instead of just a data model, probably inheritance is what you want but, really, the question still applies
Florian Voß
Florian Voß2y ago
it depends on your design, whether inheritance makes sense or not depends on how much the types have in common. But generally speaking there is the oop principle "favor composition over inheritance". So most of the times when in doubt, use composition as it gives you runtime flexibility while inheritance only takes static behaviour from parent at compile time also think about semantics, inheritance is a is relationship is a ClinicService a PetService? I dont think it is, doesnt make sense to me semantically if anything a PetService is a ClinicService but composition seems most reasonable, as in a ClinicService (which could f.e. be a HospitalService) may have a PetService as one of the services they offer in UML class diagram terms, a ClinicService has 0..1 PetService is how I would do it
Anton
Anton2y ago
as a rule of thumb, never use inheritance unless you're forced to
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?