Generic implementation of Repository pattern for Entity Framework Code First

I have been inspired by beautiful MVC starter kit by Rob Conery from ASP.NET MVC 2 Starter Kit and i 've created implementation of Repository pattern for my project where i use Entity Framework Code First and ASP.MVC 3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace YourApplication.Web.Models
{
    public class Repository : IRepository
    {
        readonly YourApplicationDbContext _yourApplicationDbContext;

        public Repository()
        {
            if(_yourApplicationDbContext == null)
            {
                _yourApplicationDbContext = new yourApplicationDbContext();
            }
        }

        public void CommitChanges()
        {
            _yourApplicationDbContext.SaveChanges();
        }

        public void Delete<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            var query = All<T>().Where(expression);
            foreach (var item in query)
            {
                Delete(item);
            }
        }

        public void Delete<T>(T item) where T : class, new()
        {
            _yourApplicationDbContext.Set<T>().Remove(item);
        }

        public void DeleteAll<T>() where T : class, new()
        {
            var query = All<T>();
            foreach (var item in query)
            {
                Delete(item);
            }
        }

        public T Single<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            return All<T>().FirstOrDefault(expression);
        }

        public IQueryable<T> All<T>() where T : class, new()
        {
            return _yourApplicationDbContext.Set<T>().AsQueryable();
        }

        public void Add<T>(T item) where T : class, new()
        {
            _yourApplicationDbContext.Set<T>().Add(item);                  
        }

        public void Add<T>(IEnumerable<T> items) where T : class, new()
        {
            foreach (var item in items)
            {
                Add(item);
            }
        }

        public void Update<T>(T item) where T : class, new()
        {
            //nothing needed here
        }
    }
}

How to use:

for example you have Industry model, then

 public ActionResult Index()
        {
            var industries = Repository.All<Industry>();
            return View(industries);
        }

or you have Company model, then

 public ActionResult Edit(int id)
        {
            var company = Repository.Single<Company>(c => c.ID.Equal(id));
            company.Title = "Mega cool new title";
            Repository.CommitChanges();  
            return View(company);
        }

Or suppose you have Address model and want to add a new record

 public ActionResult Edit(int id)
        {
           var address = new Address();
           address.Street = "Mega street name";
           Repository.Add<Address>(address);
           Repository.CommitChanges();
        }

Well, i hope this could be useful :)

7 Comments

Comments have been disabled for this content.