Skip to content

Instantly share code, notes, and snippets.

@orient-man
Created November 15, 2012 15:39
Show Gist options
  • Save orient-man/4079245 to your computer and use it in GitHub Desktop.
Save orient-man/4079245 to your computer and use it in GitHub Desktop.
Fun with proxies
using Castle.DynamicProxy;
namespace MyCompany.Infrastructure.Validation
{
public static class ValidationListenerExtensions
{
public static bool ListenAll(
this IValidationListener listener,
params IValidator[] validators)
{
var interceptor = new ErrorCatcherInterceptor();
var proxy = new ProxyGenerator()
.CreateInterfaceProxyWithTargetInterface(listener, interceptor);
var success = true;
foreach (var v in validators)
{
success &= v.Validate(proxy);
if (interceptor.ErrorOccurred)
return false;
}
return success;
}
class ErrorCatcherInterceptor : IInterceptor
{
public bool ErrorOccurred { get; private set; }
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
if (invocation.Method.Name == "OnError")
ErrorOccurred = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment