洋葱建筑中的依赖性解析 [英] Dependency Resolution in Onion Architecture

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

问题描述

洋葱建筑是一种构建应用程序来维护的方式分离关注和松耦合(示例项目在: http://onionarch.codeplex.com/ )。依赖注入/分辨率是此架构的关键方面,因为它用于将所有图层绑定在一起。

The Onion Architecture is a way of structuring applications to maintain separation of concern and loose coupling (example project at: http://onionarch.codeplex.com/). Dependency Injection/Resolution is a key aspect of this architecture, since it is used to tie all the layers together.

上述链接包含一个关于如何构造ASP.NET MVC使用洋葱分层。我真的很喜欢,但大多数这些例子使用Ninject(我们都知道这是很慢)。我想知道如果有人可能会将如何将不同的DI工具(如SimpleInjector,Unity或Autofac)集成到洋葱项目中。

The above link contains an example application on how to structure an ASP.NET MVC using the Onion layering. I really like it, but most of these examples use Ninject (which we all know is pretty slow). I was wondering if someone could perhaps eloborate on how to integrate a different DI tool (like SimpleInjector, Unity or Autofac) into an Onion Project.

所有层都是关键只有一个依赖(包括MVC项目),即核心层。除了依赖关系分辨率层,此图层可以引用所有图层。

It is key that all layers only have 1 dependency (including the MVC project), namely the Core layer. Except for the Dependency Resolution layer, this layer can reference all the layers.

我很难将MVC项目设置为启动项目,使用DI,以及不包括对MVC层中的DI工具的引用。

I'm having a hard time setting the MVC project as a startup project, using DI, and not including a reference to the DI tool in the MVC layer.

推荐答案

您的问题是


如何将不同的DI工具(如SimpleInjector,Unity或
Autofac)集成到洋葱项目中?

"how to integrate a different DI tool (like SimpleInjector, Unity or Autofac) into an Onion Project?"

我使用StructureMap而不是Ninject,其集成方式应该适用于任何其他DI框架。

I’m using StructureMap instead of Ninject, the way it’s integrated should be ok for any other DI framework.

As您说,只有依赖关系分辨率层才能引用所有其他层,它是您的洋葱架构的最外层。那么这样做,我创建了一个名为 BootStrapper 的项目。这是我引用StructureMap程序集的唯一项目。
在此项目的 App_Start 文件夹中,我有一个名为 StructureMapMvc.cs 的文件,如下所示:

As you said, only the Dependency Resolution Layer should reference all the other layers, it’s the outermost layer of your Onion Architecture. Well, to do so, I’ve created a project called BootStrapper. This is the only project where I reference StructureMap assemblies. In the App_Start folder of this project, I have a file named StructureMapMvc.cs which looks like this:

[assembly: WebActivator.PreApplicationStartMethod(typeof(XXXX.BootStrapper.App_Start.StructuremapMvc), "Start")]

namespace XXXX.BootStrapper.App_Start
{
    public static class StructuremapMvc
    {
        public static void Start()
        {
            IContainer container = IoC.Initialize();
            System.Web.Mvc.DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new StructureMapHttpDependencyResolver(container);
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
        }
    }
}

有趣的是: / p>

The interesting line is:

[assembly: WebActivator.PreApplicationStartMethod(typeof(XXXX.BootStrapper.App_Start.StructuremapMvc), "Start")]

根据矿块包的描述:


WebActivator是一个NuGet包,允许其他包在web应用程序中执行
一些启动代码。

WebActivator is a NuGet package that allows other packages to execute some startup code in web apps.

好酷啊最后一件事是确保将 BootStrapper 项目组合推送到您的Web应用程序的 / bin 文件夹(易于设置使用一个后期制作操作或OutputTo块)包)。这将避免您在MVC项目中引用 BootStrapper 项目,并打破洋葱架构原理。

Pretty cool, huh? The last thing you have to put in place is to be sure that the BootStrapper project assembly will be pushed to the /bin folder of your web application (easy to set up using a post build action or OutputTo nugget package). This will avoid you to reference the BootStrapper project in your MVC project and break the Onion Architecture principle.

所以,所有这一切,它完全符合组合根图案,当您的应用程序启动时,模块将组合在一起。

So, with all this in place, it adheres totally with the Composition Root Pattern and when your app will start, modules will be composed together.

希望这有帮助!

这篇关于洋葱建筑中的依赖性解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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