public void TestSerializeRespectedWithInheritance() { var obj = new SuperFoo { X = "abc", Z = "def", A =123, C = 456 }; // don't touch Y or B... string json = JsonSerializer.SerializeToString(obj); Assert.That(json, Is.StringMatching("{\"A\":123,\"C\":456,\"X\":\"abc\",\"Z\":\"def\"}")); }
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 Should_Handle_Callbacks_Contravariantly() { var foo = new SuperFoo(); var handler = new CustomHandler(); Assert.IsTrue(handler.Handle(foo)); Assert.IsTrue(foo.HasComposer); Assert.AreEqual(2, foo.Handled); }
public void TestSerializeRespectedWithInheritance() { var obj = new SuperFoo { X = "abc", Z = "def", A = 123, C = 456 }; // don't touch Y or B... string json = JsonSerializer.SerializeToString(obj); Assert.That(json, Is.StringMatching("{\"A\":123,\"C\":456,\"X\":\"abc\",\"Z\":\"def\"}")); }
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 Microsoft.Practices.Unity.InjectionFactory(c => new Foo())); container.RegisterType<IBar, Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => correctInstanceForIFooResolutionFromChild)); container.RegisterType<Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => fooResolvedFromMainContainer)); container.RegisterType<SuperFoo>(lifetime); child.RegisterType<Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => 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 bool?HandleSuperFooImplict(SuperFoo foo, IHandler composer) { ++foo.Handled; foo.HasComposer = true; return(null); }