English 中文(简体)
域目标建设责任:我是否应在服务层界面中使用域目标?
原标题:Domain object construction responsibility: Should I use domain objects in service layer interface?

在设计服务层时,我是否应该在接口合同中使用我的域名?例如:

公共无效的注册用户( String 用户名、 字符串真实Name)

VS 甚高

公共无效的注册用户用户( 用户用户)

域名对象是用客户代码还是用服务外观构造的?

我使用EJB,我的客户是当地部署的网络应用程序、马绍尔群岛的客户,或许是网络服务客户。

问题回答

从技术上讲,使用这种或那种工具没有问题:通过 XSD 提供的网络服务能够支持原始类型,如字符串和复杂的物体,如用户类。

现在, 如果您的 < code> User 类有 20 个属性, 您只需要 < code> username realName 来注册用户, 那么您最好先使用您的第一种方法, 因为所需的带宽较少, 您并不强迫您的客户构建一个不需要的大型 XML 文档 。

其它情景是您的用户类根据 JAXB 规则生成了一个复杂且高强度的 XML 文档。 这可以为您的客户端生成复杂信息, 并包含复杂的客户执行程序。 如果是这样, 您可以使用更简单的版本的域名类( 可能有一个或两个嵌套级别) 作为 DTO 来简化信件交换 。

在我看来, 服务层一般不应该使用域名 。 域名是处理商业逻辑、 规则和工作流程的单位, 而服务则提供与其连接的界面 。

在设计服务层时,最简单的原则是“服务方法实现了一个单一使用案例”。 (使用情况是一种假设情况,投入和产出都有相当明确的界定)。

在您的样本中 - registerUser (String username, String realName) - 看上去完全不错。 服务会即时接收所有所需的域名对象并启动业务操作 - 同时, 服务客户不知道商业逻辑内部( 例如, 用户对象构建中可能有一些特定内容 ) 。

Passing Domain objects as part of the Service layer is a way much simpler and more object oriented. Working with the user input values and constructing relevant Domain objects - is the responsibility of the Controller layer

此外,服务层描述“a href=”“http://en.wikipedia.org/wiki/Application_programing_interface” Rel=“不跟随 noreferrer”>API ,供终端客户使用。如果服务层开始以通用类型(如字符串、地图等)暴露 API,问题就会开始。

而不是 Dome 对象更有能力从控制层传送到服务层。

根据 DDD (DD ( Domain Development design < a href="http://en.wikipedia.org/wiki/Domain-droad_deign" rel=“nofollow">http://en.wikipedia.org/wiki/Domain-驱动_design ), 将用户对象通过所有服务传递到用户对象似乎无害。 根据 DDD ( DD DD ( Domain Development < a href="http://en.wikipedia.org/wik/wiki/document_deign < a > ),这是一个非常好的做法,您的所有域目标都应在您的项目的所有层次上都可以使用。 从长远看,它将使您对通常在典型的 Java EE 项目(ak anemic 域模型) 中缺少的更多对象进行编码,但唯一的补充建议是您试图在用户类别中保持商业逻辑,并在您服务中保持“ 坚固/ ” 。

In my opinion, it is not suitable to expose domain object from facade layer. Clients that are using the service should not be depended to domain objects. It is better to designing a data contract and expose them from facade layer. They will be used just for transferring data, and that is DTOs. Consider that domain objects are not just DTOs and they can provide some functionless. Also a service in facade layer expose a usage of system and it may includes the collaboration of some parts of multiple domain objects and sometimes there may be not a suitable mapping between the requirements of the method in facade layer and available domain objects. Also DTOs and their structure may require some considerations for network performance reasons. And these considerations usually are not meaningful in designing domain objects.

Should the domain objects be constructed in the client code or behind the service facade?

应该在服务侧创建 < 坚固> 域对象。 客户端只需通过创建对象所需的所有参数即可。

物体构造的一般算法是:

  1. Get parameters
  2. Validate parameters
  3. Pass valid parameters into object constructor to get a valid object
  4. Register a valid constructed object in repository for persistence (if necessary)

如果一个对象在客户端创建并转而服务,那么问题就会增加:

  1. The object from client side may be in the incorrect state (client data can not be trusted)
  2. Object state has to be validated
  3. Validation of an existing object means possible duplication of validation logic, possible loss of encapsulation when private field values are validated or sometimes single responsibility principle violation
  4. Sometimes service has to reconstruct the object using passed object property values as parameters




相关问题
SQL Server - How many users do I *really* need?

I m setting up an application, and I m looking into purchasing a license for SQL Server. My question is pretty simple (though may have a complicated answer...) How many users accounts do I really ...

Object Graph / Persistence Question

I m currently working with an online application that requires multiple screens/steps in order to complete. The application domain is modeled using an object graph. Persistence to and from a database ...

Why does stack get truncated in Exception.StackTrace?

Why does the high part of the stack (in Exception.StackTrace) gets truncated? Let s see a simple example: public void ExternalMethod() { InternalMethod(); } public void InternalMethod() { try ...

ASP.NET MVC and Service Oriented Architecture

I would like to know how do i incorporate a feature like wcf within and MVC application. My current idea of the architecture is as follows: EntityFramework -> ASP.NET MVC (Views) ...