I couldn't find an example of doing AOP Method Interception using Ninject and Castle's DynamicProxy2 or LinFu DynamicProxy, so I consulted Ninject's Unit Tests and created my own sample. In the sample shown below, I will be using Castle's DynamicProxy2, but quite honestly I think you can use Linfu DynamicProxy by just swapping out the DynamicProxy2Module for the LinfuModule in the code below as well as referencing the Ninject.Integration.LinFu Assembly as opposed to the Ninject.Integration.DynamicProxy2 Assembly. By the way, there are some really good CodeProject article's on Linfu that I mentioned on PnPGuidance: LinFu Articles on CodeProject Well-Worth Reading.
If you haven't already, you can check out my other Ninject Dependency Injection Tutorials:
With that done, here is a quick walkthrough of an example of using Ninject with Castle's DynamicProxy to intercept a method and log the name of the method being called before invoking the method.
Ninject and Castle's DynamicProxy2
I created a Console Application in Visual Studio 2008 and referenced the following assemblies:
- Ninject.Core
- Ninject.Integration.DynamicProxy2
In the Main Routine I create a StandardKernel and pass in not only my InfrastructureModule but the DynamicProxy2Module that is in the Ninject.Integration.DynamicProxy2 Assembly. I request an IFoo Service that we will talk about in a moment and then call the DoSomething Method on IFoo. I realize the names are not very creative, but I just wanted to get AOP happening here :)
class Program
{
static void Main(string[] args)
{
IKernel kernel = new StandardKernel(
new DynamicProxy2Module(),
new InfrastructureModule());
IFoo foo = kernel.Get<IFoo>();
foo.DoSomething();
}
}
As we talked about in a previous tutorial on Ninject, you register your services in a module. In this case, I am registering the IFoo Service in my InfrastructureModule. I am also registering an ILogger Service as well.
public class InfrastructureModule : StandardModule
{
public override void Load()
{
Bind<IFoo>().To<Foo>();
Bind<ILogger>().To<Logger>()
.Using<SingletonBehavior>();
}
}
Let's get some of these Interfaces and Class Declarations out of the way that we are binding in Ninject:
public interface IFoo
{
void DoSomething();
}
public class Foo : IFoo
{
[Log]
public virtual void DoSomething()
{
}
}
public interface ILogger
{
void Write(string message);
}
public class Logger : ILogger
{
public void Write(string message)
{
Console.WriteLine(message);
}
}
I want to point out that LogAttribute, [Log], on the virtual DoSomething Method on Foo. That is a special attribute that derives from the InterceptAttribute in Ninject.Core:
public class LogAttribute : InterceptAttribute
{
public override IInterceptor
CreateInterceptor(IRequest request)
{
return request.Kernel.Get<LogInterceptor>();
}
}
The LogAttribute is associated with a LogInterceptor Class that calls the ILogger Service to log the name of the method being called prior to it being invoked:
public class LogInterceptor : SimpleInterceptor
{
ILogger _logger;
public LogInterceptor(ILogger logger)
{
_logger = logger;
}
protected override void
BeforeInvoke(IInvocation invocation)
{
_logger.Write(string.Format("Called {0}",
invocation.Request.Method.Name));
}
}
When you run the application, you get the name of the method being called, DoSomething, displayed in your Console.

Conclusion
I hope you enjoyed the sample on using Ninject with Castle's DynamicProxy2 to do method interception and simple logging.
David Hayden