Overriding common Methods

As you create new business objects you'll likely want to override existing methods of the business objects to implement custom behaviors. For the most part you can simply override methods and call back to the base classes, but in some instances you'll want to completely rip out the existing methods and replace them. This topic demonstrates a few common examples of implementations both simple and complete.

Some of the methods you will likely override are:

Load() method overloads

Another common common method to overload is the Load() method which loads up a single DataRow/Entity with data from the server. The Load method by default works only with a single parameter which is the PK. But frequently you might want to do create lookups for non-PK values as well.

For example, if you have an Inventory table you probably will want to look up an item by SKU rather than by PK. To do this you can create a custom Load method. The wwBusiness object base class includes a method that makes this easy called

If you want to use something other than the PK it's recommended that you create a new method. In this method you have to reimplement the logic that Load() performs.

Here's an example of a method that retrieves the record by UserId instead of by PK. Let's start with the long hand so you get an idea what happens behind the scenes:

public bool LoadByUserId(string lcUserId) { if (lcUserId == null || lcUserId.Length == 0) return false; int lnResult = this.Execute("select * from " + this.Tablename + " where UserId='" + lcUserId + "'",this.Tablename); if (lnResult < 1) { this.SetError("Customer not found."); return false; } this.DataRow = this.DataSet.Tables[ this.TableName ].Rows[0]; return true; }

Note that Load() always creates a DataRow in a table that has the same name as the underlying table, hence the this.Tablename references.

You can also rely on the LoadBase() method which automatically performs the DataRow assignment and error handling:

public bool LoadByUserId(string lcUserId) { if (lcUserId == null || lcUserId.Length == 0) return false; return this.LoadBase("select * from " + this.Tablename + " where UserId='" + lcUserId + "'"; }

This is the preferred mechanism to perform this task.


 Last Updated: 12/27/2006 | Send topic feedback