I want to make sure I gave a shout out to Autofac given that I had been talking about Ninject for a few days. Autofac is another free, lightweight open source dependency injection tool that has a very inviting and easy-to-use interface. I mentioned Autofac on PnPGuidance a couple of times:
I haven't done a lot of demos on Autofac only because it has a really awesome Wiki with several examples on how to use Autofac.
One of the things I just noticed is that the AutofacContrib Project is supporting integration with Castle DynamicProxy2 and NMock2. This is fanstastic as now we can use AOP Method Interception with Autofac and Castle DynamicProxy2 in our applications.
Logging as a Cross-Cutting Concern
Let's pretend we want to intercept the method call, Foo.DoSomething(), and in particular, we want to log a message to our logging service that the method was indeed called. Yes, a trivial example, but it explains the concept easily.
First, we need to reference some assemblies for this Console Project:
- Autofac.dll
- AutofacContrib.DynamicProxy2
- Castle.Core
- Castle.DynamicProxy2
Here is Foo and IFoo and the method DoSomething we want to intercept decorated with a named InterceptAttribute called “Log“. The InterceptAttribute is a part of the AutofacContrib.DynamicProxy2 Assembly.
[Intercept("Log")]
public class Foo : IFoo
{
public virtual void DoSomething()
{
Console.WriteLine("In Method");
}
}
public interface IFoo
{
void DoSomething();
}
Our LogInterceptor that will be intercepting the DoSomething Method uses a logging service which is as follows:
public interface ILogger
{
void Write(string message);
}
public class Logger : ILogger
{
public void Write(string message)
{
Console.WriteLine(message);
}
}
This logging service just logs to the Console in this case, which is perfect to show in a Console Application :)
LogInterceptor
Using standard Castle DynamicProxy2 classes, we need to create a LogInterceptor that intercepts the DoSomething Method before it is invoked and let's us know it did the interception by writing a message to the logging service. Here I have just derived from StandardInterceptor in the Castle.DynamicProxy2 Assembly.
public class LogInterceptor : StandardInterceptor
{
private ILogger _logger;
public LogInterceptor(ILogger logger)
{
_logger = logger;
}
protected override void PreProceed(IInvocation invocation)
{
_logger.Write(string.Format("Called {0}",
invocation.Method.Name));
}
}
Pretty self-explanatory. The Logging Service is injected into the LogInterceptor. The PreProceed Method is called before the method being intercepted ( DoSomething ) is invoked.
ContainerBuilder and StandardInteceptionModule
Last, but not least, we need to wire all of this together. The key here is that I need to associate the InterceptAttribute with the name “Log“ to the LogInterceptor and load the StandardInterceptionModule into our container via the ContainerBuilder. Here is the code for that:
internal class Program
{
private static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.Register<Foo>().As<IFoo>();
builder.Register<Logger>().As<ILogger>().SingletonScoped();
builder.Register(c => new
LogInterceptor(c.Resolve<ILogger>())).Named("Log");
builder.RegisterModule(new StandardInterceptionModule());
IContainer container = builder.Build();
IFoo foo = container.Resolve<IFoo>();
foo.DoSomething();
}
}
When you run the Console Application you get the following. Notice that indeed the DoSomething Method is being intercepted prior to being called.

Conclusion
If you are on the market for a good dependency injection tool for the .NET Framework, make sure you check out Autofac, too!
David Hayden