【发布时间】:2014-11-19 07:30:00
【问题描述】:
我正在重构我的代码,我首先在我的服务层中删除了对实体框架的引用。该层使用位于我的 DAL 层中的工作单元和存储库(通过接口)。
现在我遇到了一个问题,因为我的基础存储库类如下所示:
public interface IDatabaseFactory<C> : IDisposable
{
C Get();
void Set(string connectionString);
}
public abstract class Repository<C, T> : IRepository<T>
where C : DbContext, IBaseContext
where T : class, IEntity
{
protected readonly IDbSet<T> dbset;
private C dataContext;
protected Repository(IDatabaseFactory<C> databaseFactory)
{
this.DatabaseFactory = databaseFactory;
this.dbset = DataContext.Set<T>();
}
protected IDatabaseFactory<C> DatabaseFactory
{
get;
private set;
}
protected C DataContext
{
get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}
public virtual void Add(T entity)
{
dbset.Add(entity);
}
//etc...
}
我显然需要 C 类型的 DbContext 约束。但是,如果这样做,我会在 dataContext 上收到错误,因为它无法解析 DbContext 中的 C。
我该如何克服这个问题?
编辑
典型的存储库如下所示:
public interface ICustomerTypeRepository : IRepository<CustomerType> { }
public class CustomerTypeRepository : Repository<IBaseContext, CustomerType>, ICustomerTypeRepository
{
public CustomerTypeRepository(IDatabaseFactory<IBaseContext> databaseFactory)
: base(databaseFactory) { }
}
在下面建议的更改之后,我仍然得到相同的错误:
类型“IBaseContext”不能用作泛型类型或方法“Repository”中的类型参数“TContext”。没有从“IBaseContext”到“System.Data.Entity.DbContext”的隐式引用转换。
【问题讨论】:
-
IDatabaseFactory<C>是如何定义的? -
您使用
DataContext仅用于获取DbSet? -
@Vlad 是的。我正在考虑完全删除 IDatabaseFactory。也许我可以在构造函数中传递 IUnitOfWork 。上下文在 UnitOfWork 类中。
标签: c# entity-framework asp.net-web-api