public void logging_is_not_injected() { IDependencyResolver resolver = new InternalDependencyResolver(); resolver.AddDependency <IGenerateCallGraphs, TopologicalSortCallGraphGenerator>(); resolver.AddDependency <IPipelineContributor, BootstrapperContributor>(); resolver.AddDependency <IPipelineContributor, First>(); var initialiser = new ThreePhasePipelineInitializer( resolver.Resolve <IEnumerable <IPipelineContributor> >(), resolver.Resolve <IGenerateCallGraphs>()); var props = new StartupProperties { OpenRasta = { Diagnostics = { TracePipelineExecution = false }, Pipeline = { Validate = false } } }; var pipeline = initialiser .Initialize(props); pipeline.MiddlewareFactories .ShouldNotContain(factory => factory is LoggingMiddlewareFactory); }
public void logging_is_injected() { IDependencyResolver resolver = new InternalDependencyResolver(); resolver.AddDependency <IGenerateCallGraphs, TopologicalSortCallGraphGenerator>(); resolver.AddDependency <IPipelineContributor, PreExecutingContributor>(); resolver.AddDependency <IPipelineContributor, First>(); var initialiser = new ThreePhasePipelineInitializer( resolver.Resolve <IEnumerable <IPipelineContributor> >(), resolver.Resolve <IGenerateCallGraphs>()); var pipeline = initialiser .Initialize(new StartupProperties { OpenRasta = { Diagnostics = { TracePipelineExecution = true }, Pipeline = { Validate = false } } }); var factories = pipeline.MiddlewareFactories .Where((factory, i) => i % 2 == 0) .ToList(); factories.Count().ShouldBe(pipeline.MiddlewareFactories.Count() / 2); factories.ShouldAllBe(factory => factory is LoggingMiddlewareFactory); }
public void resolving_funcs() { Should.NotThrow(() => { var factory = resolver.Resolve <Func <Simple> >(); return(factory()); }); }
public registration_depending_on_func_of_unregistered_after_registration() { resolver = new InternalDependencyResolver(); resolver.AddDependency <DependsOnFuncOfSimple>(); instance = resolver.Resolve <DependsOnFuncOfSimple>(); resolver.AddDependency <Simple>(); }
protected IPipeline CreatePipeline(Type callGraphGeneratorType, Type[] contributorTypes) { var resolver = new InternalDependencyResolver(); resolver.AddDependencyInstance <IDependencyResolver>(resolver); resolver.AddDependency <IPipelineContributor, BootstrapperContributor>(); resolver.AddDependency <T>(); //resolver.AddDependency<IPipeline, T>(DependencyLifetime.Singleton); if (callGraphGeneratorType != null) { resolver.AddDependency(typeof(IGenerateCallGraphs), callGraphGeneratorType, DependencyLifetime.Singleton); } foreach (var type in contributorTypes) { resolver.AddDependency(typeof(IPipelineContributor), type, DependencyLifetime.Singleton); } var runner = resolver.Resolve <T>(); runner.Initialize(); return(runner); }
public void CanMakeSingletonJellybeanDispenser() { IDependencyResolver container = new InternalDependencyResolver(); container.AddDependency<SweetShop>(DependencyLifetime.Transient); container.AddDependency<SweetVendingMachine>(DependencyLifetime.Transient); container.AddDependency<IJellybeanDispenser, StrawberryJellybeanDispenser>(DependencyLifetime.Singleton); SweetShop sweetShop = container.Resolve<SweetShop>(); SweetShop sweetShop2 = container.Resolve<SweetShop>(); Assert.AreNotSame(sweetShop, sweetShop2, "Root objects are equal"); Assert.AreNotSame(sweetShop.SweetVendingMachine, sweetShop2.SweetVendingMachine, "Contained objects are equal"); // should be same service Assert.AreSame(sweetShop.SweetVendingMachine.JellybeanDispenser, sweetShop2.SweetVendingMachine.JellybeanDispenser, "services are not equal"); }
protected static IPipelineAsync CreatePipeline( Type callGraphGeneratorType, Type[] contributorTypes, Action <StartupProperties> options = null) { var resolver = new InternalDependencyResolver(); resolver.AddDependencyInstance <IDependencyResolver>(resolver); resolver.AddDependency <IPipelineInitializer, ThreePhasePipelineInitializer>(); resolver.AddDependency(typeof(IGenerateCallGraphs), callGraphGeneratorType, DependencyLifetime.Transient); if (callGraphGeneratorType != null) { resolver.AddDependency(typeof(IGenerateCallGraphs), callGraphGeneratorType, DependencyLifetime.Singleton); } foreach (var type in contributorTypes) { resolver.AddDependency(typeof(IPipelineContributor), type, DependencyLifetime.Singleton); } var runner = resolver.Resolve <IPipelineInitializer>(); var opt = new StartupProperties { OpenRasta = { Pipeline = { Validate = true } } }; options?.Invoke(opt); return(runner.Initialize(opt)); }
public void func_is_resolved() { var resolver = new InternalDependencyResolver(); var factory = resolver.Resolve <Func <IEnumerable <Simple> > >(); resolver.AddDependency <Simple>(); factory().ShouldHaveSingleItem().ShouldNotBeNull(); }
public void CanMakeAniseedRootObject() { IDependencyResolver container = new InternalDependencyResolver(); container.AddDependency<SweetShop, AniseedSweetShop>(); SweetShop sweetShop = container.Resolve<SweetShop>(); Assert.AreEqual(Jellybean.Aniseed, sweetShop.DispenseJellyBean()); }
public void CanMakeSweetShopWithVanillaJellybeans() { IDependencyResolver container = new InternalDependencyResolver(); container.AddDependency<SweetShop>(); container.AddDependency<SweetVendingMachine>(); container.AddDependency<IJellybeanDispenser, VanillaJellybeanDispenser>(); SweetShop sweetShop = container.Resolve<SweetShop>(); Assert.AreEqual(Jellybean.Vanilla, sweetShop.DispenseJellyBean()); }
public void resolve_succeeds() { var resolver = new InternalDependencyResolver(); resolver.AddDependency <IContextStore, AmbientContextStore>(); resolver.AddDependency <Contributor>(DependencyLifetime.Singleton); resolver.AddDependency <ErrorCollector>(DependencyLifetime.Transient); var contributor = resolver.Resolve <Contributor>(); using (new ContextScope(new AmbientContext())) using (resolver.CreateRequestScope()) { resolver.AddDependencyInstance(new Request(), DependencyLifetime.PerRequest); var result = contributor.Factory(); result.ShouldHaveSingleItem(); } }
public void CanUseConstructedObject() { IDependencyResolver container = new InternalDependencyResolver(); container.AddDependency<SweetShop>(DependencyLifetime.Transient); container.AddDependency<SweetVendingMachine>(DependencyLifetime.Transient); container.AddDependencyInstance<IJellybeanDispenser>(new AnyJellybeanDispenser(Jellybean.Cocoa)); SweetShop sweetShop = container.Resolve<SweetShop>(); Assert.AreEqual(Jellybean.Cocoa, sweetShop.DispenseJellyBean()); }
public void CanUseObjectFactory() { IDependencyResolver container = new InternalDependencyResolver(); container.AddDependency<SweetShop>(DependencyLifetime.Transient); container.AddDependency<SweetVendingMachine>(DependencyLifetime.Transient); container.AddDependency<IJellybeanDispenser>(c => new AnyJellybeanDispenser(Jellybean.Orange)); SweetShop sweetShop = container.Resolve<SweetShop>(); Assert.AreEqual(Jellybean.Orange, sweetShop.DispenseJellyBean()); }
public void CanUseObjectFactoryWithSingleton() { IDependencyResolver container = new InternalDependencyResolver(); container.AddDependency<SweetShop>(DependencyLifetime.Transient); container.AddDependency<SweetVendingMachine>(DependencyLifetime.Transient); IJellybeanDispenser instance = new AnyJellybeanDispenser(Jellybean.Orange); // The lambda captures the instance and uses it across multiple calls. It's a singleton container.AddDependency<IJellybeanDispenser>(c => instance); SweetShop sweetShop = container.Resolve<SweetShop>(); SweetShop sweetShop2 = container.Resolve<SweetShop>(); Assert.AreEqual(Jellybean.Orange, sweetShop.DispenseJellyBean()); Assert.AreEqual(Jellybean.Orange, sweetShop2.DispenseJellyBean()); Assert.AreNotSame(sweetShop, sweetShop2, "Root objects are equal"); Assert.AreNotSame(sweetShop.SweetVendingMachine, sweetShop2.SweetVendingMachine, "Contained objects are equal"); // should be same service Assert.AreSame(sweetShop.SweetVendingMachine.JellybeanDispenser, sweetShop2.SweetVendingMachine.JellybeanDispenser, "services are not equal"); }
public void JellybeanDispenserHasNewInstanceEachTime() { IDependencyResolver container = new InternalDependencyResolver(); // default is " DependencyLifetime.Singleton" so specify transient objects container.AddDependency<SweetShop>(DependencyLifetime.Transient); container.AddDependency<SweetVendingMachine>(DependencyLifetime.Transient); container.AddDependency<IJellybeanDispenser, VanillaJellybeanDispenser>(DependencyLifetime.Transient); SweetShop sweetShop = container.Resolve<SweetShop>(); SweetShop sweetShop2 = container.Resolve<SweetShop>(); Assert.AreNotSame(sweetShop, sweetShop2, "Root objects are equal"); Assert.AreNotSame(sweetShop.SweetVendingMachine, sweetShop2.SweetVendingMachine, "Contained objects are equal"); Assert.AreNotSame(sweetShop.SweetVendingMachine.JellybeanDispenser, sweetShop2.SweetVendingMachine.JellybeanDispenser, "services are equal"); }
public override void EndScope() { _resolver.Resolve <IContextStore>().GetConcurrentContextInstances().Clear(); }
public void can_resolve_type() { Should.NotThrow(() => resolver.Resolve <DependsOnFuncOfSimple>()); }