public void SimpleDefaultConstructorTest() { var container = new QuickInjectContainer(); var classA = container.Resolve <A>(); Assert.AreEqual(classA.Value, 42); }
public static async Task Main(string[] args) { TaskScheduler.UnobservedTaskException += (sender, e) => { Console.WriteLine("Unobserved exception: {0}", e.Exception); }; var container = new QuickInjectContainer(); var hostingEnvironment = new HostingEnvironment { ContentRootPath = Directory.GetCurrentDirectory() }; var defaultEventSourceLoggerFactory = new DefaultEventSourceLoggerFactory(); var startup = new Startup(); startup.SetupQuickInjectContainer(container); startup.Configure(null, hostingEnvironment); var requestDelegate = new RequestDelegate(startup.HandleRequest); var server = new KestrelServer(new KestrelServerOptionsConfig(container, 5000), new SocketTransportFactory(new SocketTransportOptionsConfig(), new ApplicationLifetime(), defaultEventSourceLoggerFactory), defaultEventSourceLoggerFactory); await server.StartAsync(new HttpApplication(requestDelegate), CancellationToken.None); Thread.Sleep(Timeout.Infinite); }
public void GetObject() { var uc = new QuickInjectContainer(); object obj = uc.Resolve <object>(); Assert.IsNotNull(obj); }
public void ComplicatedRegistrationsWithChildContainerLifetimes2() { var container = new QuickInjectContainer(); container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor()); var child = container.CreateChildContainer(); var correctInstanceForIFooResolutionFromChild = new Foo(); var correctInstanceForFooResolutionFromChild = new SuperFoo(); var preSetFooOnLifetime = new Foo(); SuperFoo fooResolvedFromMainContainer = new SuperFoo(); var lifetime = new ContainerControlledLifetimeManager(); lifetime.SetValue(fooResolvedFromMainContainer); container.RegisterType <IFoo, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => new Foo())); container.RegisterType <IBar, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => correctInstanceForIFooResolutionFromChild)); container.RegisterType <Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => fooResolvedFromMainContainer)); container.RegisterType <SuperFoo>(lifetime); child.RegisterType <Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => correctInstanceForFooResolutionFromChild)); var f = container.Resolve <Foo>(); var g = container.Resolve <Foo>(); Assert.AreSame(child.Resolve <IBar>(), correctInstanceForIFooResolutionFromChild); Assert.AreSame(child.Resolve <IFoo>(), correctInstanceForIFooResolutionFromChild); Assert.AreSame(child.Resolve <Foo>(), correctInstanceForFooResolutionFromChild); Assert.AreSame(container.Resolve <Foo>(), fooResolvedFromMainContainer); Assert.AreSame(container.Resolve <SuperFoo>(), fooResolvedFromMainContainer); }
public void ChildContainerOverridingARegistrationGetsADifferentPlan2() { var container = new QuickInjectContainer(); container.RegisterType <IA>(new ParameterizedLambdaExpressionInjectionFactory <C, IA>(new GetACodeProvider())); var childContainer = container.CreateChildContainer(); childContainer.RegisterType <IA>(new ParameterizedLambdaExpressionInjectionFactory <C, IA>(new GetA2CodeProvider())); var f = container.Resolve <F>(); var f2 = childContainer.Resolve <F>(); Assert.AreEqual(42, f.Value.Value); Assert.AreEqual(44, f2.Value.Value); container.SealContainer(); GC.Collect(); f = container.Resolve <F>(); f2 = childContainer.Resolve <F>(); container.SealContainer(); GC.Collect(); container.Resolve <F>(); childContainer.Resolve <F>(); Assert.AreEqual(42, f.Value.Value); Assert.AreEqual(44, f2.Value.Value); }
public void WhenInstanceIsRegisteredAsSingletonEnsureItIsNotGarbageCollected() { ITest iTest; BTest objB = new BTest(); var uc1 = new QuickInjectContainer(); uc1.RegisterType <ITest, ATest>(); iTest = objB; uc1.RegisterInstance <ITest>(iTest); iTest = (ITest)uc1.Resolve(typeof(ITest)); Assert.IsNotNull(iTest); iTest = null; GC.Collect(); iTest = (ITest)uc1.Resolve(typeof(ITest)); Assert.IsNotNull(iTest); iTest = (ITest)uc1.Resolve(typeof(ITest)); Assert.IsNotNull(iTest); }
public override void PrepareBasic() { this.container = new QuickInjectContainer(); this.RegisterDummies(); this.RegisterStandard(); this.RegisterComplex(); }
public void TwoInstancesAreNotSame() { var uc = new QuickInjectContainer(); object obj1 = uc.Resolve <object>(); object obj2 = uc.Resolve <object>(); Assert.AreNotSame(obj1, obj2); }
public void RecursiveDependencies() { IQuickInjectContainer uc = new QuickInjectContainer(); object obj1 = uc.Resolve <MyDependency>(); Assert.IsNotNull(obj1); Assert.IsInstanceOfType(obj1, typeof(MyDependency)); }
public void SingletonsAreSame() { IQuickInjectContainer uc = new QuickInjectContainer() .RegisterType <object>(new ContainerControlledLifetimeManager()); object obj1 = uc.Resolve <object>(); object obj2 = uc.Resolve <object>(); Assert.AreSame(obj1, obj2); Assert.IsInstanceOfType(obj1.GetType(), typeof(object)); }
public void ValidateUniqueInstancesOfSameType() { var container = new QuickInjectContainer(); container.SealContainer(); var b = container.Resolve <B>(); Assert.AreNotEqual(b.A1, b.A2); }
public void SetLifetimeGetTwice() { IQuickInjectContainer uc = new QuickInjectContainer(); uc.RegisterType <A>(new ContainerControlledLifetimeManager()); A obj = uc.Resolve <A>(); A obj1 = uc.Resolve <A>(); Assert.AreSame(obj, obj1); }
public void TwoInterfacesMappedToSameConcreteTypeGetSameInstance() { IQuickInjectContainer container = new QuickInjectContainer(); container.RegisterType <IFoo, Foo>(new ContainerControlledLifetimeManager()); container.RegisterType <IBar, Foo>(); var foo = container.Resolve <IFoo>(); var bar = container.Resolve <IBar>(); Assert.AreSame(foo, bar); }
public void ContainerReturnsTheSameInstanceOnTheSameThread() { IQuickInjectContainer container = new QuickInjectContainer(); container.RegisterType <IHaveManyGenericTypesClosed, HaveManyGenericTypesClosed>(new PerThreadLifetimeManager()); IHaveManyGenericTypesClosed a = container.Resolve <IHaveManyGenericTypesClosed>(); IHaveManyGenericTypesClosed b = container.Resolve <IHaveManyGenericTypesClosed>(); Assert.AreSame(a, b); }
public override void Dispose() { // Allow the container and everything it references to be garbage collected. if (this.container == null) { return; } this.container.Dispose(); this.container = null; }
private static QuickInjectContainer Configure() { var container = new QuickInjectContainer(); container.RegisterType <IRecordContext <Record>, ShazamContext>(new TransientLifetimeManager()); container.RegisterType <IRecordService, RecordService>(new TransientLifetimeManager()); container.RegisterType <SaveEndpointHandler>(); container.RegisterType <GetRecordsEndpointHandler>(); return(container); }
public void ResolutionMonitorIsCalledForContainer() { var resolutionMonitor = new AssertingResolutionMonitor(typeof(IA)); var container = new QuickInjectContainer(new NoOpCompilationMonitor(), resolutionMonitor); container.RegisterType <IA, A>(); container.Resolve <IA>(); Assert.IsTrue(resolutionMonitor.BeginWasCalled); Assert.IsTrue(resolutionMonitor.EndWasCalled); }
public void SetSingletonRegisterInstanceTwice() { IQuickInjectContainer uc = new QuickInjectContainer(); A aInstance = new A(); uc.RegisterInstance <A>(aInstance).RegisterInstance <A>(aInstance); A obj = uc.Resolve <A>(); A obj1 = uc.Resolve <A>(); Assert.AreSame(obj, obj1); }
public void RegisterTypeOfMultiLevelParameterizedInjectionFactory() { var container = new QuickInjectContainer(); container.RegisterType <HttpContextBase>() .RegisterType <HttpRequestBase>(new ParameterizedLambdaExpressionInjectionFactory <HttpContextBase, HttpRequestBase>(httpContext => httpContext.Request)) .RegisterType <HttpCookieCollection>(new ParameterizedLambdaExpressionInjectionFactory <HttpRequestBase, HttpCookieCollection>(httpRequest => httpRequest.Cookies)); var cookie = container.Resolve <HttpCookieCollectionUser>(); Assert.AreNotEqual(cookie, null); }
public void ValidateSetValueSimpleDefaultConstructor() { var container = new QuickInjectContainer(); var lifetimeManager = new TestLifetimeManager(); container.RegisterType <A>(lifetimeManager); container.SealContainer(); container.Resolve <A>(); // side-effect is that lifetimeManager should have the right value Assert.AreEqual(42, (lifetimeManager.GetValue() as A).Value); }
public void TestResolutionContext() { var container = new QuickInjectContainer(); container.RegisterTypeAsResolutionContext <A>(); var context = new A(); var b = container.Resolve <B>(context); Assert.AreSame(b.A1, b.A2); Assert.AreSame(b.A1, context); }
public void ParameterizedCodeProviderReturnsInstanceThroughItsFactory() { var container = new QuickInjectContainer(); var lifetimeManager = new TestLifetimeManager(); container.RegisterType <C>(lifetimeManager); container.RegisterType <IA>(new ParameterizedLambdaExpressionInjectionFactory <C, IA>(new GetACodeProvider())); container.SealContainer(); var ia = container.Resolve <IA>(); Assert.AreSame((lifetimeManager.GetValue() as C).PropToVerify, ia); }
public void UseExternallyControlledLifetimeResolve() { IQuickInjectContainer parentuc = new QuickInjectContainer(); parentuc.RegisterType <UnityTestClass>(new ExternallyControlledLifetimeManager()); UnityTestClass parentinstance = parentuc.Resolve <UnityTestClass>(); parentinstance.Name = "Hello World Ob1"; UnityTestClass parentinstance1 = parentuc.Resolve <UnityTestClass>(); Assert.AreSame(parentinstance.Name, parentinstance1.Name); }
public void TestSealContainer() { var container = new QuickInjectContainer(); var lifetimeManager = new SynchronizedTestLifetimeManager(); container.RegisterType <IA>(new ParameterizedLambdaExpressionInjectionFactory <C, IA>(new ExceptionThrowingCodeProvider())); container.SealContainer(); container.RegisterType <F>(lifetimeManager); container.Resolve <F>(); }
public void SetValueCallsArePreservedWhenTransientLifetimeRemovalRuns() { var container = new QuickInjectContainer(); container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor()); container.RegisterType <IFoo, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => new Foo())); container.RegisterType <IBar, Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => new Foo())); var a = container.Resolve <IFoo>(); var b = container.Resolve <IBar>(); Assert.AreSame(a, b); }
public void DoNotThrowOnUnconstructableTypeIfLifetimeManagerIsSet() { var container = new QuickInjectContainer(); var lifetimeManager = new TestLifetimeManager(); container.RegisterType <IA>(lifetimeManager); container.SealContainer(); var a = new A(); lifetimeManager.SetValue(a); Assert.AreSame(a, container.Resolve <IA>()); }
public void ParameterizedCodeProviderReturnsInstanceThroughItsFactoryInstance() { var container = new QuickInjectContainer(); var c = new C(new B(new A(), new A()), new A()); container.RegisterInstance(c); container.RegisterType <IA>(new ParameterizedLambdaExpressionInjectionFactory <C, IA>(new GetACodeProvider())); container.SealContainer(); var ia = container.Resolve <IA>(); Assert.AreSame(c.PropToVerify, ia); }
public void ChildContainerOverridingARegistrationGetsADifferentPlan() { var container = new QuickInjectContainer(); container.RegisterType <IA>(new ParameterizedLambdaExpressionInjectionFactory <C, IA>(new GetACodeProvider())); var childContainer = container.CreateChildContainer(); childContainer.RegisterType <A, A2>(); var b = container.Resolve <B>(); var b2 = childContainer.Resolve <B>(); Assert.AreEqual(42, b.A1.Value); Assert.AreEqual(44, b2.A1.Value); }
public void LifetimeManagerWillProvideValueForAnInterfaceType() { var container = new QuickInjectContainer(); container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor()); var lifetime = new ContainerControlledLifetimeManager(); var foo = new Foo(); lifetime.SetValue(foo); container.RegisterType <IFoo>(lifetime); var a = container.Resolve <IFoo>(); Assert.AreSame(a, foo); }
public void ReregistrationOfATypeUpdatesBuildCache() { var container = new QuickInjectContainer(); container.AddBuildPlanVisitor(new LifetimeManagerRequiresRecoveryBuildPlanVisitor()); var foo = new Foo(); container.RegisterType <Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => foo)); var cfoo = container.Resolve <ConsumesFoo>(); container.RegisterType <Foo>(new ContainerControlledLifetimeManager(), new ParameterizedLambdaExpressionInjectionFactory <Foo>(() => new Foo())); var cfoo2 = container.Resolve <ConsumesFoo>(); Assert.AreNotSame(cfoo.Foo, cfoo2.Foo); }