Subclassing the Business Objects

If you plan on making changes to the business rules it's recommended you spend a little time getting familiar with the business object framework that the Web Store is based on. The wwBusiness class is easy to pick up and work with after you have a basic understanding of its relatively simple concepts of retrieving and updating data.

Making Changes? Subclass the base objects or create partial Classes

The first step in making changes is to subclass or create partial classes of the base class you want to change, so that the changes you make don't overwrite existing code. You should never change code in the original business object classes.

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:

Partial Classes

All of the business objects are created as partial classes, which means they are defined like so:

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.

Subclassing


Subclassing is a two step process:

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";

Use a separate project for customizations

It might be a good idea to create your customizations in a separate project so your changes are clearly delineated from the base project. To do this:

If you separate the projects out like this you can isolate all of your changes into the separate project. The one exception is the WebStoreFactory methods, which must be changed in the original Westwind.WebStore.Business project. All other customizations can be moved to the new project.



 Last Updated: 3/20/2007 | Send topic feedback