Exemplo n.º 1
0
        public void MixedTest1()
        {
            MemoryStream memoryStream = new MemoryStream();

            IContainer container = new OdysseyContainer(new List <Registration>
            {
                new Registration(
                    typeof(ILogger),
                    typeof(Logger),
                    injections: new Injections(
                        constructorInjection: new ConstructorInjection(new List <ParameterInjection>()
                {
                    new ParameterInjection("memoryStream", memoryStream),
                }),
                        decoratorInjection: new DecoratorRegistration(
                            typeof(LoggerDecorator),
                            new Injections(
                                new ConstructorInjection(new List <ParameterInjection>()
                {
                    new ParameterInjection("text", "[Test]")
                })))))
            });

            ILogger service = (ILogger)container.Resolve(new Resolution(typeof(ILogger)));

            Assert.IsNotNull(service);
            Assert.IsInstanceOfType(service, typeof(LoggerDecorator));

            service.Log("Hello");

            memoryStream.Seek(0, SeekOrigin.Begin);
            TextReader textReader = new StreamReader(memoryStream);

            Assert.AreEqual("[Test]Hello", textReader.ReadToEnd());
        }
Exemplo n.º 2
0
        public void MinimalInstance()
        {
            IInterface originalService1 = new Implementation();
            IInterface originalService2 = new Implementation2();

            IContainer container = new OdysseyContainer(new List <Registration>
            {
                new Registration(typeof(IInterface), typeof(Implementation), instance: originalService1),
                new Registration(typeof(IInterface), typeof(Implementation), instance: originalService2, name: "This"),
            });

            IInterface service1 = (IInterface)container.Resolve(new Resolution(typeof(IInterface), name: "This"));
            IInterface service2 = (IInterface)container.Resolve(new Resolution(typeof(IInterface), name: "This"));
            IInterface service3 = (IInterface)container.Resolve(new Resolution(typeof(IInterface)));

            Assert.IsNotNull(service1);
            Assert.IsNotNull(service2);
            Assert.IsNotNull(service3);

            Assert.IsTrue(ReferenceEquals(service1, originalService2));
            Assert.IsTrue(ReferenceEquals(service2, originalService2));
            Assert.IsFalse(ReferenceEquals(service3, originalService2));
            Assert.IsTrue(ReferenceEquals(service3, originalService1));

            Assert.IsTrue(ReferenceEquals(service1, service2));
            Assert.IsFalse(ReferenceEquals(service1, service3));
        }
Exemplo n.º 3
0
        public void Minimal()
        {
            IContainer container = new OdysseyContainer(new List <Registration>
            {
                new Registration(typeof(IInterface), typeof(Implementation)),
            });

            IInterface service = (IInterface)container.Resolve(new Resolution(typeof(IInterface)));

            Assert.IsNotNull(service);
        }
Exemplo n.º 4
0
        public void MinimalCreateOnResolve()
        {
            IContainer container = new OdysseyContainer(new List <Registration>
            {
                new Registration(typeof(IInterface), typeof(Implementation), createOnResolve: true),
            });

            IInterface service1 = (IInterface)container.Resolve(new Resolution(typeof(IInterface)));
            IInterface service2 = (IInterface)container.Resolve(new Resolution(typeof(IInterface)));

            Assert.IsNotNull(service1);
            Assert.IsNotNull(service2);
            Assert.IsFalse(ReferenceEquals(service1, service2));
        }
Exemplo n.º 5
0
        public void Minimal()
        {
            IContainer container = new OdysseyContainer(new List <Registration>
            {
                new Registration(typeof(IInterface), typeof(Implementation)),
                new Registration(typeof(IInterface), typeof(Implementation2), name: "This"),
            });

            IInterface service1 = (IInterface)container.Resolve(new Resolution(typeof(IInterface), name: "This"));
            IInterface service2 = (IInterface)container.Resolve(new Resolution(typeof(IInterface), name: "This"));

            Assert.IsNotNull(service1);
            Assert.IsNotNull(service2);

            Assert.IsTrue(ReferenceEquals(service1, service2));
        }
Exemplo n.º 6
0
        public void Minimal()
        {
            IContainer container = new OdysseyContainer(new List <Registration>
            {
                new Registration(
                    typeof(IInterface),
                    typeof(Implementation),
                    injections: new Injections(new ConstructorInjection(new List <ParameterInjection>()
                {
                    new ParameterInjection("number", 2),
                }))),
            }, enableDebugMode: true);

            IInterface service = (IInterface)container.Resolve(new Resolution(typeof(IInterface)));

            Assert.IsNotNull(service);
            Assert.AreEqual(service.Number, 2);
        }