| West Wind Web Store .NET 2.0 |
Overriding common Methods
|
Some of the methods you will likely override are:
public override bool Validate() { if ( string.IsNullOrEmpty(this.Entity.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='" + this.Entity.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; }
public override bool New() { if (!base.New()) return false; this.Entity.CountryId="US"; this.Entity.State = ""; this.Entity.Entered = DateTime.Now; return true; }
public override bool Save() { if (this.AutoValidate && !this.Validate()) return false; this.Entity.Updated = DateTime.Now; this.Entity.Password = this.Entity.Password.ToLower(); this.Entity.Email = this.Entity.ToLower(); return base.Save(); }
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