[英]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 :- 我正在使用Entity Framework 6的asp.net vmc5 Web应用程序上工作。现在,我正在尝试使这些工作正常:-
Define a generic repository. 定义通用存储库。
For each DBSet type to create a dedicated repository which will be derived from the generic repository. 为每种DBSet类型创建一个专用存储库,该存储库将从通用存储库派生。
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. 使用UnitOfwork类,以便调用多个存储库类将导致生成单个事务。
I have a DbSet
of type SkillType
. 我有一个
DbSet
类型的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:- 以下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:- 最后,我创建了以下UnitOfWork类:
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. 我不能为SkillManagmentRepositry类定义两个派生类。
Also I am getting this error inside the SkillTypeRepository:- SkillManagement.DAL.GenericRepository' does not contain a constructor that takes 0 arguments 我也在SkillTypeRepository内部收到此错误:-SkillManagement.DAL.GenericRepository'不包含带有0个参数的构造函数
Rewrite SkillTypeRepository
to this: 将
SkillTypeRepository
重写为此:
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. 如我的评论中所述,您的
SkillTypeRepository
没有构造函数,但GenericRepository
却有,因为基类具有构造函数,您需要在派生类中提供一个构造函数并调用:base(params)
,以了解更多详细信息。
You can then simply call base.context
to obtain a reference to the SkillManagementEntities
. 然后,您可以简单地调用
base.context
以获得对SkillManagementEntities
的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.