What format is data in when passed to a domain layer for validations

五迷三道 提交于 2019-12-23 04:55:20

问题


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:

Product Name: Product ABC

Product Code: 1234

Description: a description

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;}
}

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.

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;

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

Thanks in advance.

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

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(ProductName, ProductCode, Description);

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.


回答1:


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

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

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.

So this is close:

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

But even better would be

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?

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).



来源:https://stackoverflow.com/questions/35608067/what-format-is-data-in-when-passed-to-a-domain-layer-for-validations

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