public void RegisterDependencies(Container container) { //http context container.RegisterInstance<HttpContextBase>(new HttpContextWrapper(HttpContext.Current) as HttpContextBase, new SingletonReuse()); //cache provider container.Register<ICacheProvider, HttpCacheProvider>(reuse: Reuse.Singleton); // settings register for access across app container.Register<IDatabaseSettings>(made: Made.Of(() => new DatabaseSettings()), reuse: Reuse.Singleton); //data provider : TODO: Use settings to determine the support for other providers container.Register<IDatabaseProvider>(made: Made.Of(() => new SqlServerDatabaseProvider()), reuse: Reuse.Singleton); //database context container.Register<IDatabaseContext>(made: Made.Of(() => DatabaseContextManager.GetDatabaseContext()), reuse: Reuse.Singleton); //and respositories container.Register(typeof(IDataRepository<>), typeof(EntityRepository<>)); var asm = AssemblyLoader.LoadBinDirectoryAssemblies(); //services //to register services, we need to get all types from services assembly and register each of them; var serviceAssembly = asm.First(x => x.FullName.Contains("mobSocial.Services")); var serviceTypes = serviceAssembly.GetTypes(). Where(type => type.IsPublic && // get public types !type.IsAbstract && // which are not interfaces nor abstract type.GetInterfaces().Length != 0);// which implementing some interface(s) container.RegisterMany(serviceTypes, reuse: Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Replace); //we need a trasient reporter service rather than singleton container.Register<IVerboseReporterService, VerboseReporterService>(reuse: Reuse.InResolutionScope, ifAlreadyRegistered:IfAlreadyRegistered.Replace); //settings var allSettingTypes = TypeFinder.ClassesOfType<ISettingGroup>(); foreach (var settingType in allSettingTypes) { var type = settingType; container.RegisterDelegate(type, resolver => { var instance = (ISettingGroup) Activator.CreateInstance(type); resolver.Resolve<ISettingService>().LoadSettings(instance); return instance; }, reuse: Reuse.Singleton); } //and ofcourse the page generator container.Register<IPageGenerator, PageGenerator>(reuse: Reuse.Singleton); //event publishers and consumers container.Register<IEventPublisherService, EventPublisherService>(reuse: Reuse.Singleton); //all consumers which are not interfaces container.RegisterMany(new[] {typeof(IEventConsumer<>)}, serviceTypeCondition: type => !type.IsInterface); }
private static IMediator BuildMediator() { var container = new Container(); container.RegisterDelegate<SingleInstanceFactory>(r => serviceType => r.Resolve(serviceType)); container.RegisterDelegate<MultiInstanceFactory>(r => serviceType => r.ResolveMany(serviceType)); container.RegisterInstance(Console.Out); container.RegisterMany(new[] { typeof(IMediator).GetAssembly(), typeof(Ping).GetAssembly() }, type => type.GetTypeInfo().IsInterface); return container.Resolve<IMediator>(); }
public static IContainer BuildContainer() { var container = new DryIoc.Container(); container.Register <SampleDbContext>(reuse: Reuse.Scoped); container.Register <TraceInterceptor>(reuse: Reuse.Transient); container.Register <TraceInterceptorAsync>(reuse: Reuse.Transient); container.RegisterMany( typeof(ICustomerFindService).Assembly.GetTypes().Where(x => x.IsDependencyComponent()), serviceTypeCondition: t => t.IsComponentInterface(), reuse: Reuse.Scoped, ifAlreadyRegistered: IfAlreadyRegistered.Replace ); container.Intercept <TraceInterceptor>(t => t.IsComponentInterface()); return(container); }
internal static IBus Build(Config configuration) { var logger = configuration.Logger; logger.Debug("Constructing bus..."); _container = _container ?? new Container(); _container.Register(configuration.Logger.GetType(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.Register(configuration.Serializer.GetType(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.Register(configuration.Compressor.GetType(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.Register(configuration.Transport.GetType(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.CommandPipeline.GetCommandHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.CommandPipeline.GetCommandTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.CommandPipeline.GetPreHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.CommandPipeline.GetPostHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.CommandPipeline.GetErrorHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.EventPipeline.GetCompetingEventHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.EventPipeline.GetMulticastEventHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.EventPipeline.GetEventTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.EventPipeline.GetPreHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.EventPipeline.GetPostHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.EventPipeline.GetErrorHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.RequestResponsePipeline.GetRequestHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.RequestResponsePipeline.GetMulticastRequestHandlerTypes(), Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.RequestResponsePipeline.GetRequestTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.RequestResponsePipeline.GetResponseTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.RequestResponsePipeline.GetPreHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.RequestResponsePipeline.GetPostHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.RegisterMany(configuration.RequestResponsePipeline.GetErrorHookTypes(), ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.Register<IPipeline, CommandPipeline>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.Register<IPipeline, EventPipeline>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.Register<IPipeline, RequestResponsePipeline>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); _container.Register<IBus, Bus>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); return _container.Resolve<IBus>(); }