How to: Add Validation to a Domain Model
As the author of a domain-specific language, you can add constraints to your domain models to check for semantic correctness beyond what you can express in the syntax of that language. Users of your domain-specific languages can run validation on the models that they create from those languages and see the results. For more information, see Validation Overview for Domain-Specific Languages.
To add validation to a domain model, you must:
Enable validation for the categories of events that should trigger it.
Create a partial class with a method that implements the constraint logic. An attribute identifies each validation method and the class that contains validation methods.
注意
Regardless of which solution template you use, the overall mechanics will resemble those that this topic outlines.
Enable Validation Framework
To enable the validation framework
In DSL Explorer, expand the Editor node, and then click Validation.
If DSL Explorer is not appearing, open the View menu, point to Other Windows, and click DSL Explorer.
In the Properties window, set the Uses Menu, Uses Open, and Uses Save properties to True.
If the Properties window is not appearing, press F4.
Save DslDefinition.dsl.
Regenerate the code for the solution by clicking Transform All Templates on the Solution Explorer toolbar.
注意
A dialog box might open and warn you not to run text templates from untrusted sources. If you do not want this message to appear again, select the Do not show this message again check box, and click OK. Otherwise, click OK.
Add Partial Classes for Constraints
You must write code for each constraint that you want to implement in a method. You must then add the method to the class of elements to which the constraint applies. You must decide how to separate your constraints into separate methods and to which classes you want to attach them. You should attach each constraint to the class that minimizes the amount of code that you must write. The validation methods will automatically apply to all instances of their classes.
The main definitions of the classes are in the generated code. To add a validation method, you must add a partial class in a separate file. You should keep validation classes in a separate folder (for example, CustomCode) in the Dsl project.
To add a constraint method
In Solution Explorer, right-click the Dsl project, point to Add, and then click New Item.
Under Templates, click Code File.
In Name, type Filename, and click Add.
In the Filename.cs file, add the following references:
using System; Using Microsoft.VisualStudio.Modeling; using System.Collections.ObjectModel; using Microsoft.VisualStudio.Modeling.Validation;
For the domain class for which you want to enforce a constraint, add a partial class of the domain class.
Add a ValidationState custom attribute to this class.
Add a validation method within this class with property Category. (See the following example.)
Example
The following sample code uses a Person class that has a BirthDate property defined. The validation code for this method raises an error if the birth date of the parent is later than the child's birth date.
namespace Fabrikam.FamilyTree.DomainModel
{
[ ValidationState(ValidationState.Enabled) ]
public partial class Person
{
// This attribute identifies the method ValidateParentBirth
//for validation.
[ValidationMethod
( // These values select which events invoke the method.
ValidationCategories.Open |
ValidationCategories.Save |
ValidationCategories.Menu
)
]
// This method is applied to each instance of the
// type in a model.
private void ValidateParentBirth(ValidationContext context)
{
foreach (Person parent in this.Parent)
{
if (this.Birth <= parent.Birth)
{
context.LogError(
"Birth must be after Parent's birth",
"FamilyParentBirthError",
this,
parent);
}
}
}
}
}
You should declare a validation method as private or protected if it can be overridden in a subclass. The LogError and LogWarning methods put messages in the Error List window.
The second parameter (FamilyParentBirthError in the example) should be a unique identifier for the error. The third and subsequent parameters are elements that are highlighted in the model designer when the user clicks the error message.
Compiling the Code
On the Debug menu, click Start Debugging.
Open the Empty.file_extension file.
Right-click anywhere in the model designer, and click Validate All.
The validation error appears in the Error List window.