Last week Fredrik Normén had a couple of nice posts on argument validation using C# extension methods. It got me thinking on a different approach where a method interceptor could handle the validation.
First a short recap on argument validation. I guess everyone have seen or written code with some kind of argument validation, like this for example:
public void Add(string value) { if (string.IsNullOrEmpty(value)) throw new ArgumentException("The value can't be null or empty", "value"); ///... }
To make argument validation easier I have (to a small extent) used a modified version of this design by contract utility class. This utility class lets you write argument validation like this:
public void Add(string value) { Check.Require(value != null, "value should not be null"); ///... Check.Ensure(Count > 0); }
So this got me thinking on how you could handle argument validation using method interception. With a method interceptor you could inspect the function arguments checking for validation attributes. I envisioned code looking like this:
[Ensure(() => this.Count > 0)] public void Register([NotNull] string name, [InRange(5,10)] int value) { ///... }
Getting the ensure post condition to work like that will be impossible since you can only use constant expressions in attribute constructors. This is something I think they should look at for C# 4.0, being able to define lambdas in attribute arguments would make interesting scenarios possible (like this). The NotNull and InRange attributes were very simple to implement:
public abstract class ArgumentValidationAttribute : Attribute { public abstract void Validate(object value, string argumentName); } [AttributeUsage(AttributeTargets.Parameter)] public class NotNullAttribute : ArgumentValidationAttribute { public override void Validate(object value, string argumentName) { if (value == null) { throw new ArgumentNullException(argumentName); } } } [AttributeUsage(AttributeTargets.Parameter)] public class InRangeAttribute : ArgumentValidationAttribute { private int min; private int max; public InRangeAttribute(int min, int max) { this.min = min; this.max = max; } public override void Validate(object value, string argumentName) { int intValue = (int)value; if (intValue < min || intValue > max) { throw new ArgumentOutOfRangeException(argumentName, string.Format("min={0}, max={1}", min, max)); } } }
This is of course the easy part. It is the actual interceptor that is making the magic happen. This implementation is just a proof of concept, and not optimal from a performance stand point.
public class ValidationInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { ParameterInfo[] parameters = invocation.Method.GetParameters(); for (int index = 0; index < parameters.Length; index++) { var paramInfo = parameters[index]; var attributes = paramInfo.GetCustomAttributes(typeof(ArgumentValidationAttribute), false); if (attributes.Length == 0) continue; foreach (ArgumentValidationAttribute attr in attributes) { attr.Validate(invocation.Arguments[index], paramInfo.Name); } } invocation.Proceed(); } }
To make this interceptor more feasible for production you would probably have to add some smart caching mechanism. This interceptor based argument validation is of course somewhat limited in applicability. You have to use a dynamic proxy generator to get the interceptor functionality and only interface and virtual functions can be intercepted. But it is an interesting idea and shows another nice usage scenario for method interception.
The concept of being able to intercept method calls is something that would be great if it was built into a future version of the CLR runtime. That way static and non virtual methods could be intercepted. It is a nice way to implement cross-cutting concerns (like validation). But until then we will have to live with proxy generators, it works nicely for interfaces:
public interface IRegistrationService { void Register([NotNull] string name, [InRange(5, 10)] int value); void Register([NotNullOrEmpty] string name); }
If you specify the attributes on an interface you do not need to duplicate them on the implementation methods! A small update to the interceptor to support validation of the return value would allow something like this:
public interface IUserRepository { [return: NotNull] User GetById([GreaterThanZero] int id); ///... }
The syntax for attributes on return values are kind of funky. If Spec# isn't the future (I hope it is) then maybe something like this can be :)