有人可以解释微软统一? [英] Can someone explain Microsoft Unity?

查看:116
本文介绍了有人可以解释微软统一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读MSDN上关于团结的文章(依赖注入,控制反转),但我想我需要简单来说(或者简单的例子)解释说。我熟悉的MVPC模式(我们这里使用的话),但我无法真正掌握这个东西团结着呢,我认为这是我们的应用程序设计的下一步。

I've been reading the articles on MSDN about Unity (Dependency Injection, Inversion of Control), but I think I need it explained in simple terms (or simple examples). I'm familiar with the MVPC pattern (we use it here), but I just can't really grasp this Unity thing yet, and I think it's the next step in our application design.

推荐答案

团结就是一个IoC容器。谷歌StructureMap并尝试一下吧。容易有点神交,我认为,当国际奥委会东西是新的给你。

Unity is just an IoC "container". Google StructureMap and try it out instead. A bit easier to grok, I think, when the IoC stuff is new to you.

基本上,如果你明白的IoC,那么你明白你在做什么是反向当一个对象被创建的控制。

Basically, if you understand IoC then you understand that what you're doing is inverting the control for when an object gets created.

无IOC:

public class MyClass
{
   IMyService _myService; 

   public MyClass()
   {
      _myService = new SomeConcreteService();    
   }
}

使用IoC容器:

public class MyClass
{
   IMyService _myService; 

   public MyClass(IMyService myService)
   {
      _myService = myService;    
   }
}



如果没有国际奥委会,你的类,它依赖于IMyService有到服务的新建立一个具体的版本中使用。这是不好的一些原因(你已经加上你的类的IMyService的一个特定的具体版本,可以不容易进行单元测试,你不能轻易改变它,等等。)

Without IoC, your class that relies on the IMyService has to new-up a concrete version of the service to use. And that is bad for a number of reasons (you've coupled your class to a specific concrete version of the IMyService, you can't unit test it easily, you can't change it easily, etc.)

使用IoC容器你配置容器来解决这些依赖你。因此,与基于构造注入方案,您只需通过接口IMyService依赖到构造。当你创建你的容器MyClass的,您的容器将解决你的IMyService依赖。

With an IoC container you "configure" the container to resolve those dependencies for you. So with a constructor-based injection scheme, you just pass the interface to the IMyService dependency into the constructor. When you create the MyClass with your container, your container will resolve the IMyService dependency for you.

使用StructureMap,配置容器看起来是这样的:

Using StructureMap, configuring the container looks like this:

StructureMapConfiguration.ForRequestedType<MyClass>().TheDefaultIsConcreteType<MyClass>();
StructureMapConfiguration.ForRequestedType<IMyService>().TheDefaultIsConcreteType<SomeConcreteService>();



那么,你要做的就是告诉容器,当有人要求IMyService,给他们一个在SomeConcreteService副本。而且你也已经指定,当有人要求一个MyClass的,他们得到一个具体的MyClass的。

So what you've done is told the container, "When someone requests the IMyService, give them a copy of the SomeConcreteService." And you've also specified that when someone asks for a MyClass, they get a concrete MyClass.

这是所有IoC容器确实。他们可以做更多,但是这是它的推力 - 他们解决依赖你,所以你不必(你不必使用整个代码的新关键字)。

That's all an IoC container really does. They can do more, but that's the thrust of it - they resolve dependencies for you, so you don't have to (and you don't have to use the "new" keyword throughout your code).

最后一步:当您创建MyClass的,你可以这样做:

Final step: when you create your MyClass, you would do this:

var myClass = ObjectFactory.GetInstance<MyClass>();



希望有所帮助。欢迎发邮件给我。

Hope that helps. Feel free to e-mail me.

这篇关于有人可以解释微软统一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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