开发者

Validation & Validation Rules for Web Services

I have experienced the following scenario in most web service apps I've worked on:

  1. We create a Domain Model (in C#). This is the heart of the application. The Domain Model contains business and validation rules, that determine under what conditions an entity is valid or not.
  2. We create a "web service" layer (in C#/WCF). This layer defines DTO-like objects, that are exposed by the web services. The DTO-like objects are sliced and assembled from pieces of the domain entities, usually in a coarse grained way.
  3. On the web client (JavaScript & HTML) the validation rules are duplicated in a different format, usually some form of JavaScript validation.

How can the validation rules attached to the pieces of domain entities, be exposed to the client, through the web services? The reason for this is that the domain model's validation rules should be defined once, and then be available to consumer clients in some format throughout the rest of the system.

The only solution I've come up with so far is to make the validation rules from the domain, available in some form of metadata, defined in XML, JSON, or similar. The big problem with this is that schema between the service layer's DTOs and the domain entities are different, and therefore the domain's validation rules can't be directly sent to the web client - the client works with a different schema, and the domain model.

Therefore my question is: What is the app开发者_C百科roach requiring the least amount of manual and duplicated code, that maps between the different schemas and layers in the application, but allow everything to interpret the validation rules?


There are two types of Business Rules:

Application Specific Rules

Domain Specific Rules.

How you divide rules between the two is another story, but there is no reason why you cannot have duplicated checks.

For example I use a ViewModel in my Web Application which is decorated with Validation Attributes facilitating Unobstrusive Client-Side Validation through jQuery.

I use AutoMapper to convert the Domain Object to the ViewModel and vice-versa.

Having the separate ViewModel allows me to extend the Domain Validation of mandatory fields, with some Application specific checks (where this is possible through Validation Attributes).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜