Created
December 4, 2012 14:38
-
-
Save SergeiGolos/4204601 to your computer and use it in GitHub Desktop.
[C#] Raven Unity Bootstrap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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