1

I'm not so much seeking a specific implementation but trying to figure out the proper terms for what I'm trying to do so I can properly research the topic.

I have a bunch of interfaces and those interfaces are implemented by controllers, repositories, services and whatnot. Somewhere in the start up process of the application we're using the Castle.MicroKernel.Registration.Component class to register the classes to use for a particular interface. For instance:

Component.For<IPaginationService>().ImplementedBy<PaginationService>().LifeStyle.Transient

Recently I became interested in creating an audit trail of every class and method call. There's a few hundred of these classes so writing a proxy class for each one by hand isn't very practical. I could use a template to generate the code but I'd rather not blow up our code base with all that.

So I'm curious if there's some kind of on the fly solution. I know nHibernate creates proxy classes at some point which overlay all the entity classes. Can someone give me some guidance on how I might be able to do something similar here?

Something like:

Component.For<IPaginationService>().ImplementedBy<ProxyFor<PaginationService>>().LifeStyle.Transient

Obviously that won't work because I can only use generics to generalize the types of methods but not the methods themselves. Is there some tricky reflection approach I can use to do this?

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
  • You mean like [Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class](https://msdn.microsoft.com/en-us/magazine/dn574804.aspx)? – NightOwl888 Sep 25 '17 at 18:29
  • Not sure what you want to do, I know you know, but you can use reflection to create proxies at runtime. NHibernate and EF does it and it makes the lazy loading and other features possible. See [this post](https://stackoverflow.com/a/4802281/316799). For audit I like to use Listeners of NHibernate, it is a very useful feature :) – Felipe Oriani Sep 25 '17 at 18:30
  • 1
    Have you considered dynamic interception? http://app-code.net/wordpress/?p=689 – David Osborne Sep 26 '17 at 07:48
  • @DavidOsborne - These both seem like good answers to my (vague) question. If you add them I'd be happy to give you an upvote for you time. – Spencer Ruport Oct 05 '17 at 18:49
  • @FelipeOriani - These both seem like good answers to my (vague) question. If you add them I'd be happy to give you an upvote for you time. – Spencer Ruport Oct 05 '17 at 18:49

1 Answers1

2

You are looking for what Castle Windsor calls interceptors. It's an aspect-oriented way to tackle cross-cutting concerns -- auditing is certainly one of them. See documentation, or an article about the approach:

Aspect oriented programming is an approach that effectively “injects” pieces of code before or after an existing operation. This works by defining an Inteceptor wrapping the logic being invoked then registering it to run whenever a particular set/sub-set of methods are called.

If you want to apply it to many registered services, read more about interceptor selection mechanisms: IModelInterceptorsSelector helps there.

Using PostSharp, things like this can be even done at compile time. This can speed the resulting application, but when used correctly, interceptors are not slow.

Lukáš Lánský
  • 4,641
  • 3
  • 32
  • 48