UNIT of Work Design Pattern

Start from the beginning
                                        

private int _CustomerCode = 0;

public int Id

{

get { return _CustomerCode; }

set { _CustomerCode = value; }

}

private string _CustomerName = "";

public string CustomerName

{

get { return _CustomerName; }

set { _CustomerName = value; }

}

public void Insert()

{

DataAccess obj = new DataAccess();

obj.InsertCustomer(_CustomerCode, CustomerName);

}

public List<IEntity> Load()

{

DataAccess obj = new DataAccess();

Customer o = new Customer();

SqlDataReader ds = obj.GetCustomer(Id);

while (ds.Read())

{

o.CustomerName = ds["CustomerName"].ToString();

}

List<IEntity> Li = (new List<Customer>()).ToList<IEntity>();

Li.Add((IEntity) o);

return Li;

}

public void Update()

{

DataAccess obj = new DataAccess();

obj.UpdateCustomer(_CustomerCode, CustomerName);

}

}

I am not showing the data access code in the article to avoid confusion. You can always the download the full code which is attached with the article.

Step 3:- Create the unit of work collection

The next step is to create the unit of work collection class.  So below is a simple class we have created called as “SimpleExampleUOW”. This class I have made it as a generic class so that I can attach this class with any business object entity.

This class has 2 generic collections defined as a class level variable one is “Changed” and other is “New”. The “Changed” generic collection will store entities which are meant for updates. The “New” generic collection will have business entities which are fresh / new records.

Please note in this demo I have not implemented delete functionality. I leave that as a home work  " /> .

 Collapse | Copy Code

public class SimpleExampleUOW<T> where T : IEntity

{

private List<T> Changed = new List<T>();

private List<T> New = new List<T>();

…..

…..

UNIT of Work Design PatternWhere stories live. Discover now