简体   繁体   中英

Getting Generic Repository + Child repository + UnitOfWork inside my asp.net mvc web application

I am working on an asp.net vmc5 web application that uses Entity framework 6. Now I am trying to get these working :-

  1. Define a generic repository.

  2. For each DBSet type to create a dedicated repository which will be derived from the generic repository.

  3. Create an interface for each of the dedicated repository.

  4. Use UnitOfwork class so that calling multiple repository classes will result in a single transaction generated.

I have a DbSet of type SkillType . So I created the following interface:-

namespace SkillManagementp.DAL
{
public interface ISkillTypeRepository {
}

Then the following generic Repository:-

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 following SkillTypeRepository:-

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

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

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

And finally I created the following UnitOfWork class:-

namespace SkillManagement.DAL
{
    public class UnitOfWork : IDisposable
    {
        private SkillManagementEntities db = new SkillManagementEntities();
        private SkillTypeRepository skillTypeRepository;


        public SkillTypeRepository SkillTypeRepository
        {
            get
            {

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



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

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}

But I am getting these errors:-

  • I can not define two derived classes for my SkillManagmentRepositry class.

  • Also I am getting this error inside the SkillTypeRepository:- SkillManagement.DAL.GenericRepository' does not contain a constructor that takes 0 arguments

Rewrite SkillTypeRepository to this:

public class SkillTypeRepository :  GenericRepository<SkillType>, ISkillTypeRepository 
{            
    public SkillTypeRepository() : base(new SkillManagementEntities())
    {

    }
 //rest of code etc
}

As mentioned in my comment, your SkillTypeRepository does not have a constructor, but the GenericRepository does, as the base class has a constructor you need to provide one in the derived class and call :base(params) see here for more details.

You can then simply call base.context to obtain a reference to the SkillManagementEntities .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM