Saturday 9 October 2010

PostSharp Collaborators

One of the things we've been working on recently is caching with Memcache.  Caching is a cross-cutting concern that we thought could be refactored into an aspect.  Thus making caching happen automatically with the small addition of an attribute.  We went down, what seemed, the obvious route of postsharp and duly made and aspect that took care of our caching needs.

The problem we had with our aspect was getting the instance of the cache into the aspect. Normally we would include this in the constructor as a collaborator.  Obviously we can't do that because the aspect needs an empty constructor.  So what are the options?
  1. Pass the instance into the method where the aspect is being used, then pull it out in the aspect by getting the method args. 
  2. Use a service locator/factory or some other mechanism to get hold of the instance.
We tried the first option and it wasn't particularly satisfactory, it looks like your passing something into the method that you're not using in that method call (exasperated by R# telling you you don't need it). Not ideal! So we went down the line of getting hold of the instance using a service locator.

Since we went live with I found out that Castle Windsor has some limited options for AOP, it's called an IoC Interceptor this gives you the ability to intercept every method call on an interface.  This is fine for what we have as all the interface calls require caching.

No comments:

Post a Comment