Wednesday, 2 April 2014

Unit of work


-------------------------------------------------------------------------------------
public interface IUnitOfWork : IDisposable
   {
       ICourseRepository Courses { get; }
       IAuthorRepository Authors { get; }
       int Complete();
   }


-------------------------------------------------------------------------------------
public class UnitOfWork : IUnitOfWork
   {
       private readonly PlutoContext _context;
 
       public UnitOfWork(PlutoContext context)
       {
           _context = context;
           Courses = new CourseRepository(_context);
           Authors = new AuthorRepository(_context);
       }
 
       public ICourseRepository Courses { getprivate set; }
       public IAuthorRepository Authors { getprivate set; }
 
       public int Complete()
       {
           return _context.SaveChanges();
       }
 
       public void Dispose()
       {
           _context.Dispose();
       }
   }


public interface IRepository<TEntitywhere TEntity : class
    {
        TEntity Get(int id);
        IEnumerable<TEntity> GetAll();
        IEnumerable<TEntity> Find(Expression<Func<TEntitybool>> predicate);
 
        // This method was not in the videos, but I thought it would be useful to add.
        TEntity SingleOrDefault(Expression<Func<TEntitybool>> predicate);
 
        void Add(TEntity entity);
        void AddRange(IEnumerable<TEntity> entities);
        
        void Remove(TEntity entity);
        void RemoveRange(IEnumerable<TEntity> entities);
    }

-------------------------------------------------------------------------------------
public interface ICourseRepository : IRepository<Course>
   {
       IEnumerable<Course> GetTopSellingCourses(int count);
       IEnumerable<Course> GetCoursesWithAuthors(int pageIndex, int pageSize);
   }

-------------------------------------------------------------------------------------

public class Repository<TEntity> : IRepository<TEntitywhere TEntity : class
    {
        protected readonly DbContext Context;
 
        public Repository(DbContext context)
        {
            Context = context;
        }
 
        public TEntity Get(int id)
        {
            // Here we are working with a DbContext, not PlutoContext. So we don't have DbSets 
            // such as Courses or Authors, and we need to use the generic Set() method to access them.
            return Context.Set<TEntity>().Find(id);
        }
 
        public IEnumerable<TEntity> GetAll()
        {
            // Note that here I've repeated Context.Set<TEntity>() in every method and this is causing
            // too much noise. I could get a reference to the DbSet returned from this method in the 
            // constructor and store it in a private field like _entities. This way, the implementation
            // of our methods would be cleaner:
            // 
            // _entities.ToList();
            // _entities.Where();
            // _entities.SingleOrDefault();
            // 
            // I didn't change it because I wanted the code to look like the videos. But feel free to change
            // this on your own.
            return Context.Set<TEntity>().ToList();
        }
 
        public IEnumerable<TEntity> Find(Expression<Func<TEntitybool>> predicate)
        {
            return Context.Set<TEntity>().Where(predicate);
        }
 
        public TEntity SingleOrDefault(Expression<Func<TEntitybool>> predicate)
        {
            return Context.Set<TEntity>().SingleOrDefault(predicate);
        }
 
        public void Add(TEntity entity)
        {
            Context.Set<TEntity>().Add(entity);
        }
 
        public void AddRange(IEnumerable<TEntity> entities)
        {
            Context.Set<TEntity>().AddRange(entities);
        }
 
        public void Remove(TEntity entity)
        {
            Context.Set<TEntity>().Remove(entity);
        }
 
        public void RemoveRange(IEnumerable<TEntity> entities)
        {
            Context.Set<TEntity>().RemoveRange(entities);
        }
    }

-------------------------------------------------------------------------------------
public class PlutoContext : DbContext
{
    public PlutoContext()
        : base("name=PlutoContext")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
 
    public virtual DbSet<Author> Authors { getset; }
    public virtual DbSet<Course> Courses { getset; }
    public virtual DbSet<Tag> Tags { getset; }
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CourseConfiguration());
    }

-------------------------------------------------------------------------------------
public class UnitOfWork : IUnitOfWork
   {
       private readonly PlutoContext _context;
 
       public UnitOfWork(PlutoContext context)
       {
           _context = context;
           Courses = new CourseRepository(_context);
           Authors = new AuthorRepository(_context);
       }
 
       public ICourseRepository Courses { getprivate set; }
       public IAuthorRepository Authors { getprivate set; }
 
       public int Complete()
       {
           return _context.SaveChanges();
       }
 
       public void Dispose()
       {
           _context.Dispose();
       }
   }