通过多层架构中的层传递业务实体 [英] Passing business entities through layers in multi layer architecture

查看:50
本文介绍了通过多层架构中的层传递业务实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在开发一个利用多层架构的项目,如应用架构指南 2.0 中所述,其中 5层(DAL、BLL、Facade、表示层和公共层).
在这里,我们有一个业务逻辑层,它由业务组件和业务实体(它们是使用 O/R 映射器生成的实体)组成,通常我们在表示层中需要这些实体来绑定和向用户呈现数据,因此我们对这些实体进行冒泡通过其他层到达表示层.

现在的问题是:
这是一个正确的方法吗?(根据我的定义,如果我们应该共享这些,我们应该将它们放在公共层中,以便我们可以在所有层中使用它们).我们不应该把这个实体移到公共层吗?或者我们应该定义像数据传输对象(DTO)这样的东西并将它们传递给层(这当然看起来是多余的).

如有任何澄清,我们将不胜感激.

解决方案

适当分层的应用程序通常会使用 DTO 来防止业务实体被掺假和扭曲以适应其他层的需求等原因.

但是,在非常小的应用程序中,您可以免去 DTO 映射的负担,并将业务实体一直传递到 UI.您可以将它们保留在 BLL 中,如果所有层都引用它,这并不引人注目.无论如何,它不再是一个真正的分层应用程序,因为您将架构压缩成一个大层.

Mark Seemann 有一篇关于这个问题的有趣博文.>

Currently I'm working on a project exploiting multi layer architecture as described in Application Architecture Guide 2.0 with 5 layers(DAL, BLL, Facade, Presentation Layer and Common Layer).
Here we have a Business Logic Layer which consists of Business Components and Business Entities(which are entities generated using an O/R Mapper), regularly we need this entities in our presentation layer for binding and presenting data to the user so we bubble this entities up to the Presentation Layer through other layers.

Now the question is:
Is it a right approach? (As I know by definition if we are supposed to share these we should place them in Common Layer so we can use them in all layers). Shouldn't we move this entities to common layer? or should we define something like Data Transfer Objects(DTOs) and pass them through the layers(which of course seems redundant).

Any clarification would be appreciated.

解决方案

A properly layered application will generally use DTO's to prevent business entities from being adulterated and distorted to suit the needs of other layers, among other reasons.

However, in very small apps you could spare yourself the burden of DTO mapping and have the business entities passed all the way to the UI. You can keep them in the BLL, it's not dramatic if all layers have a reference to it. It is not really a layered application any more anyway since you sort of crunched the architecture into one big layer.

Mark Seemann has an interesting blog post on this problem.

这篇关于通过多层架构中的层传递业务实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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