I have 3 abstract classes (Customer, Tender, Site). For Customer has inherited class such as CorporateCustomer. For Tender has PreTender, InTender, PostTender concrete classes. Site has CorporateSite class.
I’ve already layout our the method, properties nicely, relationship between this objects and tested works well.
In MyTestApp implementation:
Customer customer = new CorporateCustomer(35); // ==> This will pass CustomerID = 35
string customerTenderName = customer.InTenders[0].Name; // ==> Customer has many Tender
Customer customer = new CorporateCustomer(35); // ==> This will pass CustomerID = 35
string customerSiteName = customer.CorporateSites[0].Name; // ==> Customer has many Site
Within my Site also has implemented Get():
Site site = new CorporateSite();
List<Site> = site.Get(); // ==> This will return all sites
Now the issue is that I want to do the following as well with the same Get()
Customer customer = new CorporateCustomer(35);
List<Site> = customer.CorporateSite.Get(); // ==> This will return all site belong to Customer (at this stage I don't know how to do this as yet)
As you can see because it’s using the same Get() method how differentiate this without using like this GetByCustomer().
Is there any way somehow that you within the Get() i have a “checking” which one is relevant needs to be displayed based on the object/class is instantiate or something.
Thank you
You should be able to do this with a Strategy that you can inject into the CorporateSite class (and other classes). The CorporateSite class then uses the Strategy to implement the Get method.
In this example I use a
Func<IList<Site>>as the Strategy (since delegates are excellent anonymous interfaces), but you can use an interface or abstract class instead if you would rather prefer that.Imagine that you modify the CorporateSite class to take a Strategy as input:
If you have a good default implementation, you can add a constructor overload that uses a default Strategy, but I am leaving that as en exercise for the reader.
The point here is that the CorporateSite class itself does not implement the Get method but rather defers that implementation to the Strategy.
The Customer class can then inject the desired Strategy into the CorporateSite instance it exposes:
Other classes that need to expose a CorporateSite can do the same while applying their own Get logic.
You can use the same pattern to implement other classes that need to have a Get method.