小编v0i*_*0id的帖子

在Unity容器中将null注册为实例

我有一个带有可选依赖项的存储库类:

class MyRepository : BaseRepository, IMyRepository
{
    public MyRepository(IDataContext dataContext, ICacheProvider cacheProvider = null)
        : base(dataContext, cacheProvider)
    {}

    // …
}
Run Code Online (Sandbox Code Playgroud)

cacheProvider参数的存在充当存储库的策略.我想要像这样设置Unity容器:

Container.RegisterType<IDataContext, MyDataContext>(new PerResolveLifetimeManager(), new InjectionConstructor())
         .RegisterInstance<ICacheProvider>(null) // ???
         .RegisterType<IMyRepository, MyRepository>();
Run Code Online (Sandbox Code Playgroud)

即没有为MyRepository指出具有一个参数的特定InjectionConstructor,而是使用带有null的默认构造函数作为cacheProvider参数.

有没有办法做到这一点?

.net c# unity-container

17
推荐指数
3
解决办法
7192
查看次数

标签 统计

.net ×1

c# ×1

unity-container ×1