统一 IoC 配置
Unity IoC Config
是否有任何配置告诉 Unity 为每个解析共享同一个实例?
class A:IA {
A(Iref @ref,IB x) {
@ref.Exec(() => x.Foo());
}
...
}
class B:IB {
IC c;
B(IC c) {
c=c
}
void Foo() { c.Bar(); }
}
class C:IC {
C(Iref @ref) // I need Unity to give me the same instance that is resolved in Class A
{
@ref....
}
void Bar() {}
...
}
我试图通过 PerResolveLifetimeManager
注册 Iref
但我遇到的问题是它为所有依赖项共享相同的实例,而不是为每个依赖项共享相同的实例。
问题看起来像这样
Class FooController:Controller
{
FooController(IA test1 , IA test2)
{
// Unity resolves Iref one time so it is shared between test1 and also test2!
// but what i need is Iref instance for each IA, basically a singleton
// scoped just inside IA in order to be shared between Class A and C
}
FooController()
{
//so the only solution that resolve it properly is by forcing the resolve manually,
// the Unity container instantiates Iref for each instance (test1 and test2)
// and of course it's shared between class A and also C for each instance of IA
IA test1 = container.Resolve<IA>();
IA test2 = container.Resolve<IA>();
}
}
我的问题是有没有使用依赖注入来解决这个问题的解决方案?
您只需将 IA 类型注册到 TransientLifetimeManager
和所有其他依赖类型,并使用它们自己的生命周期。这意味着 IA 可以具有单例依赖关系,它们将被解析为单例 (ContainerControlledLifetimeManager
),而 IA 将在每次注入时被解析。
TransientLifetimeManager. For this lifetime manager Unity creates and
returns a new instance of the requested type for each call to the
Resolve or ResolveAll method. This lifetime manager is used by default
for all types registered using the RegisterType, method unless you
specify a different lifetime manager.
ContainerControlledLifetimeManager which registers an existing object
as a singleton instance. For this lifetime manager Unity returns the
same instance of the registered type or object each time you call the
Resolve or ResolveAll method or when the dependency mechanism injects
instances into other classes. This lifetime manager effectively
implements a singleton behavior for objects. Unity uses this lifetime
manager by default for the RegisterInstance method if you do not
specify a different lifetime manager. If you want singleton behavior
for an object that Unity will create when you specify a type mapping
in configuration or when you use the RegisterType method, you must
explicitly specify this lifetime manager. If you registered a type
mapping using configuration or using the RegisterType method, Unity
creates a new instance of the registered type during the first call to
the Resolve or ResolveAll method or when the dependency mechanism
injects instances into other classes. Subsequent requests return the
same instance.
在此处阅读更多内容。
是否有任何配置告诉 Unity 为每个解析共享同一个实例?
class A:IA {
A(Iref @ref,IB x) {
@ref.Exec(() => x.Foo());
}
...
}
class B:IB {
IC c;
B(IC c) {
c=c
}
void Foo() { c.Bar(); }
}
class C:IC {
C(Iref @ref) // I need Unity to give me the same instance that is resolved in Class A
{
@ref....
}
void Bar() {}
...
}
我试图通过 PerResolveLifetimeManager
注册 Iref
但我遇到的问题是它为所有依赖项共享相同的实例,而不是为每个依赖项共享相同的实例。
问题看起来像这样
Class FooController:Controller
{
FooController(IA test1 , IA test2)
{
// Unity resolves Iref one time so it is shared between test1 and also test2!
// but what i need is Iref instance for each IA, basically a singleton
// scoped just inside IA in order to be shared between Class A and C
}
FooController()
{
//so the only solution that resolve it properly is by forcing the resolve manually,
// the Unity container instantiates Iref for each instance (test1 and test2)
// and of course it's shared between class A and also C for each instance of IA
IA test1 = container.Resolve<IA>();
IA test2 = container.Resolve<IA>();
}
}
我的问题是有没有使用依赖注入来解决这个问题的解决方案?
您只需将 IA 类型注册到 TransientLifetimeManager
和所有其他依赖类型,并使用它们自己的生命周期。这意味着 IA 可以具有单例依赖关系,它们将被解析为单例 (ContainerControlledLifetimeManager
),而 IA 将在每次注入时被解析。
TransientLifetimeManager. For this lifetime manager Unity creates and returns a new instance of the requested type for each call to the Resolve or ResolveAll method. This lifetime manager is used by default for all types registered using the RegisterType, method unless you specify a different lifetime manager.
ContainerControlledLifetimeManager which registers an existing object as a singleton instance. For this lifetime manager Unity returns the same instance of the registered type or object each time you call the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. This lifetime manager effectively implements a singleton behavior for objects. Unity uses this lifetime manager by default for the RegisterInstance method if you do not specify a different lifetime manager. If you want singleton behavior for an object that Unity will create when you specify a type mapping in configuration or when you use the RegisterType method, you must explicitly specify this lifetime manager. If you registered a type mapping using configuration or using the RegisterType method, Unity creates a new instance of the registered type during the first call to the Resolve or ResolveAll method or when the dependency mechanism injects instances into other classes. Subsequent requests return the same instance.
在此处阅读更多内容。