c# - Is DTO-Enpoint validation not moving the business logic to a wrong layer? -
in asp.net web api model validation done dataannotations or fluentvalidation framework.
according ms validation done: http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api
this sample above link:
public class product { public int id { get; set; } [required] public string name { get; set; } public decimal price { get; set; } [range(0, 999)] public double weight { get; set; } } microsoft seems use simple samples mix of dto/business model/persistance object... terrible... passed client , database same time.
question: stuff weight must between 0 , 999 not belong service layer instead of application layer?
for me business logic there no database limitation field type (double) sure no dataprovider logic.
stuff weight must between 0 , 999 not belong service layer instead of application layer?
model validation shouldn't domain.
for example, asp.net webapi uses model validation perform http entity validation. valuable validation because means unexpected http request may not reach api controller , nice because avoid executing lot of code before doing actual domain logic.
since data annotations , model validation cross-layer feature, can both validate dtos , domain models using same data annotation attributes , validator , you're not breaking separation of concerns.
for me, dto validation more contract: i won't process request it's data isn't in form i'm expecting it. i'm not goint implement deep validation, @ least receive required data in expected format.
at end of day, need decide right validation rules applied software layers. maybe dtos shouldn't validate weight should between 0 , 999 should check weight equal or greater 0, while domain layer should validate against concrete domain rules/specifications. if domain requires weight should less 999, add validation there. any domain receiving data dto won't receiving negative weights, while domain may validate if it's between 0 , 999.
Comments
Post a Comment