配置“深度”与 Unity IoC 的依赖关系
假设我的 MVC 2 项目中有以下类和接口:
存储库:
IRepository1、IRepository2、IRepository3
public class ConcreteRepository1 : IRepository1
{
public ConcreteRepository1()
{
...
}
...
}
public class ConcreteRepository2 : IRepository2
{
public ConcreteRepository2()
{
...
}
...
}
public class ConcreteRepository3 : IRepository3
{
public ConcreteRepository3()
{
...
}
...
}
服务类:
public class Service1
{
private IRepository1 repo1;
private IRepository2 repo2;
public Service1(IRepository1 repo1, IRepository2 repo2)
{
this.repo1 = repo1;
this.repo2 = repo2;
}
...
}
控制器:
public class Controller1 : Controller
{
private Service1 srv1;
private Service2 srv2;
public Controller1(Service1 srv1, Service2 srv2)
{
this.srv1 = srv1;
this.srv2 = srv2;
}
...
}
我有自定义的 ControllerFactory,并且我知道如何将具体存储库与接口联系起来:
IUnityContainer container = new UnityContainer();
container.RegisterType<IRepository1, ConcreteRepository1>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository2, ConcreteRepository2>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository3, ConcreteRepository3>(new TransientLifetimeManager(), new InjectionConstructor());
问题是我应该如何注册服务我的自定义 ControllerFactory 中的“实例和控制器”类型,使统一容器解析整个层次结构 Controller->Service->Repository 并避免在内部调用 Resolve控制器或服务?
谢谢。
Let's say I have the following classes and interfaces in my MVC 2 project:
Repositories:
IRepository1, IRepository2, IRepository3
public class ConcreteRepository1 : IRepository1
{
public ConcreteRepository1()
{
...
}
...
}
public class ConcreteRepository2 : IRepository2
{
public ConcreteRepository2()
{
...
}
...
}
public class ConcreteRepository3 : IRepository3
{
public ConcreteRepository3()
{
...
}
...
}
Service classes:
public class Service1
{
private IRepository1 repo1;
private IRepository2 repo2;
public Service1(IRepository1 repo1, IRepository2 repo2)
{
this.repo1 = repo1;
this.repo2 = repo2;
}
...
}
Controllers:
public class Controller1 : Controller
{
private Service1 srv1;
private Service2 srv2;
public Controller1(Service1 srv1, Service2 srv2)
{
this.srv1 = srv1;
this.srv2 = srv2;
}
...
}
I have custom ControllerFactory and I know how to tie concrete repositories to interfaces:
IUnityContainer container = new UnityContainer();
container.RegisterType<IRepository1, ConcreteRepository1>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository2, ConcreteRepository2>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository3, ConcreteRepository3>(new TransientLifetimeManager(), new InjectionConstructor());
The question is how should I register services' instances and controllers' types inside my custom ControllerFactory to make unity container resolve the whole hierarchy Controller->Service->Repository and avoid calling Resolve inside controllers or services?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你已经注册了
IRepository1-3
,那么你可以获取Service1
实例,只需调用container.Resolve()
就会自动解析依赖项并将创建Controller1
类型的实例。示例:
解析:
编辑:
输出:
因此,当我们没有注册
Service
时(换句话说,它具有默认解析行为),Unity 会构造Service< /code> 默认使用最贪婪的构造函数。当您指定 new InjectionConstructor() 时,这会告诉 Unity 使用无参数构造函数。如果您使用
InjectionConstructorAttribute
标记构造函数(在本例中为public Service()
),也会收到相同的行为。If you have already registered
IRepository1-3
, so you can getService1
instance simply callingCalling
container.Resolve<Controller1>()
will automatically resolve dependencies and will create instance of typeController1
.Sample:
Resolving:
EDIT:
Output:
So, when we haven't registered
Service
(in other words, it has default resolving behavior), Unity constructsService
using greediest constructor by default. When you specifynew InjectionConstructor()
, this tells Unity to use parameterless constructor. The same behavior can be received if you mark constructor (in this casepublic Service()
) withInjectionConstructorAttribute
.当然只是以下内容 -
对于
Service2
也是如此。 IoC容器将为你构造对象surely just the following -
and the same for
Service2
. The IoC container will construct the objects for you