Entity Framework Repository Pattern

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.

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, User Repository and Product 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. Please click Here to download the source code

How to use it

var rep = IoC.Resolve();
Person p = new Person();
p.Name = “David Tylor”;
var pers= rep.Search(s=> s.Name.Equals(“David Tylor”));
pers = rep.GetAll();

This entry was posted in Entity Framework. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *