-
Notifications
You must be signed in to change notification settings - Fork 39
AOP Interception
tylerje edited this page Oct 23, 2014
·
1 revision
Your preferred inversion of control container may already provide aspect oriented interception. Many do. But if yours does not or if you do not use one but you still need aspect oriented programming to introduce cross cutting concerns into your service implementation or even onto your client proxy, you can use ServiceWire to do so.
Here is an example of how to use ServiceWire's interception and cross cutting concerns to introduce pre- and post- and exception handling aspects.
[Test]
public void SimpleTest()
{
var preInvokeInfo = string.Empty;
var postInvokeInfo = string.Empty;
var exceptionHandlerInfo = string.Empty;
var cc = new CrossCuttingConcerns();
cc.PreInvoke = (instanceId, methodName, parameters) =>
{
preInvokeInfo = methodName + "_preInvokeInfo";
};
cc.PostInvoke = (instanceId, methodName, parameters) =>
{
postInvokeInfo = methodName + "_postInvokeInfo";
};
cc.ExceptionHandler = (instanceId, methodName,
parameters, exception) =>
{
exceptionHandlerInfo = methodName + "_exceptionHandlerInfo";
return false; //do not throw
};
var t = Interceptor.Intercept<ISimpleMath>(new SimpleMath(), cc);
var a = t.Add(1, 2);
Assert.IsNotNullOrEmpty(preInvokeInfo);
Assert.IsNotNullOrEmpty(postInvokeInfo);
Assert.IsNullOrEmpty(exceptionHandlerInfo);
var b = t.Divide(4, 0);
Assert.IsNotNullOrEmpty(preInvokeInfo);
Assert.IsNotNullOrEmpty(postInvokeInfo);
Assert.IsNotNullOrEmpty(exceptionHandlerInfo);
}
As you can see, each cross cutting concern is an Action or Func delegate. The test code demonstrates their use. Of course, you would want to do something more useful that what you see in the test code.