At the current place of work i have managed to introduce the notion of IoC and DI. As the team was using EntLib i investigated Unity and found it to be a suitable replacement for Windsor or SM considering how we were going to be using it.
We have just started up a new project and i have asked one of the lads to investigate AOP with unity, what we found was a pretty simple solution for our initial requirement, logging the service calls using Unity
Below is the 3 files that make up the spike. it very trivial, but there is some pretty average info on how Unity work, there is sample (like this) but with little explanation as to what is going on. David Hayden is probably the first port of call for more info (note you will need to reference Unity 1.2 and Unity Interception).
Note we are using the TransparentProxyInterceptor not the InterfaceInterceptor, which i believe is broken as it does not handle inheritance, which in an OO world is not good enough.
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.RegisterType();
container.AddNewExtension<Interception>();
container.Configure<Interception>().
SetDefaultInterceptorFor<ITalker>(new TransparentProxyInterceptor()).
AddPolicy(“Logging”).
AddMatchingRule(new TypeMatchingRule(typeof(ITalker))).
AddCallHandler(typeof(LoggerHandler));
Console.WriteLine(“This is the start”);
container.Resolve<ITalker>().Talk();
Console.WriteLine(“This is the end”);
Console.ReadLine();
}
}
public interface ITalker
{
void Talk();
}
public class Talker : ITalker
{
public void Talk()
{
Console.WriteLine(“helllllllooooo!”);
}
}
public class LoggerHandler : ICallHandler
{
public LoggerHandler()
{
Order = 0;
}
public int Order { get; set; }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate
etNext)
{
Console.WriteLine(“** I’m in!**”);
var result = getNext().Invoke(input, getNext);
Console.WriteLine(“** Out I go 🙂 **”);
return result;
}
}
———————————————————
Which returns :
This is the start
** I’m in!**
helllllllooooo!
** Out I go 🙂 **
This is the end
———————————————————
*Sorry about the formatting, this is done with out WLW*