0

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

John John
  • 1
  • 72
  • 238
  • 501
  • 1
    your `SkillTypeRepository` does not have a constructor, but the `GenericRepository` does, also should be `public class SkillTypeRepository : GenericRepository, ISkillTypeRepository` – Ric Jan 14 '15 at 16:16
  • 1
    as the base class has a constructor you need to provide one in the derived class and call `:base(params)`, see here http://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp – Ric Jan 14 '15 at 16:20
  • then what the method definition for the SkillTypeRepository constructor should be? – John John Jan 14 '15 at 16:24

1 Answers1

1

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.

Community
  • 1
  • 1
Ric
  • 12,855
  • 3
  • 30
  • 36
  • so you mean i do not have to define the following inside my SkillTypeRepository "private SkillManagementEntities db = new SkillManagementEntities();" and instead of this i can simple call base.context?? – John John Jan 14 '15 at 16:44
  • 1
    yes, the reason is that within your `GenericRepository`, you define `SkillManagementEntities` already, no need to have it again within `SkillTypeRepository` as this class has access to the base class fields/properties, unless they are marked private. – Ric Jan 14 '15 at 16:45
  • but if i keep my code as is, will this affect having a single transaction when .Save() method is called? – John John Jan 14 '15 at 16:54