Created
November 15, 2012 15:39
-
-
Save orient-man/4079245 to your computer and use it in GitHub Desktop.
Fun with proxies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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