[Entity Framework] DBContext API – Repository Pattern

 

When you work with data, the repository pattern is very useful. Rather than directly accessing data sources, you can separate business logic from data access logic. Business classes does not even know where the data come from.

 

1. Repository Pattern

When you  access data directly from your business logic, the data access logic and the business logic glue together. What if you need to get the data from another place? For example, you read the data from the SQL server and the new requirement says the data should be from WCF services. You need to delete and replace a lot of code from the business class.

The main goal of the Repository pattern is to separate business logic from data or service access logic.

The business class has a reference to the repository object and you can replace it with any other repository objects if they conform to the common interface.

 

2. Design

In most cases, the Repository pattern works with the Dependency Injection (DI) pattern. The business classes never create the concreate repository object. They always have a reference to the repository interface.

 

3. Implementation in MVC and EF  

When you create a repository pattern for MVC and EF, there is one more benefit. By creating the mockup repository, you can make the unit-test without hitting the physical database.

 

4. Example

The following code explains how the repository pattern can implemented in the real-life application.

  • Context class
public class Product
{
  int productId { get; set; }
}
public class ProductContext : DbContext
{
  public DbSet<Product> Products { get; set; }
}
  • Repository Interface and Class
public interface IProductRepository
{
  List<Product> ReadAll();
  Product ReadOne(int productId);
  void Insert(Product p);
  void Update(Product p);
  void Delete(Product p);
}

public class EFProductRepository : IProductRepository
{
  public List<Product> ReadAll()
  {
    List<Product> products;
    using (ProductContext context = new ProductContext())
    {
      products = context.Products.ToList<Product>();
    }
    return products;
  }

  public Product ReadOne(int productId)
  {
    Product product;
    using (ProductContext context = new ProductContext())
    {
      product = context.Products.Find(productId);
    }
    return product;
  }

  public void Insert(Product p)
  {
    using (ProductContext context = new ProductContext())
    {
      context.Products.Add(p);
      context.SaveChanges();
    }
  }

  public void Update(Product p)
  {
    using (ProductContext context = new ProductContext())
    {
      context.Entry<Product>(p).State = System.Data.EntityState.Modified;
      context.SaveChanges();
    }
  }

  public void Delete(Product p)
  {
    using (ProductContext context = new ProductContext())
    {
      context.Entry<Product>(p).State = System.Data.EntityState.Deleted;
      context.SaveChanges();
    }
  }
}
  • MVC Controller Class
public class ProductController : Controller
{
  private IProductRepository repository;

  public ProductController(IProductRepository r)
  {
    repository = r;
  }

  public ActionResult Index()
  {
    ViewBag.Message = "Product List";

    List<Product> model = repository.ReadAll();
    return View(model);
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s