I have implented the idataerrorinfo interface on the baseentity class. thiis interface provides the functionality to offer custom error information that a user interface can bind. controls like the datagridview automatautomatautomatically detect this interface and provide error icons along with descriptions about the error. hand in hand with this, I have also added the ability to attach validation rules to the Entity objects. there are some built-in validators (stringmaxlength, stringrequired, notnull, equaltovalue, lessthanorequaltovalue, etc ). there is also a delegate defined that allows you to provide your own validation methods so that you can make you business rule logic as complex as necessary. to add validation rules to your object, you simply need to override the addvalidationrules method (part of baseentity ). by default, the templates will generate two validation rules for each property (if appropriate): stringmaxlength and notnull. each entity now has a property called validationrules that provides an addrule method to add validation rules. you can add as your different rules to each property as you like. here's the basic addvalidationrules method that is added to the entity class:
Protected {
// Add M validation rules
The baseentity will call any validation rules that were added by the templates. So, how do we add custom validation rules? It's fairly simple (I think...). Let's use the orders class in northwind as an example. Let's say that we wowould like to add the following rules:
- The customer ID (customerid) must be provided.
- The freight amount (freight) must be between $0 and $200.
- The Order Date (orderdate) must not be in the future.
Let's tackle these one at a time.1. to implement rule number one, we will use one of the built-in validation methods: stringrequired. We will use the simplest overload for addrule:
Publicvalidationrulehandler is a delegate defined:
Publictarget is the object that is being validated, while E is a validationruleargs object that contains information about the rule (property to be validate, error description ). to actually add the rule, we use the following syntax:
Validationrules. addrule (validation.
Commonrules. stringrequired, "customerid ");
The validation. commonrules namespace provided des several built-in rules to do common validation tasks.
Delegateboolvalidationrulehandler (object target, validationruleargs e) void addrule (validationrulehandler handler, string propertyname)
2. To implement this rule, we will use two of the built-in validation methods: greaterthanorequaltovalue <t> and lessthanorequaltovalue <t>. These use a sly lightdifferent overload of the addrule method:
Public
Validationrules. addrule (validation. validationrules. addrule (validation. commonrules. lessthanor1_tovalue <decimal?>, New validation. commonrules. comparevalueruleargs <decimal?> ("Freight", 200); 3. for this rule, we will use custom validation method called validateorderdate. the only requirement for a validation method is that is must match the validationrulehandler delegate. here's the code for the custom method:
Privatebool validateorderdate (object target, validation. validationruleargs e) {If (this. orderdate> datetime. today) {e. description = "the order date must not be in the future. "; returnfalse;} returntrue;} There are a couple of things to note here. first, the method needs to return false if the rule is broken or true if the rule is not broken. if the rule is broken, you should set the description Property of the validationruleargs parameter so that the use gets information about the error. here's the complete orders class to implement all of these rules: # region using directives using system; using system. componentmodel; using system. collections; using system. XML. serialization; # endregion namespace nettierstest2.northwind {// <summary> // an object representation of the 'Orders 'table. [No de When found the database] /// </Summary> /// <remarks> // This file is generated once and will never be overwritten. /// </remarks> publicpartialclassorders {// <summary> // adds valim validation rules to this object. /// </Summary> protectedoverrisponid addvalidationrules () {base. addvalidationrules (); // Add custom validation rules validationrules. addrule (validation. commonrules. stringrequ IRED, "customerid"); validationrules. addrule (validation. commonrules. greaterthanor1_tovalue <decimal?>, New validation. commonrules. comparevalueruleargs <decimal?> ("Freight", 0); commonrules. lessthanor1_tovalue <decimal?>, New validation. commonrules. comparevalueruleargs <decimal?> ("Freight", 200); validationrules. addrule (validation. validationrules. addrule (validateorderdate, "orderdate");} privatebool validateorderdate (object target, validation. validationruleargs e) {If (this. orderdate> datetime. today) {e. description = "the order date must not be in the future. "; returnfalse;} returntrue ;}} there are a couple of more properties that are of interest. there is now a p Roperty called isvalid that checks the rules to see if any have been broken. you can also get all of the broken rules through the brokenruleslist property. lastly, I will add a couple of disclosures. first, this is not completely integrated into the framework. you shoshould call isvalid prior to saving the object. second, depending on where Robert goes with the business layer, this cocould change signif Icantly. Third, remember that this is a CTP and things may not be perfect! Send questions and bugs my way... Have a good weekend.
Commonrules. greaterthanorequaltovalue <decimal?>, New validation. commonrules. comparevalueruleargs <decimal?> ("Freight", 0); void addrule (validationrulehandler handler, validationruleargs ARGs) this overload allows you to pass in a custom validationruleargs (or sub-class) parameter to pass in additional information to the validator. heres 'the code to add the rule:
}
Overrisponid addvalidationrules () base. addvalidationrules ();
From: http://blog.csdn.net/ilovemsdn/archive/2007/03/27/1543212.aspx