服务(应用程序/控制器)和UI层之间的数据类型

时间:2022-02-19 11:33:07

Consider the common layered architecture of:

考虑以下常见的分层架构:

  • UI
  • Service/Application/Controller
  • Domain
  • Persistancy

What should be the types between the Service and the UI layers?

服务和UI层之间的类型应该是什么?

Should the return types of the methods in the Service layer be primitives? Can they be objects from the Domain layer?

The motivation: We are building a forum system. Somewhere in the Service layer there should be a method like "getForums". I am wondering what should be its return type -

Service层中方法的返回类型应该是原语吗?它们可以是Domain层中的对象吗?动机:我们正在建立一个论坛系统。在Service层的某个地方应该有一个像“getForums”这样的方法。我想知道它的返回类型应该是什么 -

  • Should it be some collection of forumID's (primitives) - we would need to issue many more calls to the Service layer in order to present the topic and description of the forum (using its ID).
  • 如果它是forumID(原语)的一些集合 - 我们需要向服务层发出更多的调用,以便呈现论坛的主题和描述(使用其ID)。

  • Should it be some collection of Forum objects (from the domain layer) - we could end up with a UI that has direct access to the Domain layer (sounds really bad to me)
  • 它应该是一些论坛对象的集合(来自域层) - 我们最终可能会有一个直接访问域层的UI(对我来说听起来真的很糟糕)

  • Should it be some collection of specially made objects (representing only the topic, description and ID of a forum)? (sounds like too much code for nothing)
  • 它应该是一些特制的对象集合(仅代表论坛的主题,描述和ID)吗? (听起来像是太多的代码)

1 个解决方案

#1


You could use the Data Transfer Object pattern:

您可以使用数据传输对象模式:

http://www.martinfowler.com/eaaCatalog/dataTransferObject.html

This would be implemented between the UI and the Service layer and can be tailored to the view of the data that the UI requires.

这将在UI和服务层之间实现,并且可以根据UI所需的数据视图进行定制。

#1


You could use the Data Transfer Object pattern:

您可以使用数据传输对象模式:

http://www.martinfowler.com/eaaCatalog/dataTransferObject.html

This would be implemented between the UI and the Service layer and can be tailored to the view of the data that the UI requires.

这将在UI和服务层之间实现,并且可以根据UI所需的数据视图进行定制。