在洋葱架构依赖解析 [英] Dependency Resolution in Onion Architecture

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

问题描述

借助洋葱建筑是构建应用程序的方法来保持关注和松散分离耦合(例如项目位置: HTTP://onionarch.$c$cplex.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(我们都知道是pretty的慢)。我想知道,如果有人可以或许eloborate如何整合不同的DI工具(如SimpleInjector,统一或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.

这是关键,所有的层只有1依赖性(包括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,统一,   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.

正如你所说的,只有依赖解析层应引用所有其他层,它是你的洋葱建筑的最外层。那么,这样做,我创建了一个名为的引导程序项目的。这是我引用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());
        }
    }
}

有趣的行是:

The interesting line is:

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

据该块包的描述:

According to the nugget package’s description:

WebActivator是的NuGet包,允许其他包执行   一些启动code。在Web应用程序。

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

pretty的酷,是吧?你必须把在地方最近的是要确保的引导程序的项目组件将被推到 / bin中 Web应用程序的文件夹(易使用设置后生成操作或OutputTo块包)。这将避免你引用的引导程序的项目在你的MVC项目,并打破了洋葱架构原则。

Pretty cool, huh? The latest 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天全站免登陆