Skip to content

Instantly share code, notes, and snippets.

@SergeiGolos
Created December 4, 2012 14:38
Show Gist options
  • Save SergeiGolos/4204601 to your computer and use it in GitHub Desktop.
Save SergeiGolos/4204601 to your computer and use it in GitHub Desktop.
[C#] Raven Unity Bootstrap
public static class Bootstrapper
{
public static void Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new IoCContainer(container));
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterInstance<IDocumentStore>(new EmbeddableDocumentStore().Initialize());
// e.g. container.RegisterType<ITestService, TestService>();
return container;
}
}
public class IoCContainer : System.Web.Mvc.IDependencyResolver
{
private readonly IUnityContainer container;
public IoCContainer(IUnityContainer container)
{
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return this.container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return this.container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment