如何注册在主容器的类型,但在一个子容器解决? [英] How to register types in the main container, but resolve in a child container?
问题描述
我有一个统一的容器,并使用 RegisterType
注册下列库和实施者使用 ContainerControlledLifetimeManager
。
公共接口IPersonRepository
{
的人GetByID(ObjectSpace的对象空间,INT ID);
}
使用这个模式我可以有多个线程(这是一个Web应用程序)使用在同一时间同一版本库实例尽管每个线程使用不同 ObjectSpace的
(它是一个本地缓存+机构,用于从DB +一个单元获取对象而努力等)。但是,这让我觉得很脏,而不是好样的: - )
我真的很想为:
公共接口IPersonRepository
{
的人GetByID(INT ID);
}
要做到这一点,我将不得不创建一个子容器,并使用 RegisterInstance
注册我的的ObjectSpace
。这只要做工精细,我可以:
- 注册
IPersonRepository
在孩子容器而不是 - 寿命经理更改为
TransientLifetimeManager
我不想做任何。 (1)也只是太多的工作,我想在父容器再没有更多的注册一次。 (2)会工作,但如果有很大的依赖性,那么所有这些必须是瞬时过,这会导致很多正在创建的每个我所需要的人库时间的实例。
所以我的问题是:有没有一个注册在父类型,但有一个容器一生实例解析,并存储在子容器,而不是父容器的方法吗? ?也许有使用定制一生的经理或某事的方式。
我想什么来实现是这样的:
UnityContainer unityContainer =新UnityContainer();
//可能是一个自定义的经理或某事
unityContainer.RegisterType< IPersonRepository,PersonRepository>
(新ContainerControlledLifetimeManager());使用
(VAR childContainer = unityContainer.CreateChildContainer())
{
childContainer.RegisterInstance<&ObjectSpace的GT;(新MyObjectSpace());
// 01解析子容器
变种库= childContainer.Resolve<内部新的实例; IPersonRepository>();
// 02解析相同的实例为01
库= childContainer.Resolve< IPersonRepository>();
}
使用(VAR childContainer = unityContainer.CreateChildContainer())
{
childContainer.RegisterInstance<&ObjectSpace的GT;(新MyObjectSpace());
// 03解析子容器
变种库= childContainer.Resolve<内部新的实例; IPersonRepository>();
// 04解析相同的实例为03
库= childContainer.Resolve< IPersonRepository>(); //解析相同的实例
}
由于要求一新HierarchicalLifetimeManager已被添加到统一的问题,这应该登记的类型时使用。
I have a unity container and use RegisterType
to register the following repository and implementer using ContainerControlledLifetimeManager
.
public interface IPersonRepository
{
Person GetByID(ObjectSpace objectSpace, int id);
}
Using this pattern I am able to have multiple threads (it's a web app) using the same repository instance at the same time despite the fact that each thread is using a different ObjectSpace
(which is a local cache + mechanism for fetching objects from the DB + a unit of work etc). But this makes me feel "dirty", and not the good kind :-)
What I would really like is:
public interface IPersonRepository
{
Person GetByID(int id);
}
To achieve this I would have to create a child container and use RegisterInstance
to register my ObjectSpace
. This would work fine as long as I either:
- Register
IPersonRepository
in the child container instead - Change the lifetime manager to
TransientLifetimeManager
I don't want to do either. (1) Would just be too much work, I want to register once in the parent container and then no more. (2) Would work but if there are a lot of dependencies then all of these would have to be transient too and this would result in a lot of instances being created every time I needed the person repository.
So my question is: Is there a way to register the type in the parent, but to have a container lifetime instance resolved and stored in the child container instead of the parent container? Maybe there is a way using a custom lifetime manager or something?
What I would like to achieve is this:
UnityContainer unityContainer = new UnityContainer();
//Might be a custom manager or something
unityContainer.RegisterType<IPersonRepository, PersonRepository>
(new ContainerControlledLifetimeManager());
using (var childContainer = unityContainer.CreateChildContainer())
{
childContainer.RegisterInstance<ObjectSpace>(new MyObjectSpace());
//01 Resolves a new instance inside the child container
var repository = childContainer.Resolve<IPersonRepository>();
//02 resolves same instance as 01
repository = childContainer.Resolve<IPersonRepository>();
}
using (var childContainer = unityContainer.CreateChildContainer())
{
childContainer.RegisterInstance<ObjectSpace>(new MyObjectSpace());
//03 Resolves a new instance inside the child container
var repository = childContainer.Resolve<IPersonRepository>();
//04 resolves same instance as 03
repository = childContainer.Resolve<IPersonRepository>(); //Resolves the same instance
}
Since asking the question a new HierarchicalLifetimeManager has been added to Unity, this should be used when registering the type.
这篇关于如何注册在主容器的类型,但在一个子容器解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!