mehdi.loa
mehdi.loa

Reputation: 575

validation for DTOs and business objects

when using DTOs in client-server architecture you can have some validations on client side that checks the DTO objects. when the DTO is sent back to the server after it converts back to business objects it can be verified for some server side validations. so it seems that here some logical code duplication is exists, for validation of DTOs and business obejcts, how can I remove this code duplication??

Upvotes: 1

Views: 1148

Answers (2)

nein.
nein.

Reputation: 2127

If you have simple validations like "This property is required", or "this int property must be in range x..y" you can use Data Annotations. These can be validated on client and server side with the Validator class. Depending on your frontend, your View can use these as well for checking user inputs.

The Validator.ValidateObject method returns a list of validation results which give you error messages for every failed validation.

Upvotes: 1

Paul Anderson
Paul Anderson

Reputation: 1180

Put the validation and DTO code in a common project that is referenced by both the client and server projects.

Upvotes: 1

Related Questions