| West Wind Web Store .NET 2.0 |
wwBusiness: DataRow based Business Objects
|
To create a standard business object the process consists of:
using Westwind.BusinessObjects;
above the namespace reference.
public class busInvoice : wwBusiness
I like to prefix my business objects with a bus prefix so they are easy to discover in Intellisense and to avoid name overlap with framework level classes that might be confusing, but that's entirely up to you.
The following snippet demonstrates all of the steps:
namespace Westwind.WebStore { public class busCustomer : wwBusiness { public busCustomer() { // *** Preferrably set the connection string from a config store this.ConnectionString = App.Configuration.ConnectionString; // *** Primary mapped table this.Tablename = "wws_customers"; // *** Set new row values to 'blank' values rather than nulls this.NewRowBlankValues = true; // *** Use custom updates rather than DataAdapter this.DataRowUpdateMode = DataRowUpdateModes.OptimizedByPk; // *** Values set here only for reference - these are defaults this.ConnectType = ServerTypes.SqlServer; this.PkField = "Pk"; this.PkType = PkFieldTypes.intType; } } }
This creates a new business object with default data settings. Once these are set the business object is ready for use in your own code.
You should always set the connection string from some sort of configuration store that can be changed. This can be ConfigurationManager.ConnectionStrings - here I'm using the wwAppConfiguration manager and a application wide ConnectionString value to hold this value. You can also set this manually in your application of course but it's recommended this is done here for encapsulation.
The other thing that you typically will want to do is map the business object to a primary table in the database. This is the table that will automatically be used for CRUD operations. You also need to specify the primary key field and type of the Pk. The business object can handle creating new Pks for you, or your can override the CreateNewId() method to generate Pks of your own. By default wwBusiness uses Integer Pks and generates a new value using a stored procedure and reference table.
NewBlankRowValues is optional but used in all objects of the store - this sets all fields to default blank values rather than nulls to avoid update problems. Finally the tablename is explicitly set and mapped to an underlying database table that is used as the default table updated by this business object if you don't override Load, GetBlankRecord and Save methods.
busInvoice Invoice = new busInvoice();
To create a Factory method you'd use the WebStoreFactory class for example and implement:
public class WebStoreFactory public static busInvoice GetbusInvoice() { return new busInvoice(); } }
If at some point in the future you decide to subclass busInvoice into busMyNewInvoice you change that to:
public static busInvoice GetbusInvoice() { return (busInvoice) new busMyNewInvoice(); }
which makes sure that existing code still works just fine. Anytime you need the new functionality you can then simply case the object to your specific business object type:
busInvoice Invoice = WebStoreFactory.GetbusInvoice(); busMyNewInvoice MyInvoice = Invoice as busMyInvoice;
At this point you're ready to use the business object in your application. The following demonstrates a few things you can do:
public string DataRowBusTest() { busInvoice Customer = new busCustomer(); // *** Execute query and load into TCustomers Table of internal DataSet if (Customer.Execute("select * from " + Customer.Tablename, "TCustomers") = -1) { this.lblErrorMessage.Text = Customer.ErrorMessage; return false; } int Count = Customer.DataSet.Tables["TCustomers"].Rows.Count; // *** Load a customer by pk - load DataRow property if (!Customer.Load(1000)) { this.lblErrorMessage.Text = Invoice.ErrorMessage; return false; } // *** Read some values string Company = (string) Invoice.DataRow["company"]; decimal Entered = (DateTime) Invoice.DataRow["entered"]; // *** Update data Customer.DataRow["Updated"] = DateTime.Now.Date; if (!Customer.Save()) { this.lblErrorMessage.Text = Customer.ErrorMessage; return false; } // *** Creating a new customer Customer.New(); // *** New Pk value is generated before data is saved int Pk = (int)Customer.DataRow["pk"]; Customer.DataRow["company"] = "West Wind"; Customer.DataRow["address"] = "32 Kaiea"; Customer.Save(); // *** Delete a customer by pk Customer.Delete(Pk); return true; }
Last Updated: 3/4/2007 |
Send topic feedback