Dashboard > Castle Contrib > Home > Castle.Facilities.MethodValidator
Castle.Facilities.MethodValidator
Added by Eric Hauser, last edited by Eric Hauser on Sep 18, 2008  (view change)
Labels: 
(None)


Introduction

Castle.Facilities.MethodValidator provides a way to validate method parameters using Castle.Components.Validator.  The facility generates a proxy for the class with the methods needing validated, so the method must be virtual.  Some examples of classes that you might want to use the MethodValidator with are web services and controllers.

Building

To build from source:

The binaries will be placed into the build\bin\ folder.  The only requirement on Castle binaries is that you are using a revision from trunk later than 5303 - which is currently between versions 1.0 RC3 and 1.0.



Usage

To enable the facility, you need to add the facilities to your container configuration:

<castle>
    <facilties>
        <facility id="methodValidator" type="Castle.Facilities.MethodValidator, Castle.Facilities.MethodValidator" />
    <facilities>
</castle>

Now, any components added to the container will be inspected to see if the methods have validators.  The following are some examples of how you can use the validators on methods:

 		public void MethodWithRefParameters(
			[ValidateSet("someValue", "secondValue")] string set,
			[ValidateCreditCard] string creditCard,
			[ValidateNonEmpty] ref string nonEmpty)
		{
		...
		}

		public void MethodWithOutParameters(
			[ValidateDate] string date,
			[ValidateDateTime] string dateTime,
			out string outValue)
		{
		...
		}

		public void MethodWithRefAndOutParameters(
			[ValidateDecimal] string @decimal,
			[ValidateDouble] string @double,
			out string someValue,
			[ValidateEmail] string email,
			[ValidateInteger] ref string integer)
		{
		...
		}

		public void MethodWithParams(
			[ValidateRegExp("abc")] params string[] range0To10)
		{
		...
		}

		public void GenericMethod<T>(
			[ValidateSingle] string single,
			[ValidateRange(0, 10)] int range0To10,
			[ValidateRange(0, 10)] T range0to10)
		{
		...
		}

		public void MethodWithObject(
			[ValidateCreditCard] string creditCard,
			[ValidateObject] ComplexObject @object)
		{
		...
		}

 If a there is a validation failure before the method is invoked, a MethodValidationException will be thrown.  Currently, you can use any of the existing Castle validators except for any of the cross reference validators.  Any parameter can be validated as long as it does not have the ref or out modifier.  If an object is decorated with its own validators, then the [ValidateObject] attribute tells the facilties to validate the entire object.

Future Enhancements

  • Patch for Monorail so this can be used with MR out of the box 
  • Support for cross reference validators
  • Support validation of out and ref parameters


Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators