Passing business entities through layers in multi layer architecture

孤街醉人 提交于 2019-12-13 13:07:53

问题


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.


回答1:


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.




回答2:


Keep your business entities in business layer only. DTO's might seem redundant initially, but as a project grows you'll start noticing the differences between them: DTO's are much flatter, serialisation friendly types and entities have much more complex relations and power, they carry more application logic than the DTO's.

DTO's are plain data and for this reason they are suitable to pass that data between the layers. They don't have to be a direct representation of your entities and for this reason they could improve compatibility when you need to modify your BLL but keep the service contracts.

The only exception might be DAL and BLL interaction, where DAL would deal with entities directly. But even here DTO's could be used to absorb the impact of the ORM.



来源:https://stackoverflow.com/questions/15255562/passing-business-entities-through-layers-in-multi-layer-architecture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!