Sunday, July 7, 2013

Aspect Interceptor With Castle Windsor C#

Purpose


  1. Show how to create an interceptor using Castle Windsor
  2. Describe when interceptors should be used

Code

An Interceptor

Intercepts the target, and then calls proceed

Windsor Registration

Initializes windsor, and then assigns the SimpleInterceptor as an implementation for IInterceptor.

Using an interceptor

By adding the interceptor attribute and making the method virtual.  The TestClass's DoStuff() method will now be intercepted when the method is called and after the method has returned

When

Interceptors should be implemented to allow you to create a "Cross Cutting Concerns".  The best example is logging.  With logging you may want to know when a method was started and when it returned.

Besides logging there are several use cases.  One that I wrote recently was the ability to publish domain/events when a certain method was called.  This allowed me to implement the domain business logic, and then to publish that event independently.

From a Single Responsibility standpoint this allows your class to do "One thing and do it well", but then allows you to independently spin off into different set of concerns.


No comments:

Post a Comment