I am working on an asp.net mvc web application & entity framework 6. i want to have these inside my application:-
Generic repository class.
Dedicated repository class for each entity type. Where this dedicated repository will be derived from the generic repository.
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?