The repository and unit of work design patterns are used to create an abstraction layer between the data access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store and provides the following design benefits: 1. TDD- facilitates automated test-driven development (TDD). Enterprise Data Repository (EDR): is a library design to be used in both web and standalone/non web application. It manages the lifecycle of the object context per http request for web application and one per thread in non-web applications. Repository Unit of Work Entity framework handles transaction across many operations over many different types. But in order to use multiple repository from the consuming application and able to synchronize multiple transaction across queries require a little bit of work, for instance, Repositoryand Repository, we would not able to run queries across both, as they would be created via different instances of Object Context. One of the ideal patterns to resolve this problem will be to use unit of work patter. With Unit of Work we can coordinate the different repositories object context creation and destruction. The full source code is available in codeplex; Please click Here to download the source code How to use it
C# Edit|Remove csharpIDataRepository rep = IoC.Resolve<IDataRepository>(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) Bind(); } protected void AddNewPerson(object sender, EventArgs e) { Person per = new Person(); per.Name = ((TextBox)grdPerson.FooterRow.FindControl("txtName")).Text; rep.Add<Person>(per); Bind(); } protected void EditPerson(object sender, GridViewEditEventArgs e) { grdPerson.EditIndex = e.NewEditIndex; Bind(); } protected void CancelEdit(object sender, GridViewCancelEditEventArgs e) { grdPerson.EditIndex = -1; Bind(); } protected void UpdatePerson(object sender, GridViewUpdateEventArgs e) { Person per = new Person(); per.Id =Int32.Parse( ((Label)grdPerson.Rows[e.RowIndex].FindControl("lblID")).Text); per.Name = ((TextBox)grdPerson.Rows[e.RowIndex].FindControl("txtName")).Text; rep.Update<Person>(per); Bind(); } protected void DeletePerson(object sender, EventArgs e) { LinkButton lnkRemove = (LinkButton)sender; int id=Int32.Parse(lnkRemove.CommandArgument); rep.Delete<Person>(prop => prop.Id == id); Bind(); } protected void OnPaging(object sender, GridViewPageEventArgs e) { Bind(); grdPerson.PageIndex = e.NewPageIndex; grdPerson.DataBind(); } private void Bind() { var pers = rep.GetAll<Person>(); grdPerson.DataSource = pers; grdPerson.DataBind(); } IDataRepository rep = IoC.Resolve<IDataRepository>(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) Bind(); } protected void AddNewPerson(object sender, EventArgs e) { Person per = new Person(); per.Name = ((TextBox)grdPerson.FooterRow.FindControl("txtName")).Text; rep.Add<Person>(per); Bind(); } protected void EditPerson(object sender, GridViewEditEventArgs e) { grdPerson.EditIndex = e.NewEditIndex; Bind(); } protected void CancelEdit(object sender, GridViewCancelEditEventArgs e) { grdPerson.EditIndex = -1; Bind(); } protected void UpdatePerson(object sender, GridViewUpdateEventArgs e) { Person per = new Person(); per.Id =Int32.Parse( ((Label)grdPerson.Rows[e.RowIndex].FindControl("lblID")).Text); per.Name = ((TextBox)grdPerson.Rows[e.RowIndex].FindControl("txtName")).Text; rep.Update<Person>(per); Bind(); } protected void DeletePerson(object sender, EventArgs e) { LinkButton lnkRemove = (LinkButton)sender; int id=Int32.Parse(lnkRemove.CommandArgument); rep.Delete<Person>(prop => prop.Id == id); Bind(); } protected void OnPaging(object sender, GridViewPageEventArgs e) { Bind(); grdPerson.PageIndex = e.NewPageIndex; grdPerson.DataBind(); } private void Bind() { var pers = rep.GetAll<Person>(); grdPerson.DataSource = pers; grdPerson.DataBind(); }
|