/// <summary>Registers the type mappings with the Unity container.</summary> /// <param name="container">The unity container to configure.</param> /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> private static void RegisterTypes(IUnityContainer container) { // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. // container.LoadConfiguration(); container.RegisterType <IDbContextStorage, HttpDbContextStorage>(new ContainerControlledLifetimeManager()); container.RegisterType(typeof(IDbContextProvider <>), typeof(SimpleDbContextProvider <>), new TransientLifetimeManager()); container.RegisterType <ITypeFinder, TypeFinder>(new ContainerControlledLifetimeManager()); //var repoAssemblies = AllClasses.FromAssemblies(Assembly.Load("Qxr.Tests.Repositories")).Where(t => typeof(IDependency).IsAssignableFrom(t)); //var serviceAssemblies = AllClasses.FromAssemblies(Assembly.Load("Qxr.Services")).Where(t => typeof(IDependency).IsAssignableFrom(t)); var repoAssemblies = AllClasses.FromAssemblies(Assembly.Load("Qxr.Tests.Repositories")).Where(t => t.Name.EndsWith("Repository")); var serviceAssemblies = AllClasses.FromAssemblies(Assembly.Load("Qxr.Services")).Where(t => t.Name.EndsWith("Service")); var allAssemblies = repoAssemblies.Concat(serviceAssemblies).ToList(); container.RegisterType <ILogger, Log4NetAdapter>(new ContainerControlledLifetimeManager()); var interceptionInjectionMembers = new InjectionMember[] { new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <AuditLoggingBehavior>() }; container.RegisterTypes(allAssemblies, WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.Transient, t => interceptionInjectionMembers); container.RegisterType(typeof(IReadOnlyRepository <>), typeof(QxrTestRepositoryBase <>), interceptionInjectionMembers); container.RegisterType(typeof(IRepository <>), typeof(QxrTestRepositoryBase <>), interceptionInjectionMembers); container.RegisterType <IUnitOfWork, QxrTestUnitOfWork>(interceptionInjectionMembers); //var loggerTypes = allAssemblies.Where(t => t.GetProperties().Any(c => c.PropertyType == typeof(ILogger))); //container.RegisterTypes(loggerTypes, // WithMappings.FromMatchingInterface, // WithName.Default, // WithLifetime.Transient, t => new InjectionMember[] // { // new InjectionProperty("Logger") // }); container.RegisterType <IIocResolver, IocResolver>(new InjectionConstructor(container)); DbContextProviderFactory.Initialize(container.Resolve <IIocResolver>()); LoggingFactory.Initialize(container.Resolve <ILogger>()); new AutoMapperBootstrap(container.Resolve <ITypeFinder>()).CreateMappings(); }
public override void Initialize() { IocManager.RegisterType <IDbContextStorage, HttpDbContextStorage>(DependencyLifeStyle.Singleton); IocManager.RegisterType(typeof(IDbContextProvider <>), typeof(SimpleDbContextProvider <>), DependencyLifeStyle.Singleton); DbContextProviderFactory.Initialize(IocManager.Resolve <IIocManager>()); }