0
votes

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
EF6 already provides you everything you are asking for. Why wrap it all up again in an additional, and unnecessary layer?Charlie Brown
i already have previous discussions on this. now i know that EF acts as repository & UnitOfWork. now i want to use Repository not to implement simple CRUD operations, but to be able to re-use my code accross the controller classes , mainly to reuse the methods which have complex queries which join multiple tables. and i will create multiple repos maybe a Repo for each entity type.. and to be able to coordinate between multiple Repos i decide to create a unite of work class so all the Repos operation will be wrapped inside a single transaction ..john Gu
i know that EF can act as a Repo & unitofwork for CRUD operations, but in my case i have somehow complex data access layer which i want to be able to re-use...john Gu
What your looking for is a Service Layer, not an additional rerepository. See github.com/carbonrobot/FullStackEF for examplesCharlie Brown
maybe we are talking about the same thing but in different names ,, what i am trying to achieve is not to call the DBContext object from my controller classes. and to do this from service layer or from repository,, then to coordinate the service layers together by using unit of work... but my question is how i can pass the name db context object between; Abstract Repo + Dedicated Repo + Unit Of Work + Controller. can you adivce ?john Gu

1 Answers

0
votes

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.