Reputation: 1
I am working on an asp.net vmc5 web application that uses Entity framework 6. Now I am trying to get these working :-
Define a generic repository.
For each DBSet type to create a dedicated repository which will be derived from the generic repository.
Create an interface for each of the dedicated repository.
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
Upvotes: 0
Views: 494
Reputation: 13248
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
.
Upvotes: 1