Skip to content

Instantly share code, notes, and snippets.

@nootn
Created February 5, 2013 11:58
Show Gist options
  • Save nootn/4714028 to your computer and use it in GitHub Desktop.
Save nootn/4714028 to your computer and use it in GitHub Desktop.
MiniProfiler.Windows IOC and AOP #1
private static void ConfigureIocAndAop()
{
var builder = new ContainerBuilder();
//If using an IOC container, it is a handy to be able to us an IProfiler to
//wrap mini profiler calls just to remove that dependency for unit testing
builder.RegisterType<MiniProfilerWrapper>().AsImplementedInterfaces();
//We want some classes that do some work, but we don't want to have to wrap profiler steps
//around each method call, so use AOP to do it for us!
SnapConfiguration.For(new AutofacAspectContainer(builder)).
Configure(c =>
{
c.IncludeNamespace("ConsoleApplicationWithIocAndAop.*");
c.Bind<ProfileMethodInterceptor>().To<ProfileMethodAttribute>();
});
//Register the types we need to run the application
builder.RegisterType<DoQuickWork>().AsImplementedInterfaces();
builder.RegisterType<DoSlowWork>().AsImplementedInterfaces();
builder.RegisterType<ConsoleApplication>().AsImplementedInterfaces();
//Ensure we get useful type load exceptions: http://stackoverflow.com/a/8978721/281177
try
{
_container = builder.Build();
}
catch (Exception ex)
{
if (ex is ReflectionTypeLoadException)
{
var typeLoadException = ex as ReflectionTypeLoadException;
var loaderExceptions = typeLoadException.LoaderExceptions;
throw new AggregateException(typeLoadException.Message, loaderExceptions);
}
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment