måndag 17 december 2007

Using datasets as business objects

I have to use datasets as business objects in an old .net 2.0 project, but a business object without business logic isn't that useful so I added some logic by using partial classes.
Examples of logic could be checking that the row is valid when a row is modified, like below, also you can add some methods to the database instance for convenience.
//Use the partial class of the dataset
partial class CustomerEnquiryDataset
{
//Dataset method available for the dataset instance
public string GetFirstEnquiryTitleString()
{
//Ugly, but an example of a dataset instance method...
return Tables["TblCustomerEnquiry"].Rows[0]["Title"].ToString();
}

//Create partial class of the dataset table to override EndInit()
public partial class TblCustomerEnquiryDataTable
{
public override void EndInit()
{
base.EndInit();
//Take action if a datarow changes in the TblCustomerEnquiryDataTable
this.RowChanged += new System.Data.DataRowChangeEventHandler(TblCustomerEnquiryDataTable_RowChanged);
}

private void TblCustomerEnquiryDataTable_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
{
TblCustomerEnquiryRow myOrderRow = (TblCustomerEnquiryRow) e.Row;
if (myOrderRow.Title.Length < 1)
{ throw new System.Exception("The title you entered is empty");
}
}

}
}

0 kommentarer:

Skicka en kommentar

Prenumerera på Kommentarer till inlägget [Atom]

<< Startsida