コード例 #1
0
        public void InterfaceReRegisteredFromInstanceToInstance_Success()
        {
            var c = new Container();
            IEmptyClass emptyClass = new EmptyClass();
            c.RegisterInstance(emptyClass).AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result1 = controller.ResolveObject<IEmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass1 = (IEmptyClass)((ViewResult)result1).Model;
            var result2 = controller.ResolveObject<IEmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass2 = (IEmptyClass)((ViewResult)result2).Model;

            IEmptyClass emptyClass3 = new EmptyClass();
            c.RegisterInstance(emptyClass3).AsPerHttpContext();
            var result4 = controller.ResolveObject<IEmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass4 = (IEmptyClass)((ViewResult)result4).Model;
            var result5 = controller.ResolveObject<IEmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass5 = (IEmptyClass)((ViewResult)result5).Model;

            Assert.AreEqual(emptyClass, emptyClass1);
            Assert.AreEqual(emptyClass1, emptyClass2);
            Assert.AreEqual(emptyClass3, emptyClass4);
            Assert.AreEqual(emptyClass4, emptyClass5);
            Assert.AreNotEqual(emptyClass, emptyClass3);
        }
        public void NestedFactoryObjectReturnNewObject_Success()
        {
            var c = new Container();
            c.RegisterType<IEmptyClass>(() => new EmptyClass()).AsPerHttpContext();
            c.RegisterType<ISampleClassWithInterfaceAsParameter, SampleClassWithInterfaceAsParameter>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result1 = controller.ResolveObject<ISampleClassWithInterfaceAsParameter>(c, ResolveKind.PartialEmitFunction);
            var sampleClass1 = (ISampleClassWithInterfaceAsParameter)((ViewResult)result1).Model;
            var result2 = controller.ResolveObject<ISampleClassWithInterfaceAsParameter>(c, ResolveKind.PartialEmitFunction);
            var sampleClass2 = (ISampleClassWithInterfaceAsParameter)((ViewResult)result2).Model;

            Assert.AreEqual(sampleClass1, sampleClass2);
            Assert.AreEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
        }
コード例 #3
0
        public void DifferentHttpContext_RegisterClass_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerHttpContext();
            c.RegisterType<SampleClass>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result1 = controller.ResolveObject<SampleClass>(c, ResolveKind.PartialEmitFunction);
            var sampleClass1 = (SampleClass)((ViewResult)result1).Model;
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result2 = controller.ResolveObject<SampleClass>(c, ResolveKind.PartialEmitFunction);
            var sampleClass2 = (SampleClass)((ViewResult)result2).Model;

            Assert.IsNotNull(sampleClass1);
            Assert.IsNotNull(sampleClass1.EmptyClass);
            Assert.IsNotNull(sampleClass2);
            Assert.IsNotNull(sampleClass2.EmptyClass);
            Assert.AreNotEqual(sampleClass1, sampleClass2);
            Assert.AreNotEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
        }
        public void RegisteredInterfaceAsClassWithTwoConstructorsWithAttributeDependencyConstrutor_Fail()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerHttpContext();
            c.RegisterType<ISampleClass, SampleClassWithTwoDependencyConstrutor>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result = controller.ResolveObject<ISampleClass>(c, ResolveKind.PartialEmitFunction);
            var sampleClass = (ISampleClass)((ViewResult)result).Model;

            Assert.IsNull(sampleClass);
        }
コード例 #5
0
        public void DifferentThreads_RegisterManyGenericClasses_Success()
        {
            var c = new Container();
            c.RegisterType<IEmptyClass, EmptyClass>().AsPerHttpContext();
            c.RegisterType<ISampleClassWithInterfaceAsParameter, SampleClassWithInterfaceAsParameter>().AsPerHttpContext();
            c.RegisterType<IGenericClass<IEmptyClass>, GenericClass<IEmptyClass>>().AsPerHttpContext();
            c.RegisterType<IGenericClass<ISampleClassWithInterfaceAsParameter>, GenericClass<ISampleClassWithInterfaceAsParameter>>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result1 = controller.ResolveObject<IGenericClass<IEmptyClass>>(c, ResolveKind.PartialEmitFunction);
            var genericClass1 = (IGenericClass<IEmptyClass>)((ViewResult)result1).Model;

            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result2 = controller.ResolveObject<IGenericClass<ISampleClassWithInterfaceAsParameter>>(c, ResolveKind.PartialEmitFunction);
            var genericClass2 = (IGenericClass<ISampleClassWithInterfaceAsParameter>)((ViewResult)result2).Model;

            Assert.AreNotEqual(genericClass1, genericClass2);
            Assert.AreNotEqual(genericClass1.GetType(), genericClass2.GetType());
            Assert.AreNotEqual(genericClass1.NestedClass, genericClass2.NestedClass.EmptyClass);
            Assert.AreEqual(genericClass1.NestedClass.GetType(), genericClass2.NestedClass.EmptyClass.GetType());
        }
コード例 #6
0
        public void ClassReRegisteredFromClassToTheSameClass_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result1 = controller.ResolveObject<EmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass1 = (EmptyClass)((ViewResult)result1).Model;
            var result2 = controller.ResolveObject<EmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass2 = (EmptyClass)((ViewResult)result2).Model;

            c.RegisterType<EmptyClass>().AsPerHttpContext();
            var result3 = controller.ResolveObject<EmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass3 = (EmptyClass)((ViewResult)result3).Model;
            var result4 = controller.ResolveObject<EmptyClass>(c, ResolveKind.PartialEmitFunction);
            var emptyClass4 = (EmptyClass)((ViewResult)result4).Model;

            Assert.AreEqual(emptyClass1, emptyClass2);
            Assert.AreEqual(emptyClass3, emptyClass4);
            Assert.AreNotEqual(emptyClass1, emptyClass3);
        }
        public void RegisterClassWithNestedInterfaceAsParameterWithInterfaceAsParameterAndWithConstructorWithAttributeDependencyConstrutor_Success()
        {
            var c = new Container();
            c.RegisterType<IEmptyClass, EmptyClass>().AsPerHttpContext();
            c.RegisterType<ISampleClassWithInterfaceAsParameter, SampleClassWithInterfaceAsParameterWithDependencyConstrutor>().AsPerHttpContext();
            c.RegisterType<SampleClassWithNestedInterfaceAsParameterWithDependencyConstrutor>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result = controller.ResolveObject<SampleClassWithNestedInterfaceAsParameterWithDependencyConstrutor>(c, ResolveKind.PartialEmitFunction);
            var sampleClass = (SampleClassWithNestedInterfaceAsParameterWithDependencyConstrutor)((ViewResult)result).Model;

            Assert.IsNotNull(sampleClass);
            Assert.IsNotNull(sampleClass.SampleClassWithInterfaceAsParameter);
            Assert.IsNotNull(sampleClass.SampleClassWithInterfaceAsParameter.EmptyClass);
        }
コード例 #8
0
        public void RegisterGenericClassWithClassWithNestedClass_Success()
        {
            var c = new Container();
            c.RegisterType<IEmptyClass, EmptyClass>().AsPerHttpContext();
            c.RegisterType<ISampleClassWithInterfaceAsParameter, SampleClassWithInterfaceAsParameter>().AsPerHttpContext();
            c.RegisterType<IGenericClass<ISampleClassWithInterfaceAsParameter>, GenericClass<ISampleClassWithInterfaceAsParameter>>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result = controller.ResolveObject<IGenericClass<ISampleClassWithInterfaceAsParameter>>(c, ResolveKind.PartialEmitFunction);
            var genericClass = (IGenericClass<ISampleClassWithInterfaceAsParameter>)((ViewResult)result).Model;

            Assert.IsNotNull(genericClass);
            Assert.IsNotNull(genericClass.NestedClass);
            Assert.IsNotNull(genericClass.NestedClass.EmptyClass);
        }
コード例 #9
0
        public void RegisterGenericClassWithManyParameters_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerHttpContext();
            c.RegisterType<SampleClass>().AsPerHttpContext();
            c.RegisterType<GenericClassWithManyParameters<EmptyClass, SampleClass>>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result = controller.ResolveObject<GenericClassWithManyParameters<EmptyClass, SampleClass>>(c, ResolveKind.FullEmitFunction);
            var genericClass = (GenericClassWithManyParameters<EmptyClass, SampleClass>)((ViewResult)result).Model;

            Assert.IsNotNull(genericClass);
            Assert.IsNotNull(genericClass.NestedClass1);
            Assert.IsNotNull(genericClass.NestedClass2);
            Assert.IsNotNull(genericClass.NestedClass2.EmptyClass);
            Assert.AreEqual(genericClass.NestedClass1, genericClass.NestedClass2.EmptyClass);
        }
コード例 #10
0
        public void InternalIntTypeNotRegistered_Fail()
        {
            var c = new Container();
            c.RegisterType<SampleClassWithIntType>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result = controller.ResolveObject<SampleClassWithIntType>(c, ResolveKind.PartialEmitFunction);
            var sampleClassWithSimpleType = (SampleClassWithIntType)((ViewResult)result).Model;

            Assert.IsNull(sampleClassWithSimpleType);
        }
コード例 #11
0
        public void RegisterClassWithCycleInConstructor_Fail()
        {
            var c = new Container();
            c.RegisterType<SecondClassWithCycleInConstructor>().AsPerHttpContext();
            c.RegisterType<FirstClassWithCycleInConstructor>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result = controller.ResolveObject<FirstClassWithCycleInConstructor>(c, ResolveKind.PartialEmitFunction);
            var sampleClass = (FirstClassWithCycleInConstructor)((ViewResult)result).Model;

            Assert.IsNull(sampleClass);
        }
コード例 #12
0
        public void RegisterClassWithConstructorWithoutParameters_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result = controller.ResolveObject<EmptyClass>(c, ResolveKind.FullEmitFunction);
            var emptyClass = (EmptyClass)((ViewResult)result).Model;

            Assert.IsNotNull(emptyClass);
        }
コード例 #13
0
        public void SameThread_RegisterManyGenericClasses_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerHttpContext();
            c.RegisterType<SampleClass>().AsPerHttpContext();
            c.RegisterType<GenericClass<EmptyClass>>().AsPerHttpContext();
            c.RegisterType<GenericClass<SampleClass>>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result1 = controller.ResolveObject<GenericClass<EmptyClass>>(c, ResolveKind.FullEmitFunction);
            var genericClass1 = (GenericClass<EmptyClass>)((ViewResult)result1).Model;
            var result2 = controller.ResolveObject<GenericClass<SampleClass>>(c, ResolveKind.FullEmitFunction);
            var genericClass2 = (GenericClass<SampleClass>)((ViewResult)result2).Model;

            Assert.AreNotEqual(genericClass1, genericClass2);
            Assert.AreNotEqual(genericClass1.GetType(), genericClass2.GetType());
            Assert.AreEqual(genericClass1.NestedClass, genericClass2.NestedClass.EmptyClass);
            Assert.AreEqual(genericClass1.NestedClass.GetType(), genericClass2.NestedClass.EmptyClass.GetType());
        }
コード例 #14
0
        public void InterfaceReRegisteredFromOneClassToTheOther_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerHttpContext();
            c.RegisterType<ISampleClass, SampleClass>().AsPerHttpContext();

            var controller = new DefaultController();
            HttpContext.Current = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            var result1 = controller.ResolveObject<ISampleClass>(c, ResolveKind.PartialEmitFunction);
            var sampleClass1 = (ISampleClass)((ViewResult)result1).Model;
            var result2 = controller.ResolveObject<ISampleClass>(c, ResolveKind.PartialEmitFunction);
            var sampleClass2 = (ISampleClass)((ViewResult)result2).Model;

            c.RegisterType<ISampleClass, SampleClassOther>().AsPerHttpContext();
            var result3 = controller.ResolveObject<ISampleClass>(c, ResolveKind.PartialEmitFunction);
            var sampleClass3 = (ISampleClass)((ViewResult)result3).Model;
            var result4 = controller.ResolveObject<ISampleClass>(c, ResolveKind.PartialEmitFunction);
            var sampleClass4 = (ISampleClass)((ViewResult)result4).Model;

            Assert.AreEqual(sampleClass1, sampleClass2);
            Assert.AreEqual(sampleClass1.GetType(), sampleClass2.GetType());
            Assert.AreEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
            Assert.AreEqual(sampleClass3, sampleClass4);
            Assert.AreEqual(sampleClass3.GetType(), sampleClass4.GetType());
            Assert.AreEqual(sampleClass3.EmptyClass, sampleClass4.EmptyClass);
            Assert.AreNotEqual(sampleClass1, sampleClass3);
            Assert.AreNotEqual(sampleClass1.GetType(), sampleClass3.GetType());
            Assert.AreEqual(sampleClass1.EmptyClass, sampleClass3.EmptyClass);
        }