public void BulkLocateWithKey() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export(Types.FromThisAssembly()) .ByInterface<ISimpleObject>() .WithKey(t => t.Name.Last())); container.Configure(c => { foreach (char ch in "ABCDE") { c.Export<ImportSingleSimpleObject>() .WithKey(ch) .WithCtorParam<ISimpleObject>() .LocateWithKey(ch); } }); ImportSingleSimpleObject single = container.Locate<ImportSingleSimpleObject>(withKey: 'A'); Assert.NotNull(single); Assert.IsType<SimpleObjectA>(single.SimpleObject); Assert.Equal(3, container.LocateAll<ImportSingleSimpleObject>(withKey: new[] { 'A', 'C', 'E' }).Count); }
static int Main(string[] args) { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export(Types.FromThisAssembly()). ByInterface(typeof(IExampleModule)). ByInterface(typeof(IExampleSubModule<>)). ByInterface(typeof(IExample<>))); IEnumerable<IExampleModule> exampleModules = container.LocateAll<IExampleModule>(); try { foreach (IExampleModule exampleModule in exampleModules) { exampleModule.Execute(); } } catch (Exception exp) { Console.WriteLine("Exception thrown"); Console.WriteLine(exp.Message); return -1; } return 0; }
public void BeginLifetimeScope() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().Lifestyle.SingletonPerScope()); IDisposableService service = container.Locate<IDisposableService>(); Assert.NotNull(service); bool called = false; using (var scope = container.BeginLifetimeScope()) { var secondService = scope.Locate<IDisposableService>(); Assert.NotNull(secondService); Assert.NotSame(service, secondService); Assert.Same(secondService, scope.Locate<IDisposableService>()); secondService.Disposing += (sender, args) => called = true; } Assert.True(called); }
public void OwnedLazyMetaTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().WithMetadata( "Hello", "World")); Owned<Lazy<Meta<IDisposableService>>> ownedLazy = container.Locate<Owned<Lazy<Meta<IDisposableService>>>>(); Assert.NotNull(ownedLazy); Assert.NotNull(ownedLazy.Value); Assert.NotNull(ownedLazy.Value.Value); Assert.NotNull(ownedLazy.Value.Value.Value); bool disposedCalled = false; ownedLazy.Value.Value.Value.Disposing += (sender, args) => disposedCalled = true; ownedLazy.Dispose(); Assert.True(disposedCalled); KeyValuePair<string, object> metadata = ownedLazy.Value.Value.Metadata.First(); Assert.Equal("Hello", metadata.Key); Assert.Equal("World", metadata.Value); }
public void FactoryFiveArgWithOutBasicAndOutOfOrderTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); BasicService basicService = new BasicService(); container.Configure(c => { c.Export<FiveArgParameterService>().As<IArrayOfObjectsPropertyService>(); c.ExportInstance(basicService).As<IBasicService>(); }); FiveArgParameterService.ActivateWithOutBasicServiceAndOutOfOrder factory = container.Locate<FiveArgParameterService.ActivateWithOutBasicServiceAndOutOfOrder>(); Assert.NotNull(factory); IArrayOfObjectsPropertyService instance = factory(14.0m, "Blah", 9.0, 5); Assert.NotNull(instance); Assert.Equal(5, instance.Parameters.Length); Assert.Equal("Blah", instance.Parameters[0]); Assert.Equal(5, instance.Parameters[1]); Assert.Equal(9.0, instance.Parameters[2]); Assert.Equal(14.0m, instance.Parameters[3]); Assert.Equal(basicService, instance.Parameters[4]); }
public void CombinedSpecialTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<LazyService>().As<ILazyService>().WithMetadata("Hello", "World")); LazyService.Created = false; Lazy<Meta<ILazyService>> lazy = container.Locate<Lazy<Meta<ILazyService>>>(); Assert.NotNull(lazy); Assert.False(LazyService.Created); Assert.NotNull(lazy.Value); Assert.NotNull(lazy.Value.Value); ILazyService service = lazy.Value.Value; Assert.NotNull(service); Assert.True(LazyService.Created); KeyValuePair<string, object> metadata = lazy.Value.Metadata.First(); Assert.Equal("Hello", metadata.Key); Assert.Equal("World", metadata.Value); }
public void AutoCreateTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<BasicService>().As<IBasicService>()); ImportConstructorService constructorService = container.Locate<ImportConstructorService>(); Assert.NotNull(constructorService); }
private static IDependencyInjectionContainer CreateDIContainer() { var container = new DependencyInjectionContainer { ThrowExceptions = false }; container.Configure( registration => registration .Export<PeopleService>() .As<IPeopleService>()); return container; }
public void InEnvironment() { DependencyInjectionContainer container = new DependencyInjectionContainer(ExportEnvironment.UnitTest); container.Configure(c => c.ExportAssembly(GetType().Assembly).InEnvironment(ExportEnvironment.RunTimeOnly)); IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>(); Assert.NotNull(simpleObjects); Assert.Equal(0, simpleObjects.Count()); }
public void DebugConsoleLog() { Logger.SetLogService(new DebugConsoleLogService()); DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<BasicService>().As<IBasicService>()); IBasicService basicService = container.Locate<IBasicService>(); Assert.NotNull(basicService); }
public void SelectTypes() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure( c => c.ExportAssembly(GetType().Assembly).ByInterface(typeof(ISimpleObject)).Select(TypesThat.EndWith("C"))); IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>(); Assert.NotNull(simpleObjects); Assert.Equal(1, simpleObjects.Count()); }
public void ExportInterfaces() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure( c => c.ExportAssembly(GetType().Assembly).ByInterfaces(t => t.Name.StartsWith("ISimple"))); IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>(); Assert.NotNull(simpleObjects); Assert.Equal(5, simpleObjects.Count()); }
public void BasedOnInterfaceByType() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export(Types.FromThisAssembly()) .BasedOn<ISimpleObject>() .ByType()); SimpleObjectA simpleObjectA = container.Locate<SimpleObjectA>(); Assert.NotNull(simpleObjectA); }
public void BlackOutListTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.BlackListExportType(typeof(BasicService)); container.Configure( ioc => ioc.Export<BasicService>().As<IBasicService>()); IBasicService basicService = container.Locate<IBasicService>(); Assert.Null(basicService); }
public void ImportMultiple() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.ExportAssembly(GetType().Assembly)); AttributedMultipleSimpleObject multipleSimpleObject = container.Locate<AttributedMultipleSimpleObject>(); Assert.NotNull(multipleSimpleObject); Assert.NotNull(multipleSimpleObject.SimpleObjects); Assert.Equal(3, multipleSimpleObject.SimpleObjects.Count()); }
public void BulkLifestyleSingleton() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export(Types.FromThisAssembly()) .ByInterface<IBasicService>() .Lifestyle.Singleton()); IBasicService basicService = container.Locate<IBasicService>(); Assert.NotNull(basicService); Assert.Same(basicService, container.Locate<IBasicService>()); }
public void AsKeyedStringTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => { c.ExportInstance((scope, context) => "Hello"); c.ExportInstance((scope, context) => "HelloAgain").AsKeyed<string,string>("Key"); }); Assert.Equal("Hello",container.Locate<string>()); Assert.Equal("HelloAgain",container.Locate<string>(withKey: "Key")); }
public void BasedOnInterfaceTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export(Types.FromThisAssembly()) .BasedOn<ISimpleObject>() .ByInterfaces()); IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>(); Assert.NotNull(simpleObjects); Assert.Equal(5, simpleObjects.Count()); }
public void SubstituteMultiple() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Substitute(); container.Configure(c => c.Export<ImportIEnumerableService>().As<IImportIEnumerableService>()); IImportIEnumerableService iEnumerableService = container.Locate<IImportIEnumerableService>(); Assert.NotNull(iEnumerableService); Assert.Equal(1, iEnumerableService.Count); }
public void SimpleExample() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Substitute(); container.Configure(c => c.Export<ImportConstructor>()); IImportConstructor constructor = container.Locate<IImportConstructor>(); Assert.NotNull(constructor); Assert.NotNull(constructor.BasicService); }
public void AttributedPropertyInjection() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<AttributeBasicService>().As<IAttributeBasicService>()); AttributedImportPropertyService service = new AttributedImportPropertyService(); container.Inject(service); Assert.NotNull(service.BasicService); Assert.IsType(typeof(AttributeBasicService), service.BasicService); }
public void ComplexHaveAttribute() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export(Types.FromThisAssembly()). ByInterface(typeof(IAttributedSimpleObject)). Select(TypesThat.HaveAttribute<SomeTestAttribute>())); IEnumerable<IAttributedSimpleObject> simpleObjects = container.LocateAll<IAttributedSimpleObject>(); Assert.NotNull(simpleObjects); Assert.Equal(3, simpleObjects.Count()); }
public void MoqExtension() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Moq(); container.Configure(c => c.Export<ImportConstructor>()); ImportConstructor importConstructor = container.Locate<ImportConstructor>(); Assert.NotNull(importConstructor); Assert.NotNull(importConstructor.BasicService); }
public void GenericExportTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => { c.SimpleExport(typeof(GenericService<>)).As(typeof(IGenericService<>)); c.SimpleExport(typeof(GenericTransient<>)).As(typeof(IGenericTransient<>)); }); IGenericTransient<int> genericTransient = container.Locate<IGenericTransient<int>>(); Assert.NotNull(genericTransient); }
public void BasicServiceFactoryTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<BasicService>().As<IBasicService>()); BasicServiceDelegate bsDelegate = container.Locate<BasicServiceDelegate>(); Assert.NotNull(bsDelegate); IBasicService basicService = bsDelegate(); Assert.NotNull(basicService); }
static void Main(string[] args) { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export(Types.FromThisAssembly()).ByInterfaces()); var context = container.CreateContext(); context.Export("baseUrl", (s, c) => BaseUrl); var projectProcessor = container.Locate<IProjectReadmeGenerator>(context); projectProcessor.GenerateReadme(BaseOutputDir, BaseProjectDir + "DependencyInjection/"); }
public void MessageHandlerAttributeToken() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<DispatchedMessenger>().As<IDispatchedMessenger>().AndSingleton()); TestClass tc = container.Locate<TestClass>(); IDispatchedMessenger dispatchedMessenger = container.Locate<IDispatchedMessenger>(); dispatchedMessenger.Send(5, "Multiply"); Assert.AreEqual(50, tc.TestInt); }
public void SomeTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => { c.Export<BasicService>().As<IBasicService>(); c.Export<ConstructorImportService>().AndSingleton(); }); string whatDoIHaveStr = container.WhatDoIHave(true); Console.Write(whatDoIHaveStr); }
public void ExportInterfacesAndWhere() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure( c => c.Export(Types.FromThisAssembly()). ByInterfaces(TypesThat.StartWith("ISimple")). Select(TypesThat.EndWith("C"))); IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>(); Assert.NotNull(simpleObjects); Assert.Equal(1, simpleObjects.Count()); }
public void SingletonPerNamedScopeBasicTest() { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export<BasicService>().As<IBasicService>().Lifestyle.SingletonPerNamedScope("Test")); var childScope = container.CreateChildScope(scopeName: "Test"); IBasicService basicService = childScope.Locate<IBasicService>(); Assert.NotNull(basicService); Assert.Same(basicService, childScope.Locate<IBasicService>()); }
public void DynamicMethod_AspNet() { var container = new DependencyInjectionContainer(GraceDynamicMethod.Configuration(c => { c.Trace = s => Assert.DoesNotContain("falling back", s); })); container.Configure(c => { c.Export <TestController1>(); c.Export <RepositoryTransient1>().As <IRepositoryTransient1>(); c.Export <RepositoryTransient2>().As <IRepositoryTransient2>(); c.Export <RepositoryTransient3>().As <IRepositoryTransient3>(); c.Export <RepositoryTransient4>().As <IRepositoryTransient4>(); c.Export <RepositoryTransient5>().As <IRepositoryTransient5>(); c.Export <ScopedService1>().As <IScopedService1>().Lifestyle.SingletonPerScope(); c.Export <ScopedService2>().As <IScopedService2>().Lifestyle.SingletonPerScope(); c.Export <ScopedService3>().As <IScopedService3>().Lifestyle.SingletonPerScope(); c.Export <ScopedService4>().As <IScopedService4>().Lifestyle.SingletonPerScope(); c.Export <ScopedService5>().As <IScopedService5>().Lifestyle.SingletonPerScope(); }); var controller = container.Locate <TestController1>(); }
public void SharedPerAncestor_Creates_One_Guaranteed() { var container = new DependencyInjectionContainer(); container.Configure(c => c.Export <SharedClass>().Lifestyle.SingletonPerAncestor <SomeClass>(locking: true)); var instance = container.Locate <Root>(); Assert.NotNull(instance); Assert.NotNull(instance.Instance1); Assert.NotNull(instance.Instance2); Assert.NotNull(instance.Instance1.Leaf); Assert.NotNull(instance.Instance2.Leaf); Assert.NotNull(instance.Instance1.Leaf.Shared1); Assert.NotNull(instance.Instance1.Leaf.Shared2); Assert.NotNull(instance.Instance2.Leaf.Shared1); Assert.NotNull(instance.Instance2.Leaf.Shared2); Assert.Same(instance.Instance1.Leaf.Shared1, instance.Instance1.Leaf.Shared2); Assert.Same(instance.Instance2.Leaf.Shared1, instance.Instance2.Leaf.Shared2); Assert.NotSame(instance.Instance1.Leaf.Shared1, instance.Instance2.Leaf.Shared1); }
public void LocateAll_Generic() { var container = new DependencyInjectionContainer(); container.Configure(c => { c.Export <MultipleService1>().As <IMultipleService>(); c.Export <MultipleService2>().As <IMultipleService>(); c.Export <MultipleService3>().As <IMultipleService>(); c.Export <MultipleService4>().As <IMultipleService>(); c.Export <MultipleService5>().As <IMultipleService>(); }); var objects = container.LocateAll(typeof(IMultipleService)); var array = objects.ToArray(); Assert.Equal(5, array.Length); Assert.IsType <MultipleService1>(array[0]); Assert.IsType <MultipleService2>(array[1]); Assert.IsType <MultipleService3>(array[2]); Assert.IsType <MultipleService4>(array[3]); Assert.IsType <MultipleService5>(array[4]); }
public void Replace <T>(T instance) { _container.Dispose(); _container = ConfigureContainer(_configuration); _container.Configure(c => c.ExportInstance(instance).As <T>()); }
public void Replace <I, T>() { _container.Dispose(); _container = ConfigureContainer(_configuration); _container.Configure(c => c.Export <T>().As <I>()); }
public static void RegisterTransient <TInterface, TType>(this DependencyInjectionContainer container) { container.Configure(c => c.Export <TType>().As <TInterface>()); }
public static void RegisterSingleton <TInterface, TType>(this DependencyInjectionContainer container) { container.Configure(c => c.Export <TType>().As <TInterface>().Lifestyle.Singleton()); }
protected override void OnStartup(StartupEventArgs e) { ///MessageBox.Show("Startup"); base.OnStartup(e); var container = new DependencyInjectionContainer(); container.ExportInitialize <OptionsServices>((c, a, o) => o.OptionsPath = "HLab.Erp"); container.Configure(c => c.Export <EventHandlerServiceWpf>().As <IEventHandlerService>()); //boot.Container.ExportInitialize<BootLoaderErpWpf>((c, a, o) => o.SetMainViewMode(typeof(ViewModeKiosk))); NotifyHelper.EventHandlerService = container.Locate <IEventHandlerService>(); // new EventHandlerServiceWpf(); boot. var boot = container.Locate <Bootstrapper>(); //var a0 = boot.LoadDll("HLab.Erp.Core.Wpf"); var a01 = boot.LoadDll("HLab.Options.Wpf"); var a3 = boot.LoadDll("HLab.Notify.Wpf"); var a2 = boot.LoadDll("HLab.Erp.Base.Wpf"); // var b0 = boot.LoadDll("HLab.Mvvm"); // var c0 = boot.LoadDll("HLab.Mvvm.Wpf"); // var d0 = boot.LoadDll("HLab.Erp.Data"); var d1 = boot.LoadDll("HLab.Erp.Data.Wpf"); var e0 = boot.LoadDll("HLab.Erp.Acl.Wpf"); var a1 = boot.LoadDll("HLab.Erp.Workflows.Wpf"); var g0 = boot.LoadDll("HLab.Erp.Lims.Analysis.Module"); //var g1 = boot.LoadDll("HLab.Erp.Lims.Monographs.Module"); #if !DEBUG try { #endif boot.Configure(); var mvvm = container.Locate <IMvvmService>(); mvvm.Register(typeof(Customer), typeof(CustomerViewModel), typeof(IViewClassDocument), typeof(ViewModeDefault)); mvvm.Register(typeof(Manufacturer), typeof(ManufacturerViewModel), typeof(IViewClassDocument), typeof(ViewModeDefault)); mvvm.Register(); var doc = container.Locate <IDocumentService>(); doc.MainViewModel = container.Locate <MainWpfViewModel>(); boot.Boot(); #if !DEBUG } catch (Exception ex) { var view = new ExceptionView { Exception = ex }; view.ShowDialog(); } #endif }
private void ConfigureDependencies() { _container = new DependencyInjectionContainer(); _container.Configure(c => c.Export <MailMessageService>().As <IMailMessageService>().Lifestyle.Singleton()); _container.Configure(c => c.Export <MailContactService>().As <IMailContactService>().Lifestyle.Singleton()); _container.Configure(c => c.Export <MailAccountOverviewViewModel>().As <MailAccountOverviewViewModel>().Lifestyle.Singleton()); _container.Configure(c => c.Export <MailAccountViewModel>().As <MailAccountViewModel>().Lifestyle.Singleton()); _container.Configure(c => c.Export <MailContactOverviewViewModel>().As <MailContactOverviewViewModel>().Lifestyle.Singleton()); _container.Configure(c => c.Export <MailContactViewModel>().As <MailContactViewModel>().Lifestyle.Singleton()); _container.Configure(c => c.Export <Views.MailContacts.MailContactOverviewViewModel>().As <Views.MailContacts.MailContactOverviewViewModel>().Lifestyle.Singleton()); _container.Configure(c => c.Export <MailMessageOverviewView>().As <MailMessageOverviewView>().Lifestyle.Singleton()); _container.Configure(c => c.Export <MailMessageView>().As <MailMessageView>().Lifestyle.Singleton()); _container.Configure(c => c.Export <UnitOfWork>().As <IUnitOfWork>().Lifestyle.Singleton()); _container.Configure(c => c.Export <EventAggregator>().As <IEventAggregator>().Lifestyle.Singleton()); _container.Configure(c => c.Export <CryptographyService>().As <ICryptographyService>().Lifestyle.Singleton()); _container.Configure(c => c.ExportFactory <IServiceLocator>(() => new ServiceLocator(_container)).Lifestyle.Singleton()); _container.Configure(c => c.ExportFactory(() => { var optionsBuilder = new DbContextOptionsBuilder <MailieDbContext>(); optionsBuilder.UseSqlite("Data Source=Mailie.db"); return(new MailieDbContext(optionsBuilder.Options)); }).Lifestyle.Singleton()); StaticServiceLocator.SetCurrent(_container.Locate <IServiceLocator>()); }
private void ConfigureApplicationDependencies() { _container.Configure(c => c.Export <EventAggregator>().As <IEventAggregator>().Lifestyle.Singleton()); _container.Configure(c => c.Export <Communicator>().As <ICommunicator>().Lifestyle.Singleton()); _container.Configure(c => c.Export <UserSettings>().As <IUserSettings>().Lifestyle.Singleton()); _container.Configure(c => c.Export <TcpClient>().As <ITcpClient>()); _container.Configure(c => c.ExportFactory <IServiceLocator>(() => new ServiceLocator(_container)).Lifestyle.Singleton()); }
static void Main(string[] args) { DependencyInjectionContainer container = new DependencyInjectionContainer(); container.Configure(c => c.Export <Servico>().As <IServico>()); var sessionFactory = CreateSessionFactory(); var interceptor = new LogInterceptor(); interceptor.Container = container; var session = sessionFactory.OpenSession(interceptor); interceptor.Session = session; using (var tran = session.BeginTransaction()) { var p1 = new PessoaFisica { Nome = "Ricardo Romão Soares", Cpf = "1212121" }; var p2 = new PessoaFisica { Nome = "Rogéria Silva de Abreu Soares", Cpf = "2222" }; session.Save(p2); session.Save(p1); tran.Commit(); } //session.Flush(); session.Close(); session.Dispose(); session = sessionFactory.OpenSession(interceptor); interceptor.Session = session; using (var tran = session.BeginTransaction()) { var p3 = session.Query <Pessoa>().First(); p3.Nome = "Ricardo"; session.SaveOrUpdate(p3); tran.Commit(); } //session.Flush(); session.Close(); session.Dispose(); session = sessionFactory.OpenSession(interceptor); interceptor.Session = session; using (var tran = session.BeginTransaction()) { var p3 = session.Query <Pessoa>().First(); session.Delete(p3); tran.Commit(); } }
public /*override*/ void Load(DependencyInjectionContainer container) { container.Configure(c => c.ExportGenericAsTargetCovariant(typeof(INotifier <>), typeof(INotifierClass <>)).As <INotifierClass>()); container.Configure(c => c.Export(typeof(Notifier <>)).GenericAsTarget().As <INotifier>()); }
public static void RegisterTypeForNavigation <TV, TVM>(DependencyInjectionContainer builder) { ViewModelLocationProvider.Register <TV, TVM>(); builder.Configure(c => c.Export <TVM>()); }
/// <summary> /// Registers an object for navigation /// </summary> /// <param name="container"></param> /// <param name="type">The type of object to register</param> /// <param name="name">The unique name to register with the obect.</param> /// <returns><see cref="DependencyInjectionContainer"/></returns> public static DependencyInjectionContainer ConfigureTypeForNavigation(this DependencyInjectionContainer container, Type type, string name) { container.Configure(c => c.Export(type).As(typeof(object)).AsName(name)); return(container); }
public void FluentExportStrategyConfiguration_AsKeyed_Throws_Exception_Open_Closed() { var container = new DependencyInjectionContainer(); Assert.Throws <ArgumentException>(() => container.Configure(c => c.Export(typeof(DependentService <IBasicService>)).AsKeyed(typeof(IDependentService <>), 1))); }
public static DependencyInjectionContainer PrepareGrace() { var container = new DependencyInjectionContainer(); container.Configure(c => { RegisterDummyPopulation(c); c.Export <R>().Lifestyle.SingletonPerScope(); c.Export <Scoped1>().Lifestyle.SingletonPerScope(); c.Export <Scoped2>().Lifestyle.SingletonPerScope(); c.Export <Trans1>(); c.Export <Trans2>(); c.Export <Single1>().Lifestyle.Singleton(); c.Export <Single2>().Lifestyle.Singleton(); c.ExportFactory <Scoped1, Scoped3, Single1, SingleObj1, ScopedFac1>( (scoped1, scoped3, single1, singleObj1) => new ScopedFac1(scoped1, scoped3, single1, singleObj1)).Lifestyle.SingletonPerScope(); c.ExportFactory <Scoped2, Scoped4, Single2, SingleObj2, ScopedFac2>( (scoped2, scoped4, single2, singleObj2) => new ScopedFac2(scoped2, scoped4, single2, singleObj2)).Lifestyle.SingletonPerScope(); c.ExportInstance(new SingleObj1()); c.ExportInstance(new SingleObj2()); // Level 2 c.Export <Scoped3>().Lifestyle.SingletonPerScope(); c.Export <Scoped4>().Lifestyle.SingletonPerScope(); c.Export <Scoped12>().Lifestyle.SingletonPerScope(); c.Export <Scoped22>().Lifestyle.SingletonPerScope(); c.Export <Single12>().Lifestyle.Singleton(); c.Export <Single22>().Lifestyle.Singleton(); c.Export <Trans12>(); c.Export <Trans22>(); c.ExportFactory <Scoped13, Single1, SingleObj13, ScopedFac12>((scoped13, single1, singleObj13) => new ScopedFac12(scoped13, single1, singleObj13)).Lifestyle.SingletonPerScope(); c.ExportFactory <Scoped23, Single2, SingleObj23, ScopedFac22>((scoped23, single2, singleObj23) => new ScopedFac22(scoped23, single2, singleObj23)).Lifestyle.SingletonPerScope(); c.ExportInstance(new SingleObj12()); c.ExportInstance(new SingleObj22()); // level 3 c.Export <Scoped13>().Lifestyle.SingletonPerScope(); c.Export <Scoped23>().Lifestyle.SingletonPerScope(); c.Export <Single13>().Lifestyle.Singleton(); c.Export <Single23>().Lifestyle.Singleton(); c.Export <Trans13>(); c.Export <Trans23>(); c.ExportFactory <Single1, Scoped14, ScopedFac14, ScopedFac13>( (single1, scoped14, scopedFac14) => new ScopedFac13(single1, scoped14, scopedFac14)) .Lifestyle.SingletonPerScope(); c.ExportFactory <Single2, Scoped24, ScopedFac24, ScopedFac23>( (single2, scoped24, scopedFac24) => new ScopedFac23(single2, scoped24, scopedFac24)) .Lifestyle.SingletonPerScope(); c.ExportInstance(new SingleObj13()); c.ExportInstance(new SingleObj23()); // level 4 c.Export <Scoped14>().Lifestyle.SingletonPerScope(); c.Export <Scoped24>().Lifestyle.SingletonPerScope(); c.Export <Single14>().Lifestyle.Singleton(); c.Export <Single24>().Lifestyle.Singleton(); c.Export <Trans14>(); c.Export <Trans24>(); c.ExportFactory(() => new ScopedFac14()).Lifestyle.SingletonPerScope(); c.ExportFactory(() => new ScopedFac24()).Lifestyle.SingletonPerScope(); c.ExportInstance(new SingleObj14()); c.ExportInstance(new SingleObj24()); }); ResolveDummyPopulation(container); return(container); }
public void DependencyInjectionContainer_Configure_Null_Module_Throws() { var container = new DependencyInjectionContainer(); Assert.Throws <ArgumentNullException>(() => container.Configure((IConfigurationModule)null)); }
private static IServiceProvider ConfigureServices(IServiceCollection services) { //services.AddSingleton<IGrainActivator, TenantGrainActivator>(); var container = new DependencyInjectionContainer(c => c.Behaviors.AllowInstanceAndFactoryToReturnNull = true); services.AddSingleton <IAppTenantRegistry, AppTenantRegistry>(); var providers = container.Populate(services); var tenantRegistry = container.Locate <IAppTenantRegistry>(); var tenants = tenantRegistry.GetAll().ToList(); container.Configure(c => { c.Export <TenantGrainActivator>().As <IGrainActivator>().Lifestyle.Singleton(); //c // //.Export<MockLoLHeroDataClient>() // .Export<MockHotsHeroDataClient>() // .As<IHeroDataClient>() // ; //c. c.ExportFactory <IExportLocatorScope, ITenant>(scope => scope.GetTenantContext()); //c.Export<MockHotsHeroDataClient>().AsKeyed<IHeroDataClient>("hots").Lifestyle.Singleton(); //c.Export<MockLoLHeroDataClient>().AsKeyed<IHeroDataClient>("lol").Lifestyle.Singleton(); //c.ExportFactory<IExportLocatorScope, ITenant, IHeroDataClient>((scope, tenant) => //{ // var tenant = RequestContext.Get("tenant") ?? tenant?.Key; // if (tenant == null) throw new ArgumentNullException("tenant", "Tenant must be defined"); // return scope.Locate<IHeroDataClient>(withKey: tenant); //}); //c.ExportForAllTenants<IHeroDataClient, MockLoLHeroDataClient>(Tenants.All, x => x.Lifestyle.Singleton()); //c.ForTenant(Tenants.LeageOfLegends).PopulateFrom(x => x.AddHeroesLoLGrains()); //c.ForTenant(Tenants.HeroesOfTheStorm).PopulateFrom(x => x.AddHeroesHotsGrains()); c.ForTenants(tenants, tb => { tb .ForTenant(AppTenantRegistry.LeagueOfLegends.Key, tc => tc.PopulateFrom(x => x.AddAppLoLGrains())) .ForTenant(x => x.Key == AppTenantRegistry.HeroesOfTheStorm.Key, tc => tc.PopulateFrom(x => x.AddAppHotsGrains())) ; }); /* * * // register with filter tenant * c.ForTenants(tenants, tb => * { * tb.ForTenant(x => x.Platform == "x").PopulateFrom(x => x.AddHeroesHotsGrains()); * }); * * // register one per type * c.For<IHeroDataClient>(tb => * { * tb.For(x => x.Key == "lol").Use<MockLoLHeroDataClient>(); * }); * */ }); return(providers); }
private void ConfigureContainer() { Log.Debug("Configure container."); _container.Configure(c => c.Export <RequestParser>().As <IRequestParser>().Lifestyle.Singleton()); _container.Configure(c => c.Export <ResponseBuilder>().As <IResponseBuilder>().Lifestyle.Singleton()); _container.Configure(c => c.Export <TcpServer>().As <ITcpServer>().Lifestyle.Singleton()); _container.Configure(c => c.Export <HomeAutomationCommunication>().As <IHomeAutomationCommunication>().Lifestyle.Singleton()); _container.Configure(c => c.Export <ConnectionHandler>().As <IConnectionHandler>().Lifestyle.Singleton()); _container.Configure(c => c.Export <DataConverterDispatcher>().As <IDataConverterDispatcher>().Lifestyle.Singleton()); _container.Configure(c => c.Export <ByteConverter>().As <IDataConverter>().Lifestyle.Singleton()); _container.Configure(c => c.Export <Int32ArrayConverter>().As <IDataConverter>().Lifestyle.Singleton()); _container.Configure(c => c.Export <Int32Converter>().As <IDataConverter>().Lifestyle.Singleton()); _container.Configure(c => c.Export <UInt16Converter>().As <IDataConverter>().Lifestyle.Singleton()); _container.Configure(c => c.Export <StringConverter>().As <IDataConverter>().Lifestyle.Singleton()); _container.Configure(c => c.ExportInstance(new ServiceLocator(_container)).As <IServiceLocator>().Lifestyle.Singleton()); }
protected override void Configure() { kernel = new DependencyInjectionContainer(); kernel.Configure(x => x.Export <WindowManager>().As <IWindowManager>().Lifestyle.Singleton()); kernel.Configure(x => x.Export <EventAggregator>().As <IEventAggregator>().Lifestyle.Singleton()); }
public override void RegisterTransient(Type serviceType, Type implementationType) { container.Configure(c => c.Export(implementationType).As(serviceType).AutoWireProperties()); }
public void Configure() => _container.Configure(c => Register(c, _registrations));