依赖反转,Unity [英] dependency Inversion, Unity

查看:58
本文介绍了依赖反转,Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个C#应用程序(不是asp.net或silverlight),它具有一个主类和其他几个类.我想使用MS Unity Application模块2.0引入依赖关系反转
应用程序的结构是这样的:在主类中,创建了另外两个类的对象并使用了这些类的功能.这两个类依次使用了其余类的对象和功能.
为此,我添加了一个统一容器并注册了2个类的类型:
像这样的东西

hi all,
i have a C# application (this is not asp.net or silverlight ) which has a main class and several other classes. I wanted to introduce dependency inversion using MS Unity Application block 2.0
the structure of the app is such that in the main class, objects of 2 other classes were created and functions of these classes were used.These two classes in turn used the objects and functions of the rest of the classes.
So to do this, i added a unity container and registered the types for the 2 classes:
something like this

_unityContainer = new UnityContainer();
_unityContainer.RegisterType<Class1>(new InjectionConstructor(var1,var2));
_unityContainer.RegisterType<Class2>(new InjectionConstructor(var3));



因此,为了正确实现依赖关系反转,是否有必要在此容器中注册其余类的类型?
(由于该主类未直接使用其余类中的任何对象/函数.
)



So in order to implement dependency inversion properly,is it necessary to register the types of the rest of the classes also with this container ?
(As this main class is not using any object/functions from rest of the classes directly.
)

推荐答案

这与对象生存期有关.和单元测试.这是一篇可以为您阐明该主题的文章.
http://martinfowler.com/articles/injection.html [
It''s all got to do with object lifetime. and unit testing. Here''s an article that can shed some light on the subject for you.
http://martinfowler.com/articles/injection.html[^]


这篇关于依赖反转,Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆