5

A while back i created repositories and services using linq to sql and I struggled to understand it. I finally understood it but Now I'm trying to do the same thing but using Code First EF. I'm confused on how this works with code first. If I have one repository that I can just pass in a class object and have select(), ect...How does this interact or how do I connect this to the/a DbContext? If someone can point me in the right direction or give me some advice it would be appreciated. Not much on this stuff on google since it's a relatively new pattern still.

How to use / would I use DbSet? These repositories are cool but confusing.

   public class IRepository<T> : IDisposable
        where T : class, new()
{
    IQueryable<T> Select();

    IQueryable<T> SelectWith(params Expression<Func<T, object>>[] includeProperties);

    T GetById(int id);

    T GetByIdWith(int id, params Expression<Func<T, object>>[] includeProperties);

    void InsertOnCommit(T model);

    void DeleteOnCommit(T model);

}


public class DataContext : DbContext
{
}
1
  • You can declare a private field like private DataContext context in the repositories implement IRepository<T>. Commented Mar 13, 2012 at 6:22

1 Answer 1

6

Here is a Tutorial of Repository and UnitOfWork in EF from ASP.Net. Hope this help. (UnitOfWork is to make sure multiple repositores share the same DataContext)

Generic Repository:

   public class GenericRepository<TEntity> where TEntity : class 
    { 
        internal SchoolDBContext context; 
        internal DbSet<TEntity> dbSet; 

        public GenericRepository(SchoolDBContext context) 
        { 
            this.context = context; 
            this.dbSet = context.Set<TEntity>(); 
        } 

        public virtual IEnumerable<TEntity> Get( 
          Expression<Func<TEntity, bool>> filter = null, 
          Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, 
          string includeProperties = "") 
        {
        ...
        }

        public virtual TEntity GetByID(object id) 
        { 
           return dbSet.Find(id); 
        }

        public virtual void Insert(TEntity entity) 
        { 
          dbSet.Add(entity); 
        } 

      ...
     }

UnitOfWork: Call the Save() method to update all your changes in repositories.

public class UnitOfWork : IDisposable 
{ 
    private SchoolDBContext context = new SchoolDBContext(); 
    private GenericRepository<Department> departmentRepository; 

    public GenericRepository<Department> DepartmentRepository 
    { 
        get 
        { 

            if (this.departmentRepository == null) 
            { 
                this.departmentRepository = new GenericRepository<Department>(context); 
            } 
            return departmentRepository; 
        } 
    }

    public void Save() 
    { 
        context.SaveChanges(); 
    }
    ....
 }

Controller:

 public class CourseController : Controller 
 { 
     private UnitOfWork unitOfWork = new UnitOfWork(); 

     // 
     // GET: /Course/ 

     public ViewResult Index() 
     { 
         var courses = unitOfWork.CourseRepository.Get(includeProperties: "Department"); 
         return View(courses.ToList()); 
    }

    ....
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.