다음을 통해 공유


LINQ: The Repository Pattern

What is the Repository Pattern?

The repository pattern provides a way to provide isolation between the data access layer of your application with the business layer. In using the repository pattern, you can have a separation of concerns in your data access code.

You can find more information here: http://msdn.microsoft.com/en-us/library/ff649690.aspx

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 to store, 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>();  
 }  
} 

 

To implement the repository pattern for Entity Framework, you can start by defining an interface as shown below:

public interface IRepository<T>  {   T GetById(int id);   T[] GetAll();   IQueryable<T> Query(Expression<Func<T, bool>> filter);   void Save(T entity);   void Delete(T entity);  }

Now that the interface has been defined, you can use it to implement your repository classes. The following code listing shows how you can implement the StudentRepository class:

public class StudentRepository : IRepository<Student>, IDisposable   {   private MyEntities _context;     public StudentRepository()   {   _context = new MyEntities();   }     public Student GetById(int id)   {   return _context.Students.     Where(s => s.StudentID == id).FirstOrDefault();   }     public Student[] GetAll()   {   return _context.Students.ToArray();   }     public IQueryable<Student> Query(Expression<Func<Student, bool>> filter)   {   return _context.Students.Where(filter);   }     public void Save(Student student)   {   // Code to Save a student record to the database   }     public void Delete(Student student)   {   // Code to delete a student record   }     public void Dispose()   {   if (_context != null)   {   _context.Dispose();   }   }   }