Unit of Work pattern

We implement the Unit of Work (UOW) pattern to avoid multiple calls to the database server on each object change. With Repository, we store the object state on any particular transaction and submit the changes once through the UOW pattern.

The following is the interface of Unit of Work that exposes four methods to begin and end transactions and to save changes:

    public interface IUnitOfWork 
{
void BeginTransaction();

void RollbackTransaction();

void CommitTransaction();

void SaveChanges();
}

The next item is the implementation of Unit of Work, which takes the DbFactory instance, and allow methods to begin and end transactions and call the SaveChanges method to push the changes in one call to the database. This way, a single call is made to the database server and any operation performed on the Repository will be done in-memory, within the database context:

    public class UnitOfWork : IUnitOfWork 
{
private IDbFactory _dbFactory;

public UnitOfWork(IDbFactory dbFactory)
{
_dbFactory = dbFactory;
}


public void BeginTransaction()
{
_dbFactory.GetDataContext.Database.BeginTransaction();
}

public void RollbackTransaction()
{
_dbFactory.GetDataContext.Database.RollbackTransaction();
}

public void CommitTransaction()
{
_dbFactory.GetDataContext.Database.CommitTransaction();
}

public void SaveChanges()
{
_dbFactory.GetDataContext.Save();
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.102.159