public void TestComplexNamedDependency()
        {
            logger.Trace("Entering TestComplexNamedDependency Test");
            var dependencies = new DIConfiguration();

            dependencies.Register <TDependency, TComplexNamedImplementation>(true);
            dependencies.Register <IAnimal, Dog>(true);
            dependencies.Register <IVehicle, Car>(true, ImplementationName.First);
            dependencies.Register <IVehicle, Bike>(true, ImplementationName.Second);
            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            var complexImplementation = (TComplexNamedImplementation)provider.Resolve <TDependency>();

            logger.Trace("Received resolve object");

            try
            {
                Assert.AreEqual(1, complexImplementation.vehicles.Count);
                Assert.That(complexImplementation.vehicles[0], Is.InstanceOf <Car>());
                Assert.AreEqual(typeof(Dog), complexImplementation.animal.GetType());
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }
        }
        public void TestNamedImplementation()
        {
            logger.Trace("Entering TestNamedImplementation Test");
            var dependencies = new DIConfiguration();

            dependencies.Register <IVehicle, Car>(true, ImplementationName.First);
            dependencies.Register <IVehicle, Bike>(true, ImplementationName.Second);

            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            var objCar = provider.Resolve <IVehicle>(ImplementationName.First);

            logger.Trace("Received resolve object 1");
            var objBike = provider.Resolve <IVehicle>(ImplementationName.Second);

            logger.Trace("Received resolve object 2");

            try
            {
                Assert.That(objCar, Is.InstanceOf(typeof(Car)));
                Assert.That(objBike, Is.InstanceOf(typeof(Bike)));
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }
        }
        public void TestOpenGenericDependencies()
        {
            logger.Trace("Entering TestOpenGenericDependencies Test");
            var dependencies = new DIConfiguration();

            dependencies.Register(typeof(IService <>), typeof(ServiceImpl <>));
            dependencies.Register <TDependency, TImplementation>(true);
            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            var genericObject = (ServiceImpl <TDependency>)provider.Resolve <IService <TDependency> >();

            logger.Trace("Received resolve object");

            try
            {
                Assert.AreEqual(genericObject.repository.GetType(), typeof(TImplementation));
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }
        }
        public void TestResolvingMultipleImplementations()
        {
            logger.Trace("Entering TestResolvingMultipleImplementations Test");
            var dependencies = new DIConfiguration();

            dependencies.Register <IVehicle, Car>(true);
            dependencies.Register <IVehicle, Bike>(true);
            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            var resolved = provider.Resolve <IEnumerable <IVehicle> >() as List <object>;

            logger.Trace("Received resolve object");


            try
            {
                Assert.AreEqual(resolved.Count, 2);

                Type[] actual   = new Type[] { resolved[0].GetType(), resolved[1].GetType() };
                Type[] expected = new Type[] { typeof(Car), typeof(Bike) };

                CollectionAssert.AreEquivalent(actual, expected);
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }
        }
Ejemplo n.º 5
0
        public void ResolveGenType()
        {
            var conf = new DIConfiguration();

            conf.Register <IBar, BarFromABar>();
            conf.Register(typeof(IGenBar <IBar>), typeof(IGenBar <IBar>));

            var container = new DIContainer(conf);

            IGenBar <IBar> ogen = container.Resolve <IGenBar <IBar> >();

            Assert.IsNotNull(ogen);
        }
Ejemplo n.º 6
0
        public void DependencyInjectionThroughConstructor()
        {
            var conf = new DIConfiguration();

            conf.Register <IBar, Bar>();
            conf.Register <IFoo, Foo>();

            var container = new DIContainer(conf);

            var foo = container.Resolve <IFoo>();

            Assert.IsNotNull(foo.Bar);
        }
Ejemplo n.º 7
0
        public void RegisterClass()
        {
            DIConfiguration conf = new DIConfiguration();

            conf.Register <ABar, BarFromABar>();
            conf.Register <IFoo, Foo>();
            conf.Register <Foo>();

            DIContainer container = new DIContainer(conf);

            Foo foo = container.Resolve <Foo>();

            Assert.IsNotNull(foo);
        }
Ejemplo n.º 8
0
        public void ResolveOpenGenType()
        {
            DIConfiguration conf = new DIConfiguration();

            conf.Register <IBar, BarFromABar>();
            conf.Register <IFoo, Foo>();
            conf.Register(typeof(IGenFoo <>), typeof(IGenFoo <>));

            DIContainer container = new DIContainer(conf);

            var genFoo = container.Resolve <IGenFoo <IFoo> >();

            Assert.IsNotNull(genFoo);
        }
Ejemplo n.º 9
0
        public void CorrectCreateDependency()
        {
            DIConfiguration conf = new DIConfiguration();

            conf.Register <IBar, BarFromIBar>();
            conf.Register <ABar, BarFromABar>();
            conf.Register <Foo, Foo>();

            DIContainer container = new DIContainer(conf);

            Foo foo = container.Resolve <Foo>();

            Assert.IsNotNull(foo.Bar);
        }
Ejemplo n.º 10
0
        public void ResolveEnumerable()
        {
            var expected = 2;
            var conf     = new DIConfiguration();

            conf.Register <IBar, Bar>();
            conf.Register <IBar, AnotherBar>();

            var container = new DIContainer(conf);

            var bars = container.Resolve <IEnumerable <IBar> >();

            Assert.AreEqual(expected, bars.Count());
        }
Ejemplo n.º 11
0
        public void ResolveEnumerable()
        {
            int             expected = 2;
            DIConfiguration conf     = new DIConfiguration();

            conf.Register <IBar, BarFromIBar>();
            conf.Register <IBar, BarFromABar>();

            DIContainer container = new DIContainer(conf);

            var bars = container.ResolveAll <IBar>();

            Assert.AreEqual(expected, bars.Count());
        }
Ejemplo n.º 12
0
        public void RegisterClassAsSelf()
        {
            var conf = new DIConfiguration();

            conf.Register <IBar, Bar>();
            conf.Register <IFoo, Foo>();
            conf.Register <Service>();

            var container = new DIContainer(conf);

            var service = container.Resolve <Service>();

            Assert.IsNotNull(service);
        }
Ejemplo n.º 13
0
        public void ResolveOpenGenericType()
        {
            var conf = new DIConfiguration();

            conf.Register <IBar, Bar>().AsSingleton();
            conf.Register <IFoo, Foo>().InstancePerRequest();
            conf.Register <IService, Service>();
            conf.Register(typeof(IServiceGeneric <>), typeof(ServiceGeneric <>));

            var container = new DIContainer(conf);

            var serviceGeneric = container.Resolve <IServiceGeneric <IService> >();

            Assert.IsNotNull(serviceGeneric.Service);
        }
Ejemplo n.º 14
0
        public void WhyThisWork()
        {
            var conf = new DIConfiguration();

            conf.Register <IBar, Bar>().AsSingleton();
            conf.Register <IFoo, Foo>().InstancePerRequest();
            conf.Register <IService, Service>();
            conf.Register(typeof(IServiceGeneric <>), typeof(ServiceGeneric <>));

            var container = new DIContainer(conf);

            var serviceGeneric = container.Resolve <IServiceGeneric <IService> >();

            //При получении конструктора возвращает конструктор класса Service?
            var serviceGeneric2 = container.Resolve <IServiceGeneric <ServiceBase> >();
        }
        public void TestExceptionSituations()
        {
            logger.Trace("Entering TestExceptionSituations Test");
            var dependencies = new DIConfiguration();

            try
            {
                logger.Trace("Trying register dependency");
                Assert.Throws <ArgumentException>(delegate { dependencies.Register <TDependency, TDependency>(true); });
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }

            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            try
            {
                logger.Trace("Trying receive resolve object");
                Assert.Throws <KeyNotFoundException> (delegate { var genericObject = (ServiceImpl <TDependency>)provider.Resolve <IService <TDependency> >(); });
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }
        }
        public void TestSingletonDependencies()
        {
            logger.Trace("Entering TestSingletonDependencies Test");
            var dependencies = new DIConfiguration();

            dependencies.Register <TDependency, TImplementation>(true);
            dependencies.Register <IAnimal, Dog>(false);
            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            var firstObject = provider.Resolve <TDependency>();

            logger.Trace("Received resolve object 1");
            var secondObject = provider.Resolve <TDependency>();

            logger.Trace("Received resolve object 2");

            try
            {
                Assert.AreSame(firstObject, secondObject);
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }

            var thirdObject = provider.Resolve <IAnimal>();

            logger.Trace("Received resolve object 3");
            var fourthObject = provider.Resolve <IAnimal>();

            logger.Trace("Received resolve object 4");

            try
            {
                Assert.AreNotSame(thirdObject, fourthObject);
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }
        }
Ejemplo n.º 17
0
        public void CyclicDependency()
        {
            var conf = new DIConfiguration();

            conf.Register <IFoo, Foo>();
            conf.Register <IBar, CycBar>();

            var container = new DIContainer(conf);

            try
            {
                var bar = container.Resolve <IBar>();
                Assert.Fail("Cannot be cyclic dependencies");
            }
            catch (Exception e)
            {
                Assert.IsTrue(true, e.Message);
            }
        }
Ejemplo n.º 18
0
        public void InsolvableCreation()
        {
            DIConfiguration conf = new DIConfiguration();

            conf.Register <IFoo, Foo>();
            conf.Register <IBar, BarFromIBar>();

            DIContainer container = new DIContainer(conf);

            try
            {
                IBar bar = container.Resolve <IBar>();
                Assert.Fail("Cannot be created");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e, e.Message);
            }
        }
Ejemplo n.º 19
0
        public void AbstractClassDependency()
        {
            var conf = new DIConfiguration();

            conf.Register <BarBase, AbstrBar>();

            var container = new DIContainer(conf);

            var bar = container.Resolve <BarBase>();

            Assert.IsNotNull(bar);
        }
        public void TestDuplicateImplementations()
        {
            logger.Trace("Entering TestDuplicateImplementations Test");
            var dependencies = new DIConfiguration();

            dependencies.Register <TDependency, TImplementation>(true);
            dependencies.Register <TDependency, TImplementation>(true);
            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            try
            {
                Assert.AreEqual(1, dependencies.dependenciesContainer[typeof(TDependency)].Count);
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }
        }
        public void TestRecursiveDependencies()
        {
            logger.Trace("Entering TestRecursiveDependencies Test");
            var dependencies = new DIConfiguration();

            dependencies.Register <TDependency, TComplexImplementation>(true);
            dependencies.Register <IAnimal, Dog>(true);
            dependencies.Register <IVehicle, Car>(true);
            dependencies.Register <IVehicle, Bike>(true);
            var provider = new DIContainer(dependencies);

            logger.Trace("Container created");

            var complexImplementation = (TComplexImplementation)provider.Resolve <TDependency>();

            logger.Trace("Received resolve object");

            try
            {
                // Check that IEnumerable<IVehicle> dependencies are created.
                Type[] actual   = new Type[] { complexImplementation.vehicles[0].GetType(), complexImplementation.vehicles[1].GetType() };
                Type[] expected = new Type[] { typeof(Car), typeof(Bike) };

                CollectionAssert.AreEquivalent(actual, expected);

                // Check that IAnimal dependency is created.
                Assert.AreEqual(typeof(Dog), complexImplementation.animal.GetType());
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                throw;
            }

            logger.Trace("Testing created Object");
            Mock <Dog> dependencyMock = new Mock <Dog>();

            dependencyMock.Setup(x => x.DoNothing()).Returns(true);
            (complexImplementation as TComplexImplementation).DoNothing(dependencyMock.Object);
        }
Ejemplo n.º 22
0
        public void IsSingleton()
        {
            var conf = new DIConfiguration();

            conf.Register <IBar, Bar>().AsSingleton();

            var container = new DIContainer(conf);

            var bar1 = container.Resolve <IBar>();
            var bar2 = container.Resolve <IBar>();

            Assert.AreEqual(bar1, bar2);
        }
Ejemplo n.º 23
0
        public void IsNotSingleton()
        {
            DIConfiguration conf = new DIConfiguration();

            conf.Register <IBar, BarFromABar>();

            DIContainer container = new DIContainer(conf);

            IBar bar1 = container.Resolve <IBar>();
            IBar bar2 = container.Resolve <IBar>();

            Assert.AreNotEqual(bar1, bar2);
        }
Ejemplo n.º 24
0
        public void IsNotSingleton()
        {
            var conf = new DIConfiguration();

            conf.Register <IBar, Bar>().InstancePerRequest();

            var container = new DIContainer(conf);

            var bar1 = container.Resolve <IBar>();
            var bar2 = container.Resolve <IBar>();

            Assert.AreNotEqual(bar1, bar2);
        }
Ejemplo n.º 25
0
        public void AbstractClassAdded()
        {
            DIConfiguration conf = new DIConfiguration();

            try
            {
                conf.Register <ABar, ABar>();
                Assert.Fail("registered class is abstract");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e, e.Message);
            }
        }
Ejemplo n.º 26
0
        public void RegisterAbstractClassAsSelf()
        {
            try
            {
                var conf = new DIConfiguration();
                conf.Register <BarBase>();

                var container = new DIContainer(conf);

                var bar = container.Resolve <BarBase>();
                Assert.Fail("Cannot create instance of abstract class");
            }
            catch (Exception e)
            {
                Assert.IsTrue(true, e.Message);
            }
        }
Ejemplo n.º 27
0
        public void RegInterfaceInterface()
        {
            try
            {
                var conf = new DIConfiguration();
                conf.Register <IBar>();

                var container = new DIContainer(conf);

                var bar = container.Resolve <IBar>();
                Assert.Fail("Cannot create instance of interface");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e, e.Message);
            }
        }
Ejemplo n.º 28
0
        public void RegisterAbstractClass()
        {
            try
            {
                DIConfiguration conf = new DIConfiguration();
                conf.Register <ABar>();

                DIContainer container = new DIContainer(conf);

                ABar bar = container.Resolve <ABar>();
                Assert.Fail("Cannot create instance of abstract class");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e, e.Message);
            }
        }
Ejemplo n.º 29
0
        public void NoSuchType()
        {
            DIConfiguration conf = new DIConfiguration();

            conf.Register <IFoo, Foo>();

            DIContainer container = new DIContainer(conf);

            try
            {
                IBar bar = container.Resolve <BarFromIBar>();
                Assert.Fail("Cannot create instance of not registered type");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e, e.Message);
            }
        }
Ejemplo n.º 30
0
        public void NotRegisteredType()
        {
            var conf = new DIConfiguration();

            conf.Register <IFoo, Foo>();

            var container = new DIContainer(conf);

            try
            {
                var bar = container.Resolve <IBar>();
                Assert.Fail("Cannot create instance of not registered type");
            }
            catch (Exception e)
            {
                Assert.IsTrue(true, e.Message);
            }
        }