Reputation: 1316
This week I've been experimenting with inheritance, something which until recently I hadn't used much.
In simplest terms: I need to implement a sign up system for Vendors, who once verified are able to purchase multiple packages. Rather than simply creating a Vendors class, I wanted to try out Table-Per-Type inheritance from a Person entity. Originally I had the below implementation.
This implementation worked as far as creating a PersonLogin and verifying the account. However as soon as I tried to use the PersonLogin as a Vendor I realized I was stuck as each Person must be of a specific sub-type and cannot be multiple.
So back to the drawing board I went: I redesigned my DB to have Vendors inherit from the PersonLogin entity, and the PersonLogin to inherit from the Person entity.
I am now implementing this second version, however as I'm relatively new to the world of inheritance I am not sure whether I have done it correctly or efficiently.
I want to find the best way to accomplish this functionality, whether I use inheritance or relationships is irrelevant.
Current Controller Action for adding Packages to Vendors:
[HttpPost]
public ActionResult RegisterPackage(int packageID, int personID)
{
if (ModelState.IsValid)
{
db.PackagesInVendors.Add(new PackagesInVendor() { PackageID = packageID, VendorID = personID });
db.SaveChanges();
return View("Index");
}
ViewBag.Packages = new SelectList(db.Packages, "ID", "Name", packageID);
ViewBag.People = new SelectList(db.People, "PersonID", "Email", personID);
return View();
}
All feedback or suggestions are most welcome, Cheers.
Upvotes: 0
Views: 136
Reputation: 6425
Good question and good investigation.
I'm keen to see what others post, but I 'save' TPT Inheritance for special cases where it would be more work to avoid database inheritance than to use it.
In your Vendor-Person scenario I would use composition over inheritance and include a ForeignKey Vendor.PersonId
I wouldn't use inheritance at all in your scenario.
An example where I might use inheritance would be if I had 'base type' Product that had 'categories' with their own properties, ie: ClothingProduct, ToyProduct, ExperienceProduct etc.
One gotcha of EF TPT inheritance is that it's easy to work with inherited tables in EF - but in one situation satellite websites were also using the raw data with sql queries and I am told it made data access for them much harder.
Upvotes: 1