依赖注入VS分层架构 [英] Dependency Injection vs Layered Architecture

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

问题描述

我已经读了很多关于依赖注入和服务定位器(反?)模式 - 在计算器上不少吧(谢谢你们:)。我有一个关于这个模式时,它是一个n层架构中是如何工作的问题。

I've been reading a lot about dependency injection and the service locator (anti-?) pattern - a lot of it on StackOverflow (thanks guys :). I have a question about how this pattern works when it's within a n-layer architecture.

我见过很多的博客文章在那里他们描述注入IDataAccess组件到业务对象。例如。

I've seen a lot of blog posts where they describe injecting a IDataAccess component into the business objects. E.g.

public class Address
{
    IDataAccess _dataAccess;
    public Address(IDataAccess dataAccess)
    {
        this._dataAccess = dataAccess;
    }
}

不过,我是IM pression下,在n层架构,UI层不应该需要有数据访问层的任何知识......甚至不知道有/是/数据接入层!如果DI需要露出IDataAccess接口在BusinessObjects的构造,这则暴露给用户界面,业务层使用引擎盖下的数据访问层的事实 - 东西UI并不需要知道或肯定在乎

However, I was under the impression that in an n-layer architecture, the UI layer should not need to have any knowledge of the data access layer... or even know that there /is/ a data access layer! If DI requires exposing the IDataAccess interface in the constructors of the BusinessObjects, this then exposes to the UI the fact that the Business Layer uses a data access layer under the hood - something the UI doesn't need to know or care about surely?

所以,我的基本问题是:是否DI要求我公开我所有的底层接口,所有的上层,是一个很好的还是坏事

So, my fundamental question is: Does DI require that I expose all my lower layer interfaces to all upper layers and is this a good or a bad thing?

感谢

编辑:为了澄清(经过几次评论),我知道我的业务对象应该是无知的哪些特定的实施其中IDataAccess它使用(因此依赖注入的构造函数),但我认为BO上面层不应该知道,业务对象甚至要求在DAL依赖。

To clarify (after a few comments), I know my business object should be ignorant of the which specific implementation of which IDataAccess it uses (hence the Dependency being injected in the constructor) but I thought that the layers above the BO should not know that the Business Object even requires a dependency on a DAL.

推荐答案

我认为答案是相当简单的。您的底层(界面,BLL,DAL,实体)都只是一堆。它是由客户来决定哪些库来使用,它会增加客户的灵活性。此外,他们是图书馆,因此任何应用程序相关的配置(连接字符串,数据缓存等)位于客户端上。这些配置本身,有时还需要注入和纳入组合物根

I think the answer is rather simple. Your bottom layers (interface, bll, dal, entities) are just a bunch of libraries. It is up to the client to decide which libraries to be used and it will increase client's flexibility. Moreover they are libraries, so any application-related configurations (connection strings, data caching, etc) lies on the client. Those configuration itself, sometimes also need to be injected and included into Composition Root.

不过,如果你想有一个统一逻辑,而不是客户的灵活性,你可以选择网络/应用服务的附加层。

However, if you want to has an uniform logic and not client's flexibility, you can choose web/app services as an additional layer.

1st Layer        Entities

2nd Layer       Interface

3rd Layer       BLL  &  DAL

4th Layer    Web/App Services

5th Layer           UI

这样,你的作文根存在于一个层(第4)。并添加你的UI只需要(如果需要或1)服务引用添加到第四层。然而,这又意味着相同的标志西曼的文章,层次感是值得的映射 。我认为你可以修改应用程序/ Web服务构成根

This way, your composition root exists in one layer (4th). And add your UI just need to add service reference to 4th layer (or 1st if needed). However, this implies the same Mark Seeman's article again, layering is worth the mapping. I assume that you can change the app/web service to Composition Root.

此外,这种(应用程序/ Web服务)的设计有优点/缺点。优点:

Moreover, this (app/web service) design has pros/cons. Pros:


  1. 您的应用程序被封装

  1. Your app is encapsulated

您的应用程序是由应用程序/ Web服务桥接。这是存在保证你的UI不知道数据访问,从而满足您的要求。

Your app is being bridged by app/web services. It is guranteed that your UI don't know the DataAccess, thus fulfill your requirements.

您的应用程序被固定

简单地说,有需要的用户界面来访问应用程序服务是安全方面的巨大收益。

Simply said, having UI need to access app service is a huge gain in security aspect.

访问可移植性

现在您的应用程序可以随处访问。它可以通过第三方应用程序(其它网页)已经不依赖于动态链接库进行连接。

Now your app can be accessed everywhere. It can be connected by 3rd party app (other web) without has relying on dlls.

缺点:


  1. 服务调用期间管理成本

  1. Overhead cost during service call

认证,网络连接等,将Web服务调用过程中导致的开销。我没有经验的性能影响,但它应该是足够的人流量大的应用程序。

Authentication, network connection, etc, will cause overhead during webservice call. I'm inexperienced for the performance impact but it should be enough for high traffic app.

客户端的不灵活性

客户端现在需要使用的服务,而不是正常的对象访问BLL /服务。

Client now need to access BLL/Services by using services instead of normal objects.

客户端的不同类型的更多服务

More Service for Different Type of Client

现在,你需要提供比需要更多的服务。如 WebRequestRetriever MobileRequestRetriever 而不是访问到仅仅 IRequestRetriever 并让组成根线了休息。

Now you need to provide more service than needed. Such as WebRequestRetriever, MobileRequestRetriever instead of accessing to a mere IRequestRetriever and let the composition root wire up the rest.

道歉,如果这个答案boarden话题(完之后才意识到)。

Apologize if this answer boarden the topic (just realized after finished).

这篇关于依赖注入VS分层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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