关于泛型类的Unity 2.0 IOC配置

时间:2011-10-31 12:21:34

标签: c# .net generics inversion-of-control unity-container

我想要一些Repository类扩展一个通用泛型类来执行一些常见的操作,问题是:如何在配置文件中配置UserExRepository类型

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...
}

1 个答案:

答案 0 :(得分:1)

您可以将绑定泛型配置为泛型,泛型到非泛型,非泛型到泛型,

<unity>
    <containers>
        <container>
            <types>
                 <type type="Repository`1[[User]]" mapTo="UserExRepository ">
            </types>
        </container>
    </containers>
</unity>

但您无法配置通用约束。