注册null作为实例,在Unity容器 [英] Register null as instance in Unity container
本文介绍了注册null作为实例,在Unity容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可选的依赖的资源库类:
I have a repository class with optional dependency:
class MyRepository : BaseRepository, IMyRepository
{
public MyRepository(IDataContext dataContext, ICacheProvider cacheProvider = null)
: base(dataContext, cacheProvider)
{}
// …
}
中的cacheProvider参数作为战略储备库的存在。 我想这样的设置统一的容器:
The existence of cacheProvider parameter acts as strategy for the repository. I want setup Unity container like this:
Container.RegisterType<IDataContext, MyDataContext>(new PerResolveLifetimeManager(), new InjectionConstructor())
.RegisterInstance<ICacheProvider>(null) // ???
.RegisterType<IMyRepository, MyRepository>();
即。没有指出特定的InjectionConstructor一个参数MyRepository,但使用默认构造函数,使用null作为的的cacheProvider 的参数。
有没有办法做到这一点?
Is there any way to do this?
推荐答案
在 .RegisterType&LT; IMyRepository,MyRepository&GT;()
调用,指定<一href="http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.injectionconstructor%28PandP.20%29.aspx">InjectionConstructor与<一个href="http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.optionalparameter%28PandP.20%29.aspx">OptionalParameter,在
.RegisterType<IMyRepository, MyRepository>(new InjectionConstructor(
new ResolvedParameter<IDataContext>(),
new OptionalParameter<ICacheProvider>()));
这篇关于注册null作为实例,在Unity容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文