有人可以解释一下 Microsoft Unity 吗? [英] Can someone explain Microsoft Unity?

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

问题描述

我一直在阅读 MSDN 上关于 Unity(依赖注入、控制反转)的文章,但我想我需要用简单的术语(或简单的例子)来解释它.我熟悉 MVPC 模式(我们在这里使用它),但我还不能真正掌握 Unity 这个东西,我认为这是我们应用程序设计的下一步.

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.

推荐答案

Unity 只是一个 IoC容器".谷歌 StructureMap 并尝试一下.我认为,当 IoC 的东西对你来说是新的时,更容易理解.

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;    
   }
}

如果没有 IoC,依赖 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 容器的全部功能.它们可以做更多的事情,但这就是它的主旨——它们为您解决依赖关系,因此您不必这样做(并且您不必在整个代码中使用new"关键字).

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.

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

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