Unity 2.0 IOC Configuration about Generic class

不想你离开。 提交于 2019-12-22 11:19:13

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/7953635/unity-2-0-ioc-configuration-about-generic-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!