统一2.0 IOC配置有关通用类 [英] Unity 2.0 IOC Configuration about Generic class
本文介绍了统一2.0 IOC配置有关通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一些仓库类扩展一个共同的通用类来执行一些常用的操作,问题是:如何为配置一个UserExRepository类型在配置文件
公共类UserExRepository:库<使用者>中IUserEx
{
公共UserExRepository(上下文的背景下):基地(上下文){}
}
公共抽象类库< TObject的> :IRepository< TObject的>
其中,TObject的:类
{
受保护的上下文语境= NULL;
公共库(上下文的背景下)
{
上下文=语境;
}
//做一些常见的操作有关的实体,如创建,删除...
}
解决方案
您可以配置结合通用于一般的,普通的非通用的,非通用为通用的,
<团结>
<集装箱>
<集装箱>
<类型>
<类型type =Repository`1 [[用户]]mapTo =UserExRepository>
< /类型>
< /容器>
< /容器>
< /团结和GT;
但不能配置通用的限制。
I want some Repository class extend one common generic class to perform some common operation, problem is: how to config a UserExRepository type in config file.
public class UserExRepository : Repository<User>, IUserEx
{
public UserExRepository(Context context):base(context){ }
}
public abstract class Repository<TObject> : IRepository<TObject>
where TObject : class
{
protected Context Context = null;
public Repository(Context context)
{
Context = context;
}
// do some common operation about entity, like create, delete...
}
解决方案
You can configure binding generic to generic, generic to non-generic, non-generic to generic,
<unity>
<containers>
<container>
<types>
<type type="Repository`1[[User]]" mapTo="UserExRepository ">
</types>
</container>
</containers>
</unity>
but you cannot configure generic constraint.
这篇关于统一2.0 IOC配置有关通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文