Repository Design Pattern
Get flash to fully experience Pearltrees
Method 1: The Beginner's Out-of-the-Box Entity Framework Design
The pattern documented and named “Repository” is one of the most misunderstood and misused. In this post we’ll implement the pattern in C# to achieve this simple line of code:
The Repository Pattern is a common construct to avoid duplication of data access logic throughout our application. This includes direct access to a database, ORM, WCF dataservices, xml files and so on. The sole purpose of the repository is to hide the nitty gritty details of accessing the data. We can easily query the repository for data objects, without having to know how to provide things like a connection string. The repository behaves like a freely available in-memory data collection to which we can add, delete and update objects.
Introduction A popular pattern for ORM data access is the Repository pattern. Repositories are currently very popular even in EF for the reasons below: Hide EF from upper layer Make code better testable The big disadvantage of EF is rigid architecture which can be hardly mocked, so if you want to unit test upper layer you must wrap EF somehow to allow mocking its implementation.
I’m going to be talking at this evening’s Open Source .NET Exchange at Skills Matter . It’s just around the corner from Farringdon station and is ably organised by Gojko Adzic. I think there are still some places left. It’s free, but you need to register in advance. It should be great evening with some excellent speakers.
I have watch Rob Conery’s great screencast about MVC Storefront . If you haven’t seen them, you should take a look. Really interesting, he build and app by using Agile, "TDD" etc. I have some comments about his implementation I want to share, and if you don't agree with me, it's fine, because I'm not an expert, this post is based on my own experience and knowledge ;) Feel absolutely free to criticize me, but please give suggestions about what things can be done better, and also a reason why.
Implementing the Repository Pattern A data repository that implements the Repository pattern can provide dependency-free access to data of any type. For example, developers might create a class that reads user information from Active Directory, and exposes it as a series of User objects—each having properties such as Alias , EmailAddress , and PhoneNumber . The repository may also provide features to iterate over the contents, find individual users, and manipulate the contents. Third-party tools are available to help developers build repositories, such as the CodeSmith tools library. Visual Studio includes the tools to generate a typed DataSet that can expose values as properties, rather than as data rows, and this can form the basis for building a repository.