这可能以前被问过,但我不能解决它。也许如果我能得到正确的头衔,我可以把它放在一边。如何设置一个类型的约束,所以它必须是另一种通用类型的类型
我有这个通用库接口:
public interface IRepository<TEntity>
{
TEntity Resolve<TEntity>(); // dummy function, just to get the idea
}
我也有工作的通用单位,这是能够解决通用的存储库:
public interface IUnitOfWork
{
IRepository<TEntity> GetGenericRepository<TEntity>() where TEntity : class;
}
到目前为止好。
但随着现实生活的继续,我想创建一个自定义存储库,并具有一些特定的功能。所以我在想:继承;像这样:
public class SpecialRepository : IRepository<SomeEntityType>
{
public void SomeSpecialFunction() { };
}
显然,这种类型不能与GetGenericRepository
梅索德所以我想解决:让我们添加一个额外的方法将IUnitOfWork
接口:
public interface IUnitOfWork
{
//same old get generic repository
IRepository<TEntity> GetGenericRepository<TEntity>() where TEntity : class;
//the newly added.
T GetInheretedRepository<T>() where T : class;
}
我希望能够调用工作单位有特殊的仓库,这样的事情:
public test()
{
IUnitOfWork uow = new UnitOfWork();
//I want to make this call with a constraint on TemplateRepo
//to enforce it's type: IRepository<T> (which TemplateRepo is)
var y = uow.GetInheretedRepository<TemplateRepo>();
}
的问题是:如何将T GetInheretedRepository<T>() where T : class;
中的类型T
限制为类型:IRepository<TEntity>
?
我尝试这样:
public interface IUnitOfWork
{
//the newly added.
//error: Only class or interface could be specified as constraint
T GetInheretedRepository<T>() where T : class, IRepository; }
和
public interface IUnitOfWork
{
//the newly added.
//error: type argument missing
T GetInheretedRepository<T>() where T : class, IRepository<>;
}
是不工作。
我可以删除约束作为一个快速修复或可能创建一个继承的工作单元,但然后;问题依然存在。
尽管我将其他答案标记为“答案”,但我正在调整您在文章中描述的模式。我在基于MSMQ的环境中使用过类似的东西,但从未在数据层级使用过。听起来很有希望。 :-) – Stefan