Skip to content
| Marketplace
Sign in
Visual Studio>Templates>EF. 4.x Repository Library
EF. 4.x Repository Library

EF. 4.x Repository Library

Debru Aklil

|
6,473 installs
| (1) | Free
A project template for consuming entity framework using “Repository” and “UnitofWork” pattern. Use this template when working with an EF 4.x release.
Download

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).
2. Hides the detailed data store mechanism/technology.
3. Code reusable and easy to use
4. Improve the code's maintainability and readability
5. Dependency Injection: With repository pattern we can use Unity containers to
inject the relevant object that we want to use in the code.

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
The repository designed to more generic and simple to manipulation; to a chive this all the repository method is defend as generic type. With this design the repository will only be need to be created once as instance member and use it to manipulate different entities in the 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
csharp
        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();        }
        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();         }

 

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft