Reputation: 1369
I have two different types of invoices i.e. one for a Consumer and the other for a Business. I have created an "Invoice" abstract class that the Consumer and Business invoices both inherit from. The Consumer invoice references a Consumer Entity and the Business invoice references a Business entity.
Now this works perfectly however the issue arises when I have a collection of "Invoices" in the context class. (Which will essentially contain Consumer and Business invoices). I can't get a reference to the "Business" object or the "Consumer" object. (Makes perfect sense... but I was just wondering how I get around this problem? I would imagine it is a pretty common scenario?)
Do I just create a new method in the abstract class called "GetInvoiceRecipient" that returns a "Recipient" object? The only issue with this solution though is that now I can't retrieve the invoices for a given consumer or given business?
Upvotes: 1
Views: 168
Reputation: 1326
I tend to use managing classes for this scenario. That is, a class that contains a dictionary containing a bunch of your classes, be they Consumer, Business, or classes that implement the interface IInvoice. The managing class can have public methods like Add(IInvoice, key) and Lookup(key) as IInvoice
As for your inheritance model, you may want to look into composition instead as it is generally a more flexible option. What is composition as it relates to object oriented design? though I'm sure it doesnt matter with only two subclasses.
Upvotes: 0
Reputation: 788
You could just create a Recipient Interface and let the "Consumer" class and the "Business" class implement it.
The abstract Invoice will "talk" only to the Recipient interface.
The Recipient interface will have methods called getName, getEmail or whatever you need. You have to redefine this method inside the Consumer and Business class.
This is a simple solution, but there are many others.
Upvotes: 2