Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a IRepository and what is it used for?

What is a IRepository? Why is it used, brief and simple examples won't hurt.

like image 519
Sevki Avatar asked Dec 24 '10 23:12

Sevki


People also ask

What is the purpose of a repository?

The main purpose of a repository is to store a set of files, as well as the history of changes made to those files.

What are repositories used for in C#?

According to the MSDN, “a repository is responsible for separating the logic of retrieving data and mapping it to the entity model from the business logic that acts on the model.” In simple terms, data comprising a data source layer – such as a database or a web service – should be agnostic from the business layer.

Why do we use unit of work pattern?

The unit of work class serves one purpose: to make sure that when you use multiple repositories, they share a single database context. That way, when a unit of work is complete you can call the SaveChanges method on that instance of the context and be assured that all related changes will be coordinated.

What is the benefit of repository pattern?

Benefits of Repository Pattern It centralizes data logic or business logic and service logic. It gives a substitution point for the unit tests. Provides a flexible architecture. If you want to modify the data access logic or business access logic, you don't need to change the repository logic.


1 Answers

MVC promotes separation of concerns, but that doesn't stop at the M V C level.

Data Access is a concern in itself. It should be done in the M bit of MVC, ie the model. How you structure your model is up to you, but people usually follow tried and tested patterns (why reinvent the wheel?). The Repository Pattern is the current standard. Don't expect a simple formula, however, because the variations are as many as there are developers, almost.

IRepository is just an interface that you create (it is not part of MVC or ASP.NET or .NET). It allows you to "decouple" your repositories from real implementations. Decoupling is good because it means your code...:

  1. Your code is much more reusable. This is just plain good.
  2. Your code can use Inversion of Control (or Dependency Injection). This is good to keep your concerns well separated. It is especially good because this allows Unit Testing...
  3. Your code can be Unit Tested. This is especially good in large projects with complex algorithms. It is good everywhere because it increases your understanding of the technologies you are working with and the domains you are trying to model in software.
  4. Your code becomes built around best practices, following a common pattern. This is good because it makes maintenance much easier.

So, having sold you decoupling, the answer to your question is that IRepository is an interface that you create and that you make your Repositories inherit from. It gives you a reliable class hierarchy to work with.

I generally use a generic IRepository:

IRepository

Where TEntity is, well, an entity. The code I use is:

using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace Wingspan.Web.Mvc {     public interface IRepository<TEntity> where TEntity : class     {         List<TEntity> FetchAll();         IQueryable<TEntity> Query {get;}         void Add(TEntity entity);         void Delete(TEntity entity);         void Save();     } } 

A concrete implementation of this interface would be:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq;  using Wingspan.Web.Mvc;  namespace ES.eLearning.Domain {     public class SqlRepository<T> : IRepository<T> where T : class     {         DataContext db;         public SqlRepository(DataContext db)         {             this.db = db;         }          #region IRepository<T> Members          public IQueryable<T> Query         {             get { return db.GetTable<T>(); }         }          public List<T> FetchAll()         {             return Query.ToList();         }          public void Add(T entity)         {             db.GetTable<T>().InsertOnSubmit(entity);         }          public void Delete(T entity)         {             db.GetTable<T>().DeleteOnSubmit(entity);         }          public void Save()         {             db.SubmitChanges();         }          #endregion     } } 

This allows me to write:

SqlRepository<UserCourse> UserCoursesRepository = new SqlRepository<UserCourse>(db); 

Where db is a DataContext instance injected into, say, a Service.

With UserCoursesRepository I can now write methods in my Service class like:

public void DeleteUserCourse(int courseId)         {             var uc = (UserCoursesRepository.Query.Where(x => x.IdUser == UserId && x.IdCourse == courseId)).Single();             UserCoursesRepository.Delete(uc);             UserCoursesRepository.Save();         } 

And now in my controllers, I can just write:

MyService.DeleteUserCourse(5); MyService.Save(); 

With this pattern the development of your app becomes more of an assembly line that leads up to a VERY simple controller. Every piece of the assembly line can be tested independently of everything else, so bugs are nipped in the bud.

If this is a long, unwieldy answer it is because the real answer is:

Buy Steven Sanderson's book Pro ASP.NET MVC 2 Framework and learn to think in MVC.

like image 159
awrigley Avatar answered Sep 24 '22 18:09

awrigley