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:

  • Validate()
    If you have any validation needs you will certainly override this method and add any validation rules to the ValidationErrors collection. A typical implementation might look like this:

    public override bool Validate()
    {
    	if ( wwUtils.Empty((string) DataRow["Sku"]) ) 
    		this.ValidationErrors.Add("Sku must be filled in.","txtSku");
    	else 
    	{
    		// *** Check to see if it exists
    		if (this.DataRow.RowState == DataRowState.Added &&
    			this.Execute("select sku from " + this.Tablename + " where sku='" + (string) DataRow["Sku"] +"'","Temp") > 0)
    			this.ValidationErrors.Add("Invalid Sku - this Sku exists already. Please pick another.","txtSku");
    	}
    
    	//... additional checks
    
    	if (this.ValidationErrors.Count > 0) 
    		return false;
    
    	return true;
    }

  • New()
    Here you can cause default values to be set after the original New method was fired.
    public override bool New() 
    {
    	if (!base.New())
    		return false;
    
    	this.DataRow["CountryId"]="US";
    	this.DataRow["State"] = "";
    	this.DataRow["Entered"] = DateTime.Now;
    	return true;
    }

  • Save()
    Here you can fix up existing values and apply formatting or other final fixups before the data has been written.
    public override bool Save() 
    {
    	if (this.AutoValidate && !this.Validate())
    		return false;
    	
    	// *** Update the country for display purposes from the CountryId
    	// *** Check if value has changed - otherwise we don't want to go back to the database
    	if ( this.DataRow.RowState == System.Data.DataRowState.Added || 
    			(string) this.DataRow["CountryId"] != (string) this.DataRow["CountryId",System.Data.DataRowVersion.Original])
    		this.DataRow["Country"] = new busLookups().GetCountryFromCountryId((string) this.DataRow["CountryId"]);
    
    	this.DataRow["Updated"] = DateTime.Now;
    	this.DataRow["Password"] = this.DataRow["Password"].ToString().ToLower();
    	this.DataRow["Email"] = this.DataRow["Email"].ToString().ToLower();
    
    	return base.Save();
    }

Load() method overloads

Another common common method to overload is the Load() method which loads up a single DataRow with data from the server. The Load method by default works only with a single parameter which is the PK. 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.


© West Wind Technologies, 1996-2018 • Updated: 01/21/04
Comment or report problem with topic