| West Wind Web Store .NET 2.0 |
Subclassing the Business Objects
|
The idea is if the framework changes later on your code sits on top of the framework so framework update won't overwrite any changes you've made.
There are two ways to extend the existing classes:
public partial busCustomer : wwBusiness<wws_customersRow>
What this means is that you can create another partial class with the same class signature that is then merged into a single class at compile time. Partial classes have some advantages over subclassing in that they allow injecting code at the current level of the class hierarchy vs. creating a subclass which may have an effect on the inheritance structure. On the downside partial classes can only add new functionality, but can't 'override' existing functionality defined in another partial class segment because any given class member can only be defined once.
All of the Web Store business objects are stored in the Westwind.WebStore.Business project. To subclass create a new class in the Westwind.WebStore.Business project and name it adequately. I will use the busInvoice object as an example and subclass it here for West Wind's operation. To do this I start by creating a new class - busInvoice_WestWind.
using System; using System.Data; namespace Westwind.WebStore { /// <summary> /// West Wind Specific subclass of the Invoice object /// </summary> public class busInvoice_WestWind : busInvoice { public override decimal CalculateShipping() { ... my code here to override the shipping behavior } } }
Since we're subclassing from an existing business object we don't need to set up a constructor with the common configuration settings (connection string, connection type etc) as we're inheriting this from the parent class.
Next we need to change the WebStoreFactory class so that it uses our new class instead of the default busInvoice class. The Web Store always uses this factory method.
So instead of direct instantiation:
busInvoice Invoice = new busInvoice();
it calls the factory method:
busInvoice Invoice = WebStoreFactory.GetbusInvoice();
To use your newly created class instead of the default class, change the WebStoreFactory class in the Westwind.WebStore.Business project and make it return our custom instance. Change the WebStoreFactory.GetbusInvoice() method to:
public static busInvoice GetbusInvoice() { return new busInvoice_WestWind(); }
And voila the store now always uses your custom class for all operations. If you're only overriding existing methods or properties you don't need to do anything else. If you add new methods or properties to your objects however you have to make sure to cast the object to the proper type. For example:
busInvoice_WestWind Invoice = (busInvoice_WestWind) WebStoreFactory.GetbusInvoice(); Invoice.MyCustomProperty = "Custom value";
Last Updated: 3/20/2007 |
Send topic feedback