public void LifetimeTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.RegisterSingleton <IServiceTwo, ServiceImplOne>();
            dependencies.Register <IRepository, RepositoryImplOne>();

            var provider  = new DependencyProvider(dependencies);
            var service1  = provider.Resolve <IServiceTwo>();
            var service2  = provider.Resolve <IServiceTwo>();
            var provider1 = provider;
            var service3  = Task.Run(() => provider1.Resolve <IServiceTwo>());

            Assert.IsTrue(Equals(service3.Result, service1) && Equals(service3.Result, service2) && Equals(service1, service2));

            dependencies = new DependencyConfiguration();
            dependencies.Register <IServiceTwo, ServiceImplOne>();
            dependencies.Register <IRepository, RepositoryImplOne>();

            provider = new DependencyProvider(dependencies);
            service1 = provider.Resolve <IServiceTwo>();
            service2 = provider.Resolve <IServiceTwo>();
            service3 = Task.Run(() => provider.Resolve <IServiceTwo>());
            Assert.IsFalse(Equals(service3.Result, service1) && Equals(service3.Result, service2) && Equals(service1, service2));
        }
Example #2
0
        public void ResolveGenTypeTest()
        {
            DependencyConfiguration configuration = new DependencyConfiguration();

            configuration.Register <IBar, BarFromABar>(false);
            configuration.Register(typeof(GenBar <IBar>), typeof(GenBar <IBar>), false);
            DependencyProvider provider = new DependencyProvider(configuration);
            GenBar <IBar>      ogen     = provider.Resolve <GenBar <IBar> >();

            Assert.IsNotNull(ogen);
        }
        public void OpenGenericDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register(typeof(IService <>), typeof(ServiceImpl <>));
            dependencies.Register <IRepository, RepositoryImpl1>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <IService <IRepository> >();

            Assert.AreEqual((service1 as ServiceImpl <IRepository>).TestObject(), "ServiceImpl<TRepository> with generic is created");
        }
        public void MultipleDependencyTestConstructor()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <IRepository, RepositoryImpl1>();
            dependencies.Register <IService, ServiceImpl3>();

            var provider = new DependencyProvider(dependencies);
            var service3 = provider.Resolve <IService>();

            Assert.AreEqual((service3 as ServiceImpl3).rep.ToArray()[0].TestObject(), "RepositoryImpl is created");
        }
Example #5
0
        public void ResolveOpenGenTypeTest()
        {
            DependencyConfiguration configuration = new DependencyConfiguration();

            configuration.Register <IBar, BarFromABar>(false);
            configuration.Register <IFoo, Foo>(false);
            configuration.Register(typeof(GenFoo <>), typeof(GenFoo <>), false);
            DependencyProvider provider = new DependencyProvider(configuration);
            var genFoo = provider.Resolve <GenFoo <IFoo> >();

            Assert.IsNotNull(genFoo);
        }
        public void RecursionDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <IRepository, RepositoryImplOne>();
            dependencies.Register <IServiceTwo, ServiceImplOne>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <IServiceTwo>();

            Assert.AreEqual("RepositoryImpl is created", (service1 as ServiceImplOne)?.Rep.TestObject());
        }
        public void SimpleRecursionDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <IRepository, RepositoryImpl1>();
            dependencies.Register <IService, ServiceImpl1>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <IService>();

            Assert.AreEqual((service1 as ServiceImpl1).rep.TestObject(), "RepositoryImpl is created");
        }
        public void DoubleRecursionDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <AService, ServiceFromAbstract>();
            dependencies.Register <IRepository, RepositoryImpl2>();
            dependencies.Register <IService, ServiceImpl1>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <IService>();

            Assert.AreEqual(((service1 as ServiceImpl1).rep as RepositoryImpl2).serv.TestObject(), "AService obj is created");
        }
Example #9
0
        public void ShouldAvoidCycle()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <AInterface, A>();
            dependencyConfiguration.Register <BInterface, B>();

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

            var A = dependencyProvider.Resolve <AInterface>();

            Assert.IsNotNull(A);
        }
Example #10
0
        public void ShouldResolveGenericType()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <IFoo, FooFromInterface>();
            dependencyConfiguration.Register(typeof(GenericFoo <IFoo>), typeof(GenericFoo <IFoo>));

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

            var GenericFoo = dependencyProvider.Resolve <GenericFoo <IFoo> >();

            Assert.IsNotNull(GenericFoo);
        }
Example #11
0
        public void ShouldReturnCollectionWithTwoElements()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <IBar, BarFromInterface>();
            dependencyConfiguration.Register <IBar, BarFromAbstract>();

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

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

            Assert.AreEqual(2, bars.Count());
        }
        public void DoubleRecursionDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <AbstractService, ServiceFromAbstract>();
            dependencies.Register <IRepository, RepositoryImplTwo>();
            dependencies.Register <IServiceTwo, ServiceImplOne>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <IServiceTwo>();

            Assert.AreEqual("AService obj is created", ((service1 as ServiceImplOne)?.Rep as RepositoryImplTwo)?.Serv.TestObject());
        }
Example #13
0
        public void ResolveEnumerableTest()
        {
            int expected = 2;
            DependencyConfiguration configuration = new DependencyConfiguration();

            configuration.Register <IBar, BarFromIBar>(false);
            configuration.Register <IBar, BarFromABar>(false);
            configuration.Register <AFoo, Foo>(false);
            configuration.Register <ABar, BarFromABar>(false);
            DependencyProvider provider = new DependencyProvider(configuration);
            var bars = provider.Resolve <IEnumerable <IBar> >();

            Assert.AreEqual(expected, bars.Count());
        }
        public void ReturningNullIfIncorrectImplTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <AbstractService, ErrorAbstractImpl>();
            dependencies.Register <IServiceTwo, ErrorNoPublicConstructorImpl>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <AbstractService>();
            var service2 = provider.Resolve <IServiceTwo>();

            Assert.IsNull(service1);
            Assert.IsNull(service2);
        }
        public void SimpleErrorDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <AService, ErrorAbstratcImpl>();
            dependencies.Register <IService, ErrorNoPublicConstructorImpl>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <AService>();
            var service2 = provider.Resolve <IService>();

            Assert.IsNull(service1);
            Assert.IsNull(service2);
        }
Example #16
0
        public void ShouldResolveOpenGenericType()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <IBar, BarFromAbstract>();
            dependencyConfiguration.Register <IFoo, FooFromInterface>();
            dependencyConfiguration.Register(typeof(GenericFoo <>), typeof(GenericFoo <>));

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

            var OpenGenFoo = dependencyProvider.Resolve <GenericFoo <FooFromInterface> >();

            Assert.IsNotNull(OpenGenFoo);
        }
Example #17
0
        public void ShouldCreateDependencyCorrectly()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <IBar, BarFromInterface>();
            dependencyConfiguration.Register <AbstractBar, BarFromAbstract>();
            dependencyConfiguration.Register <FooFromInterface, FooFromInterface>();

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

            var foo = dependencyProvider.Resolve <FooFromInterface>();

            Assert.IsNotNull(foo.Bar);
        }
        public void MultipleDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <IRepository, RepositoryImpl1>();
            dependencies.Register <IService, ServiceImpl1>();
            dependencies.Register <IService, ServiceImpl2>();

            var provider = new DependencyProvider(dependencies);
            var services = provider.Resolve <IEnumerable <IService> >().ToArray();

            Assert.AreEqual((services[0] as ServiceImpl1).rep.TestObject(), "RepositoryImpl is created");
            Assert.AreEqual((services[1] as ServiceImpl2).rep.TestObject(), "RepositoryImpl is created");
        }
Example #19
0
        public void BasicTest()
        {
            config.RegisterSingleton <Interface1, Class1If1>();
            config.Register <Interface2, Class1If2>();

            DependencyProvider provider = new DependencyProvider(config);

            Interface1 item = null;

            Assert.DoesNotThrow(() => provider.ValidateConfig());

            Assert.DoesNotThrow(() => item = provider.Resolve <Interface1>());

            Assert.NotNull(item);

            Assert.AreEqual(item.GetType(), typeof(Class1If1));
        }
        public void SimpleDependencyTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <IService, ServiceFromInterf>();
            dependencies.Register <AService, ServiceFromAbstract>();
            dependencies.Register <Random, Random>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <IService>();
            var service2 = provider.Resolve <AService>();
            var service3 = provider.Resolve <Random>();

            Assert.IsNotNull(service1);
            Assert.IsNotNull(service2);
            Assert.IsNotNull(service3);
        }
        public void CorrectWorkOfContainerTest()
        {
            var dependencies = new DependencyConfiguration();

            dependencies.Register <IServiceTwo, ServiceFromInterface>();
            dependencies.Register <AbstractService, ServiceFromAbstract>();
            dependencies.Register <Random, Random>();

            var provider = new DependencyProvider(dependencies);
            var service1 = provider.Resolve <IServiceTwo>();
            var service2 = provider.Resolve <AbstractService>();
            var service3 = provider.Resolve <Random>();

            Assert.IsNotNull(service1);
            Assert.IsNotNull(service2);
            Assert.IsNotNull(service3);
        }
Example #22
0
    public override void Configure(IFunctionsHostBuilder builder)
    {
        var config = builder.GetContext().Configuration;

        builder.Services.AddSingleton(new ServiceBusClient(config["ServiceBus"]));
        builder.Services.AddSingleton <IConnectionMultiplexer>(ConnectionMultiplexer.Connect(config["Redis"]));

        DependencyConfiguration.Register(builder.Services);
    }
Example #23
0
        public void NotRegisteredInnerDependencyTest()
        {
            DependencyConfiguration configuration = new DependencyConfiguration();

            configuration.Register <IFoo, Foo>(false);
            configuration.Register <IBar, BarFromIBar>(false);
            DependencyProvider provider = new DependencyProvider(configuration);

            try
            {
                IBar bar = provider.Resolve <IBar>();
                Assert.Fail("Cannot be created");
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e, e.Message);
            }
        }
Example #24
0
        public void DependencyCreationTest()
        {
            DependencyConfiguration configuration = new DependencyConfiguration();

            configuration.Register <IBar, BarFromABar>(false);
            DependencyProvider provider = new DependencyProvider(configuration);
            IBar bar = provider.Resolve <IBar>();

            Assert.IsNotNull(bar);
            Assert.AreEqual(bar.GetType(), typeof(BarFromABar));
        }
Example #25
0
        public void NotSingletonDependencyTest()
        {
            DependencyConfiguration conf = new DependencyConfiguration();

            conf.Register <IBar, BarFromABar>(false);
            DependencyProvider provider = new DependencyProvider(conf);
            IBar bar1 = provider.Resolve <IBar>();
            IBar bar2 = provider.Resolve <IBar>();

            Assert.AreNotEqual(bar1, bar2);
        }
Example #26
0
        public void ShouldReturnNull()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <IFoo, FooFromInterface>();

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

            IBar bar = dependencyProvider.Resolve <BarFromInterface>();

            Assert.IsNull(bar);
        }
Example #27
0
        public void ShouldReturnAsSelfCreation()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <FooFromInterface>();

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

            var foo = dependencyProvider.Resolve <FooFromInterface>();

            Assert.IsNotNull(foo);
            Assert.AreEqual(typeof(FooFromInterface), foo.GetType());
        }
Example #28
0
        public void ShouldThrowExceptionToAbstractClassImpl()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            try
            {
                dependencyConfiguration.Register <AbstractBar, AbstractBar>();
            }
            catch (Exception e)
            {
                Assert.IsNotNull(e, e.Message);
            }
        }
Example #29
0
        public void ShouldRegisterInstancePerDependency()
        {
            DependencyConfiguration dependencyConfiguration = new DependencyConfiguration();

            dependencyConfiguration.Register <IBar, BarFromInterface>();

            DependencyProvider dependencyProvider = new DependencyProvider(dependencyConfiguration);

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

            Assert.AreNotEqual(bar1, bar2);
        }
Example #30
0
 public void InterfaceRegisterTest()
 {
     try
     {
         DependencyConfiguration configuration = new DependencyConfiguration();
         configuration.Register <IBar, IBar>(false);
         Assert.Fail("Cannot create instance of interface");
     }
     catch (Exception e)
     {
         Assert.IsNotNull(e, e.Message);
     }
 }