Passing Context object between Repository, Generic repository & unit of work

asp.net-mvc entity-framework entity-framework-6 repository unit-of-work

Question

I am working on an asp.net mvc web application & entity framework 6. i want to have these inside my application:-

  1. Generic repository class.

  2. Dedicated repository class for each entity type. Where this dedicated repository will be derived from the generic repository.

  3. Have a Unit of work class which will coordinate between the dedicated repositories so all the operations from the repositories will be wrapped into a single database transaction.

The Generic repository is:-

namespace SkillManagement.DAL
{
    public class GenericRepository<TEntity> where TEntity : class
    {
        internal SkillManagementEntities context;
        internal DbSet<TEntity> dbSet;

        public GenericRepository(SkillManagementEntities context)
        {
            this.context = context;
            this.dbSet = context.Set<TEntity>();
        }//code goes here...

The Interface

namespace SkillManagement.DAL
{

        public interface ISkillTypeRepository {



    }
}

One of the dedicated repository:-

namespace SkillManagement.DAL
{
    public class SkillTypeRepository :  GenericRepository<SkillType> , ISkillTypeRepository ,IDisposable
    {
        private SkillManagementEntities db = new SkillManagementEntities();
        public SkillTypeRepository(SkillManagementEntities db)
            : base(db)
        {

        }

        public void Dispose()
        {
            db.Dispose();

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

The unit of work

namespace SkillManagement.DAL
{
    public class UnitOfWork : IDisposable
    {
        private SkillManagementEntities db = new SkillManagementEntities();
        private SkillTypeRepository skillTypeRepository;
        private Repository2 repository2;
        private Repository3 repository3;
        // code goes here....


        public SkillTypeRepository SkillTypeRepository
        {
            get
            {

                if (this.skillTypeRepository == null)
                {
                    this.skillTypeRepository = new SkillTypeRepository(db);
                }
                return skillTypeRepository;
            }
        }

       //code goes here for repository2 , repository3 , etc..

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

Then inside my Controller I will be called the unite of work class as follow:-

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

      //
      // GET: /Course/

      public ViewResult Index()
      {
         var skilltype = unitOfWork.SkillTypeRepository.Get();
         return View(courses.ToList());
      }

So my question is basically about if I am passing the Context object correctly between the dedicated repository>>generic repository >>unit of work. So all the operations from multiple repositories will be wrapped into a single db transaction?

Can anyone advice please?

1
0
1/15/2015 1:31:02 AM

Popular Answer

I think just removing private SkillManagementEntities db = new SkillManagementEntities(); in all repositories is enough.

You've already had a single context private SkillManagementEntities db = new SkillManagementEntities(); is your unit of work. Just pass it to all of your repositories, don't create a new context inside any repository.

0
1/16/2015 2:54:42 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow