using System; using System.CodeDom; using Composite.Data.Validation; using Composite.Functions; using Composite.Data; namespace MyCustomValidator { // Microsoft validation attribute public sealed class MyValidatorAttribute : Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ValueValidatorAttribute { public MyValidatorAttribute(int someValue) { this.SomeValue = someValue; } public int SomeValue { get; set; } protected override Microsoft.Practices.EnterpriseLibrary.Validation.Validator DoCreateValidator(Type targetType) { return new MyValidator(SomeValue); } } // Microsoft validation public sealed class MyValidator : Microsoft.Practices.EnterpriseLibrary.Validation.Validator { public int SomeValue { get; set; } public MyValidator(int someValue) : base("Some message", "someValue") { SomeValue = someValue; } protected override string DefaultMessageTemplate { get { return "Some message"; } } // This method does the actual validation protected override void DoValidate(object objectToValidate, object currentTarget, string key, Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResults validationResults) { int num = (int)objectToValidate; if (num > SomeValue) { LogValidationResult(validationResults, string.Format("Value should not exceed {0}", SomeValue), currentTarget, key); } } } // Composite validation function return type public sealed class MyValidatorPropertyValidatorBuilder : PropertyValidatorBuilder { public MyValidatorPropertyValidatorBuilder(int someValue) { this.SomeValue = someValue; } public int SomeValue { get; set; } public override Attribute GetAttribute() { return new MyValidatorAttribute(this.SomeValue); } public override CodeAttributeDeclaration GetCodeAttributeDeclaration() { CodeAttributeDeclaration codeAttributeDeclaration = new CodeAttributeDeclaration(new CodeTypeReference(typeof(MyValidatorAttribute))); codeAttributeDeclaration.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(this.SomeValue))); return codeAttributeDeclaration; } } // Container class for method based function provider public sealed class MyValidatorContainer { [Function(Description="Validates that an integer should not exceed the specified value")] [FunctionParameter(Name="someValue", Label="MaxValue", Help="Description of Parameter")] public static PropertyValidatorBuilder MyValidator(int someValue) { return new MyValidatorPropertyValidatorBuilder(someValue); } } }