Microsoft 企业库中 Unity 应用程序块的用途?

发布于 2024-08-10 23:26:34 字数 75 浏览 7 评论 0原文

有人可以向我解释一下 Unity 应用程序块的用途吗?我尝试查看文档,但它都非常抽象。

Unity 块有哪些实际用途?

Can someone explain to me what is the purpose of the Unity Application Block? I tried looking through the documentation but its all very abstract.

What are some practical uses for the Unity block?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2024-08-17 23:26:34

控制反转

快速总结(本主题有更多阅读内容,我强烈建议阅读更多内容)...

来自企业模式和实践团队的 Microsoft Unity 是一个控制反转容器项目,或者简称国际奥委会。就像 Castle Windsor、StructureMap 等。这种类型的开发用 Lamen 的术语来说也称为“松散耦合组件”。

IoC 包含对象的依赖注入模式,在该模式中,您依赖外部组件来连接对象内的依赖关系。

例如,您不是访问静态管理器(这几乎不可能进行单元测试),而是创建一个依赖外部依赖项进行操作的对象。让我们以一个 Post 服务为例,您希望在其中访问数据库以获取 Post。

public class PostService : IPostService
{
  private IPostRepository _postRepo;

  public PostService(IPostRepository postRepo)
  {
    _postRepo = postRepo;
  }

  public IList<Post> GetPosts()
  {
    return _postRepo.GetAllPosts().ToList();
  }
}

此 PostService 对象现在对 IPostRepository 具有外部依赖关系。请注意,如何不使用具体对象和静态管理器类?相反,您拥有一个简单接口的松耦合 - 它使您能够连接实现 IPostRepository 的所有不同类型的具体类。

Microsoft Unity 的目的是自动为您连接 IPostRepository。所以你永远不必担心做什么:

// you never have to do this with Unity
IPostRepository repo = new PostRepository();
IPostService service = new PostService(repo); // dependency injection
IList<Post> posts = service.GetPosts();

上面显示了你必须实现两个具体类,PostRepository() 和 PostService()。这将您的应用程序与需求/要求这些确切的实例紧密耦合,并且使单元测试变得非常困难。

相反,您可以在端点中使用 Unity(MVC 中的控制器,或 ASPX 页面中的代码隐藏):

IUnityContainer ioc = new UnityContainer();
IPostService postService = ioc.Resolve<IPostService>();
IList<Post> posts = postService.GetPosts();

请注意,此示例中没有使用任何具体内容(显然,除了 UnityContainer 和 Post)!没有具体的服务,也没有存储库。这是最好的松耦合。

这才是真正的亮点...

Unity(或任何 IoC 容器框架!)将检查 IPostService 是否有任何依赖项。它将发现它需要(依赖于)IPostRepository 的实例。因此,Unity 将进入其对象映射并查找第一个实现向容器注册的 IPostRepository 的对象,然后返回它(即 SqlPostRepository 实例)。这就是 IoC 框架背后的真正力量——检查服务并自动连接任何依赖项的能力。

我需要完成关于 UNity、Castle 和 StructureMap 比较的博客文章。实际上,我更喜欢温莎城堡,因为它的配置文件选项和个人的可扩展性点。

Inversion of Control

A quick summation (lot more reading is available this topic, and I highly suggest reading more)...

Microsoft's Unity from the Enterprise Patterns and Practices team is an Inversion of Control container project, or IoC for short. Just like Castle Windsor, StructureMap, etc. This type of development is also referred in lamen's terms as Loosely Coupling your components.

IoC includes a pattern for Dependency Injection of your objects, in which you rely on an external component to wire up the dependencies within your objects.

For example, instead of accessing static managers (which are near impossible to unit test), you create an object that relies on an external dependency to act upon. Let's take a Post service in which you want to access the DB to get a Post.

public class PostService : IPostService
{
  private IPostRepository _postRepo;

  public PostService(IPostRepository postRepo)
  {
    _postRepo = postRepo;
  }

  public IList<Post> GetPosts()
  {
    return _postRepo.GetAllPosts().ToList();
  }
}

This PostService object now has an external dependency on IPostRepository. Notice how no concretes and no static manager classes are used? Instead, you have a loose-coupling of a simple Interface - which gives you the power of wiring up all different kinds of concrete classes that implement IPostRepository.

Microsoft Unity's purpose is to wire up that IPostRepository for you, automatically. So you never have to worry about doing:

// you never have to do this with Unity
IPostRepository repo = new PostRepository();
IPostService service = new PostService(repo); // dependency injection
IList<Post> posts = service.GetPosts();

The above shows where you have to implement two concrete classes, PostRepository() and PostService(). That is tightly-coupling your application to demand/require those exact instances, and leaves it very difficult to unit test.

Instead, you would use Unity in your end point (The controller in MVC, or code behind in ASPX pages):

IUnityContainer ioc = new UnityContainer();
IPostService postService = ioc.Resolve<IPostService>();
IList<Post> posts = postService.GetPosts();

Notice that there are no concretes used in this example (except UnityContainer and Post, obviously)! No concretes of the services, and no repository. That is loosely-coupling at its finest.

Here's the real kicker...

Unity (or any IoC container framework out there!) will inspect IPostService for any dependencies. It will see that it wants (depends) on an instance of IPostRepository. So, Unity will go into it's object map and look for the first object that implements IPostRepository that was registered with the container, and return it (i.e. a SqlPostRepository instance). That is the real power behind IoC frameworks - the power to inspect services and wire up any of the dependencies automatically.

I need to finish my blog post about the comparisons of UNity vs Castle vs StructureMap. I actually prefer Castle Windsor due to its configuration file options, and extensibility points personally.

绝不放开 2024-08-17 23:26:34

Unity 应用程序块用于依赖注入。我认为 DI 最好的简单定义来自

当你自己从冰箱里拿东西时,可能会引起问题。你可能会把门开着,你可能会得到一些妈妈或爸爸不希望你拥有的东西。您甚至可能正在寻找我们没有或已经过期的东西。

您应该做的是陈述需求,“我需要在午餐时喝点东西”,然后我们会确保您坐下来吃饭时有东西。

举个例子,

IUnityContainer container = new UnityContainer();
ILunch lunch = container.Resolve<ILunch>();
Console.WriteLine(lunch.Drink); 

这输出“Lemonade”,因为我们将 Drink 定义为依赖项。

public class ILunch {
    [Dependency]
    public IDrink Drink { get; set; }
} 

就实用性而言,当您对其他对象有依赖关系并且不希望开发人员必须手动设置它们时,依赖注入确实很棒。就是这么简单。它也非常适合嘲笑。我能想到的最常用的例子是模拟数据层。有时数据库还没有准备好,所以我没有停止开发,而是有一个返回假数据的假层。数据对象通过 DI 访问,并且可以配置为返回访问假层或真实层的对象。在此示例中,我几乎使用 DI 容器作为可配置工厂类。有关 Unity 的更多信息,MSDN 有一些很棒的资源 http://msdn.microsoft.com /en-us/library/cc468366.aspx

其他好的 DI 框架包括 Spring.NETNinject

The Unity Application Block is used for dependency injection. I think the best simple definition for DI is from this question

When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn't want you to have. You might even be looking for something we don't even have or which has expired.

What you should be doing is stating a need, "I need something to drink with lunch," and then we will make sure you have something when you sit down to eat.

So for an example,

IUnityContainer container = new UnityContainer();
ILunch lunch = container.Resolve<ILunch>();
Console.WriteLine(lunch.Drink); 

This Outputs "Lemonade" because we defined Drink as a Dependency.

public class ILunch {
    [Dependency]
    public IDrink Drink { get; set; }
} 

As far as practicality goes, Dependency Injection is really great when you have Dependencies on other objects and you don't want the developer to have to manually set them. It is that simple. It is also great for mocking. The most used example I can think of that I use is mocking a data layer. Sometimes the database isn't ready so instead of stopping development I have a fake layer that returns fake data. The data object is accessed via DI and can be configured to return objects which access the fake layer or the real layer. In this example I am almost using the DI Container as a configurable factory class. For more on Unity MSDN has some great resources http://msdn.microsoft.com/en-us/library/cc468366.aspx.

Other good DI Frameworks include Spring.NET and Ninject

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文