Validation Framework

At the heart of the validation framework is a class called ValidationEngine. When validation needs to be done, this class is called in order to perform the validation and log the possible validation errors. The ValidationEngine class is located inside the Microsoft.Practices.Modeling.CodeGeneration assembly. The Validation Framework is invoked when a user uses the context menu on a model to “Validate” or “Validate all”
The ValidationEngine makes use of Enterprise Library’s Validation Application Block to specify how validation should be done for each type of element in a model. This allows consumers of this framework to specify additional validation rules using Enterprise Libraries configuration file.
The Enterprise Library configuration file that contains these validation rules is called “ruleset.config” and located in the Service Factory Guidance Package. After building this project, the ruleset.config is copied to the projects output directory and can be consumed by the Validation Framework.

Validating a ModelElement

Validating a ModelElement should be done by calling the ValidationEngine.Validate() method, with following arguments:
  • ValidationElementState state. ValidationElementState.FirstElement if this is a call from outside the ValidationEngine. If a call is made to validate a connected element, from within the ValidationEngine ValidationElementState.LinkedElement should be passed.
  • ValidationContext context. An instance of ValidationContext that should be created for a call from outside the ValidationEngine. This ValidationContext should be re-used within the ValidationEngine to validate connected elements.
  • ModelElement currentElement. The Model Element that should be validated by the ValidationEngine.
  • string ruleSet. The name of the RuleSet that should be contained in the Validation Application Block configuration. This RuleSet allows the ValidationEngine to validate a specific element using different validation logic, depending on the type of validation that should be performed. The RuleSet that contains the validation logic used by the Services Factory is Menu.

After calling the ValidationEngine.Validate method, the validation errors are written to Visual Studio’s error list and can be accessed from within the ValidationContext’s CurrentViolations property.
 ModelElement element;
 ValidationContext context = new ValidationContext(ValidationCategories.Menu, element);
 
 ValidationEngine.Validate(
     ValidationElementState.FirstElement,
     context,
     element,
     "Menu");
 
 if (context.CurrentViolations.Count == 0)
 {
 //no validation errors found
 } 
else
 {
 //validation errors where found and logged to Visual Studio's Error List.
 }

Specifying How Validation Should Be Performed

The validation logic for the Service Factory models is contained in the ruleset.config –file and described in Enterprise Libraries configuration format.
This configuration file should specify an entry for each type of Model Element that should be validated. For instance; validation rules that apply on a ServiceContract element should be listed under its respective type.
The Services Factory comes with a couple of validators of its own, which are contained in the ServiceFactory.Validation assembly. These Validators are:

ElementObjectValidator

This Validator can be specified to validate another instance of ModelElement which should be included in validation.
To use this Validator, two attributes can be declared in configuration:
  • targetRuleset. The RuleSet that should be used to validate the Model Element. Typically the same RuleSet as this rule is contained in.
  • fileConfigurationSource. The path to the configuration file that contains the validation configuration to validate the Model Element.


This attribute defaults to the “ruleset.config” file in the Guidance package’s registration directory.

CrossModelReferenceValidator

The CrossModelReferenceValidator can be used to validate a reference to a ModelElement in an external model (such as a reference to the SeviceContract Model to the DataContract Model). For this Validator two optional attributes can be specified in configuration:
  • elementNameProperty. The name of the property on the target element that is used to determine whether this element can be uniquely identified, from within the element it is referred from. This attribute defaults to “Name”, if no value is specified.
  • validateReferencedElement. true if validation should continue on the referenced element, otherwise false. This attribute defaults to false, if no value is specified in configuration.

UniqueNamedElementCollectionValidator<TModelElement>

This Validator, as well as its Validators that derive from this class can be used to validate uniqueness of a ModelElement’s within a collection. For example, the LinkedOperationsElementCollectionValidator validates whether a ServiceContract does not contain two OperationContract’s with the same name.
This Validator supports one optional attribute in its configuration:
  • collectionElementUniqueIdProperty. The name of the property used to uniquely identify the element. This attribute defaults to Name, if no value is specified.

IdentifierValidator

The IdentifierValidator can determine whether a string (such as the name of an element) can be used as an indentifier within code (such as the name of a class).
For this Validator the one optional attribute can be specified in configuration:
  • language. The name of the CodeDom provider that should be used to validate the string. For the languages C# and Visual Basic .NET you can use: “CSharp” and “VisualBasic”. This attribute defaults to “CSharp”, if no value is specified.

NamespaceValidator

The NamespaceValidator can be used to determine whether a string can be used as a valid namespace name. For this Validator, the one optional attribute can be specified in configuration:
  • language. The name of the CodeDom provider that should be used to validate the string. For the languages C# and Visual Basic .NET you can use: “CSharp” and “VisualBasic”. This attribute defaults to “CSharp”, if no value is specified.

ProjectItemIsUniqueValidator

The ProjectItemIsUniqueValidator can be used to determine whether a model project already contains a projectItem by this name. This Validator can be used when programmatically creating new models within a model project. This Validator cannot be used from configuration.

FileExistsValidator

The FileExistsValidator can determine whether a given string refers to an existing file on disk. This Validator cannot be used from configuration.

FileExtensionValidator

The FileExtensionValidator can determine whether a given string represents a filepath that ends with a specified extension. This Validator cannot be used from configuration.

FileNameValidator

The FileNameValidator can determine whether a given string represents a valid filepath. This Validator cannot be used from configuration.

LocationValidator

The LocationValidator determines whether a string would be either a valid Uri or a filepath that points at an existing file. This Validator cannot be used from configuration.

NonEmptyStringValidator

The NonEmptyStringValidator can determine whether a string is non-empty. This Validator cannot be used from configuration.

Custom Validators

When referring to Validators other than those contained in the Microsoft.EnterpriseLibrary.Validation or ServiceFactory.Validation assemblies, the assembly that contains the validator should be deployed to the Global Assembly Cache.

Last edited Jun 13, 2007 at 1:35 AM by ndelgado, version 1

Comments

No comments yet.