Applying The Repository Pattern on LINQ and ADO.NET Entity Framework 4

Introduction The Repository Design Pattern

The Repository design pattern adds a layer of abstraction on top of the query layer and helps to eliminate duplicate query logic in your application’s code. In his great book “Patterns of Enterprise Application Architecture”, Martin Fowler defines the Repository Pattern as: “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.”
This article takes a look at the principles of the repository pattern and discusses how it can be implemented in applications that make use of LINQ and the Entity Framework 4.0.

Pre-requisites

To use the code examples illustrated in this article, you should have any one of the following installed in your system:

  • Microsoft Visual Studio 2008
  • Microsoft Visual Studio 2010

What is the Repository Pattern?

A repository is essentially a collection of in memory domain objects. The repository pattern provides a way to provide isolation between the data access layer of your application with the presentation and business layer. In using the repository pattern, you can have persistence, ignorance and a separation of concerns in your data access code. The repository encapsulates the necessary logic to persist your domain objects in the underlying data store. In essence, you can use the repository pattern to decouple the domain model from the data access code.

Martin Fowler states: “A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.” Reference: http://martinfowler.com/eaaCatalog/repository.html

The following code snippet illustrates how a repository interface is designed:

    public interface IStudentRepository
       {
           Student GetByID(int studentID);
           Student Load(int studentID);
           void Save(Student student);
           void Delete(Student student);
       }
 

Similar to the interface for the Student Repository, you can have interfaces for Product and Order Repositories as shown below:

   public interface IProductRepository
       {
           Product GetByID(int productID);
           Product Load(int productID);
           void Save(Product product);
           void Delete(Product product);
       }

   public interface IOrderRepository
       {
           Order GetByID(int orderID);
           Order Load(int orderID);
           void Save(Order order);
           void Delete(Order order);
       }
 

You can now use generics to generalize this interface. Here’s a generic implementation of the interface for any repositories of this kind.

  public interface IRepository<T>
  {
      T GetById(int id);
      T Load(int id);
      void Save(T entity);
      void Delete(T entity);
  }

Designing a LINQ Repository

To implement the repository pattern for LINQ, you would first need to define the interface that would contain the declaration of all the methods you would want in your Repository.

The following interface shows how you can use the repository pattern in LINQ:

   public interface IRepository<T> where T : class
       {
           IQueryable<T> GetAll();
           void InsertOnSubmit(T entity);
           void DeleteOnSubmit(T entity);
           void SubmitChanges();
       }
 

You can then use your repository to encapsulate the way storage, retrieval and query would be accomplished in your data access code using the LINQ data context. The IRepository interface is implemented by the repository class as shown in the code listing below:

   public class Repository<T> : IRepository<T> where T : class
       {
           public DataContext Context
           {
               get;
               set;
           }

           public virtual IQueryable<T> GetAll()
           {
               return Context.GetTable<T>();
           }
           public virtual void InsertOnSubmit(T entity)
           {
               GetTable().InsertOnSubmit(entity);
           }
           public virtual void DeleteOnSubmit(T entity)
           {
               GetTable().DeleteOnSubmit(entity);
           }
           public virtual void SubmitChanges()
           {
               Context.SubmitChanges();
           }
           public virtual ITable GetTable()
           {
               return Context.GetTable<T>();
          
    }
       }
 

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read