传递到域层进行验证时数据的格式是什么 [英] What format is data in when passed to a domain layer for validations

查看:68
本文介绍了传递到域层进行验证时数据的格式是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将数据从表示层的用户界面传递到应用程序层然后传递到域层进行验证时,我对应该采用哪种形式的数据感到困惑。我正在传递DTO,但听说我不应该这样做。相反,我应该只将基元和标量传递给域层。我不确定如果不使用DTO类结构怎么办。以下是我从UI使用DTO的方式:

I am confused about what form data is supposed to be in when passing data from a User Interface in the Presentation Layer to an Application Layer then the Domain Layer for validations. I am passing in a DTO but I've heard I should not. Rather that I should only pass in primitives and scalars to the Domain Layer. I'm not sure how this is done if not using a DTO class structure. Below is how I am using a DTO from my UI:

我的用户界面在屏幕上可能具有以下值:

My User Interface may have values as follows on screen:


产品名称:ABC产品

Product Name: Product ABC

产品代码:1234

说明:说明

当用户单击提交按钮以将此记录添加到数据库时,我将按以下方式构建DTO:

When the user clicks the submit button to add this record to the database I build a DTO as follows:

public class NewProductDto
{
 public string ProductName  {get;set;}
 public string ProductCode  {get;set;}
 public string Description  {get;set;}
}

我将此DTO传递给应用程序层,然后传递给域层,在该域中,它读取值以验证并创建一个新实例。一个实体。

I pass this DTO to the Application Layer then to the Domain Layer where it reads the values to validate and create a new instance of an entity.

如果我不应该这样做,那么应该如何打包来自UI的值以供应用程序层接收并发送到域层以执行验证和创建新实体?

If I am not supposed to be doing this then how are the values from the UI supposed to be packaged for the Application Layer to receive and send to the Domain Layer to perform validations and creation of new entities?

也许只是一个简单的数据

Maybe just a simple data structure?

struct NewProduct
{
 public string ProductName;
 public string ProductCode;
 public string Description;
}

struct NewProduct aNewProductStructure;

(即CreateNewProduct(aNewProductStructure)而不是CreateNewProduct(aNewProductDto)吗?

(i.e. CreateNewProduct(aNewProductStructure) instead of CreateNewProduct(aNewProductDto) ?

谢谢

----------更新2/24/2016 9:58

---------- Update 2/24/2016 9:58 am

根据最近的信息我忽略了似乎应用程序层应该从UI接收DTO,然后将其转换为片段以传递到域,因此在上面的示例中,应用程序层应该将要创建的新产品传递给域层:

Okay based on recent information I overlooked it appears the Application Layer is supposed to receive the DTO from the UI but then converts it into pieces to pass to the domain. So in my example above the Application Layer should pass the new product to be created as follows to the domain layer:

CreateNewProduct(产品名称,产品代码,描述);

CreateNewProduct(ProductName, ProductCode, Description);

CreateNewProduct的定义:

Definition for CreateNewProduct:

public int CreateNewProduct(string ProductName, string ProductCode, string Description)
{
....
}

基本上,我应该将各个值传递给域。

Basically, I am supposed to pass in the individual values to the Domain.

推荐答案

使用DTO跨过程边界传输数据是一件好事。

Using a DTO to transmit data across a process boundary is a Good Thing.

ng>反腐败组件,它负责确保发送给域的消息格式正确,它本身位于应用程序组件中。

The anti-corruption component, which is responsible for ensuring that the messages to the domain are well formed, itself lives in the application component.

也就是说,应用程序组件采用DTO,并从中创建将被域识别的值类型是正常的。所有数据验证(对值类型的范围检查,字符串解码,空检查等)都在应用程序层中进行。

Which is to say, it is normal for the application component to take the DTO, and create from it value types that will be recognized by the domain. All of the data validation (range checking on value types, decoding of strings, null checks, etc) happens in the application layer.

因此,这很接近:

public int CreateNewProduct(string ProductName, string ProductCode, string Description)
{
    ....
}

但更好的是

public int CreateNewProduct(ProductName productName, ProductCode productCode, Description description)
{
    ....
}

在应用程序层完成所有数据验证后,将域保留为命令与业务规则一致-给定当前状态

With all of the data validation done by the application layer, the domain is left to reconcile the command with the business rules -- given the current state of the domain, is creating a new product with these arguments allowed?

域模型使用它理解的类型描述了对应用程序的任何更改,然后应用程序负责标准化结果(即,建立一个DTO发送b确认给客户,将更改传递给持久性组件等)。

The domain model describes any changes to the application using the types that it understands, and the application is then responsible for normalizing the result (ie, building a DTO to send back to the client, passing changes to the persistence component, etc).

这篇关于传递到域层进行验证时数据的格式是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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