Ejemplo n.º 1
0
 public void IntnerceptSpeceficTypeAsInterface()
 {
     sut.InterceptSpecificType((Target1 i) => (ITarget1) new Wrapper1(i));
     sut.Bind <ITarget1>().To <Target1>();
     Assert.True(sut.Get <ITarget1>() is Wrapper1);
     sut.Bind <ITarget1>().To <Target2>(); // override binding.
     Assert.True(sut.Get <ITarget1>() is Target2);
 }
        public void ResolveByStringValue()
        {
            sut.Bind <IBO>().To <BO1>().WhenParameterHasValue("1");
            sut.Bind <IBO>().To <BO2>().WhenParameterHasValue("2");

            Assert.False(sut.CanGet <IBO>());
            var factory = sut.Get <Func <string, IBO> >();

            Assert.True(factory("1") is BO1);
            Assert.True(factory("2") is BO2);
        }
Ejemplo n.º 3
0
        public void TestRecursiveResolutionWhenEnabledOkForAcyclicDependencies()
        {
            var ioc = new IocContainer(allowRecursiveResolution: true);

            ioc.Bind <IServiceLocator>(() => ioc);
            ioc.Bind <IExampleA, RecursiveNonCyclicA>();
            ioc.Bind <IExampleB, RecursiveNonCyclicB>();

            IExampleB resolved = ioc.Resolve <IExampleB>();

            Assert.IsInstanceOfType(resolved, typeof(RecursiveNonCyclicB));
        }
Ejemplo n.º 4
0
        public void TestRecursiveResolutionWhenDisabledNotOkForAcyclicDependencies()
        {
            var ioc = new IocContainer(allowRecursiveResolution: false);

            ioc.Bind <IServiceLocator>(() => ioc);
            ioc.Bind <IExampleA, RecursiveNonCyclicA>();
            ioc.Bind <IExampleB, RecursiveNonCyclicB>();

            Assert.ThrowsException <ActivationException>(
                () => ioc.Resolve <IExampleB>()
                );
        }
Ejemplo n.º 5
0
        public void TestGetInstanceNonexistentKey()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest, Concrete>();
            ioc.Bind <ITest, Concrete>("key");

            IServiceLocator locator = ioc;

            Assert.ThrowsException <ActivationException>(
                () => locator.GetInstance(typeof(ITest), "wrong")
                );
        }
Ejemplo n.º 6
0
        public void TestResolveObjectFromUnboundKeyedArguments()
        {
            var ioc = new IocContainer();

            // no key, so this should not bind successfully
            ioc.Bind <ITest, ConcreteTest>();

            // different key, so this should also not bind successfully
            ioc.Bind <ITest, ConcreteTest>("wrong key");

            Assert.ThrowsException <ActivationException>(
                () => ioc.Resolve <ObjectTakingKeyedITest>()
                );
        }
Ejemplo n.º 7
0
        public static void SetupIoc(IocContainer service)
        {
            ConfigureLog();


            service.Bind <IIocService>().ToConstant(service);

            // Root Window
            service.Bind <INavigationWindow>().To <NavigationWindow>().AsSingleton();
            service.Bind <RootNavigationWindow>().And <Window>().ToSelf().AsSingleton();

            // System Services
            service.Bind <IOpenSaveFile>().To <OpenSaveFileAdapter>();
        }
Ejemplo n.º 8
0
        public void TestCycleInClassesBoundByInterfaceTakingInterfaceTypes()
        {
            var ioc = new IocContainer();

            ioc.Bind <ICyclicA, CyclicAThroughInterface>();
            ioc.Bind <ICyclicB, CyclicBThroughInterface>();

            Assert.ThrowsException <ActivationException>(
                () => ioc.Resolve <ICyclicA>()
                );

            Assert.ThrowsException <ActivationException>(
                () => ioc.Resolve <ICyclicB>()
                );
        }
Ejemplo n.º 9
0
        public void TestGetAllInstances()
        {
            var ioc = new IocContainer();

            var inst1 = new Concrete();
            var inst2 = new Concrete();

            ioc.Bind <ITest>(() => inst1);
            ioc.Bind <ITest>("key", () => inst2);

            IServiceLocator locator = ioc;

            object[] instances = locator.GetAllInstances(typeof(ITest)).ToArray();
            CollectionAssert.AreEqual(new[] { inst1, inst2 }, instances);
        }
Ejemplo n.º 10
0
        public async Task TestAsyncSingleInstanceActivation()
        {
            var ioc = new IocContainer();

            bool started = false;

            // there shouldn't be any issues from reading from multiple contexts.
            Task <ITest> t = Task.Run(() =>
            {
                Volatile.Write(ref started, true);

                ioc.Bind <ITest>(
                    factory: Test.Create,
                    singleInstance: true
                    );

                return(ioc.Resolve <ITest>());
            });

            while (!Volatile.Read(ref started))
            {
                await Task.Delay(10);
            }

            ITest instance = ioc.Resolve <ITest>();

            Assert.IsNotNull(instance);

            ITest fromTask = await t;

            Assert.AreSame(instance, fromTask);
        }
        /// <summary>
        ///     Loads the current module.
        /// </summary>
        protected override bool LoadInternal()
        {
            //NOTE: You can use the custom extension methods in bindings.
            BindingServiceProvider
            .ResourceResolver
            .AddType(typeof(CustomExtensionMethods).Name, typeof(CustomExtensionMethods));

            if (Mode == LoadMode.Design)
            {
                var localizationManager = new LocalizationManager();
                if (IocContainer != null)
                {
                    IocContainer.BindToConstant <ILocalizationManager>(localizationManager);
                }
            }
            else
            {
                if (!IocContainer.CanResolve <ILocalizationManager>())
                {
                    IocContainer.Bind <ILocalizationManager, LocalizationManager>(DependencyLifecycle.SingleInstance);
                }
            }

            if (IocContainer != null)
            {
                IocContainer.Bind <IResourceMonitor, ResourceMonitor>(DependencyLifecycle.SingleInstance);
            }
            return(true);
        }
Ejemplo n.º 12
0
        public void TestGenericGetAllInstancesMatchesNonGeneric()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest, Concrete>(singleInstance: true);
            ioc.Bind <ITest, Concrete>("key1", singleInstance: true);
            ioc.Bind <ITest, Concrete>("key2", singleInstance: true);
            ioc.Bind <ITest, Concrete>("key3", singleInstance: true);

            IServiceLocator locator = ioc;

            ITest[]  generic    = locator.GetAllInstances <ITest>().ToArray();
            object[] nongeneric = locator.GetAllInstances(typeof(ITest)).ToArray();

            CollectionAssert.AreEqual(generic, nongeneric);
        }
Ejemplo n.º 13
0
        public void FactoryWorks()
        {
            sut.Bind <IFactory>().ToFactory();
            var fact = sut.Get <IFactory>();

            Assert.Equal("Foo", fact.CreateBO("Foo").P);
        }
Ejemplo n.º 14
0
        private static IocContainer CreateIocContainer(string[] args)
        {
            var ioc = new IocContainer();

            ioc.Bind <IStartupData>().To <StartupData>().WithParameters(new object[] { args }).AsSingleton();
            Startup.SetupIoc(ioc);
            return(ioc);
        }
Ejemplo n.º 15
0
        public void ChildContainterResolve()
        {
            sut.Bind <Dep>().ToSelf().AsSingleton();
            var c1 = sut.Get <ChildContainer>();

            c1.Bind <IBO>().To <BO1>();
            var c2 = sut.Get <ChildContainer>();

            c2.Bind <IBO>().To <BO2>();

            var v1 = c1.Get <IBO>();
            var v2 = c2.Get <IBO>();

            Assert.True(v1 is BO1);
            Assert.True(v2 is BO2);
            Assert.Equal(v1.Dep, v2.Dep);
        }
Ejemplo n.º 16
0
        public async Task TestAsyncWrite()
        {
            bool done = false;
            var  ioc  = new IocContainer();

            ioc.Bind <ITest, Test>();

            Task t;

            try
            {
                bool started = false;

                // The Bind calls from the task and here could corrupt the
                // container. Make sure it doesn't
                t = Task.Run(() =>
                {
                    Volatile.Write(ref started, true);

                    while (!Volatile.Read(ref done))
                    {
                        ioc.Bind <ITest, Test>();
                    }
                });

                while (!Volatile.Read(ref started))
                {
                    await Task.Delay(10);
                }

                for (int i = 0; i < 1000; i++)
                {
                    ioc.Bind <ITest, Test>();
                }

                ITest instance = ioc.Resolve <ITest>();
                Assert.IsNotNull(instance);
            }
            finally
            {
                done = true;
            }

            await t;
        }
Ejemplo n.º 17
0
 protected override bool LoadInternal()
 {
     IocContainer.Bind <ICurrentLocationDataProvider, UniversalAppCurrentLocationDataProvider>(DependencyLifecycle.SingleInstance);
     IocContainer.Bind <IApplicationSettings, Infrastructure.ApplicationSettings>(DependencyLifecycle.SingleInstance);
     IocContainer.Bind <ISensorPinManager, SensorPinManager>(DependencyLifecycle.SingleInstance);
     IocContainer.Bind <IMd5AlgorithmProvider, Md5AlgorithmProvider>(DependencyLifecycle.SingleInstance);
     BindingServiceProvider.ResourceResolver.AddType("ModelExtensions", typeof(ModelExtensions));
     return(true);
 }
        /// <summary>
        ///     Loads the current module.
        /// </summary>
        protected override bool LoadInternal()
        {
            var validatorProvider = IocContainer.Get <IValidatorProvider>();

            //NOTE: Registering validator.
            validatorProvider.Register <UserLoginValidator>();
            IocContainer.Bind <IUserRepository, CollectionUserRepository>(DependencyLifecycle.SingleInstance);
            return(true);
        }
Ejemplo n.º 19
0
        public void TestParameterKeyedBindingFromParent()
        {
            var parent = new IocContainer();

            parent.Bind <ITest, Concrete>("key");

            var ioc = new IocContainer(parent);

            ioc.Bind <ITest, Concrete>(singleInstance: false);
            ioc.Bind <ITest, Concrete>("wrong", singleInstance: false);

            var resolved = ioc.Resolve <CtorTakesKeyedTest>();

            Assert.IsInstanceOfType(resolved, typeof(CtorTakesKeyedTest));

            // make sure we didn't take the default or wrong keyed instances from the child container.
            Assert.AreNotSame(ioc.Resolve <ITest>(), resolved.Test);
            Assert.AreNotSame(ioc.Resolve <ITest>("wrong"), resolved.Test);
        }
Ejemplo n.º 20
0
        public void TestCycleInClassBoundByClassWithItself()
        {
            var ioc = new IocContainer();

            ioc.Bind <SelfCyclic, SelfCyclic>();

            Assert.ThrowsException <ActivationException>(
                () => ioc.Resolve <SelfCyclic>()
                );
        }
Ejemplo n.º 21
0
        public void TestCycleInClassBoundByInterfaceWithItself()
        {
            var ioc = new IocContainer();

            ioc.Bind <ICyclicA, SelfCyclicThroughInterface>();

            Assert.ThrowsException <ActivationException>(
                () => ioc.Resolve <SelfCyclicThroughInterface>()
                );
        }
Ejemplo n.º 22
0
        public void TestResolveFromInterfaceBoundToConcreteTypeWithKey()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest, ConcreteTest>("key");

            ITest obj = ioc.Resolve <ITest>("key");

            Assert.IsInstanceOfType(obj, typeof(ConcreteTest));
        }
Ejemplo n.º 23
0
        public void TestResolveFromTypeMissingCtorParameterBinding()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest, ConcreteTest>();

            Assert.ThrowsException <ActivationException>(
                () => ioc.Resolve <ObjectTakingITestAndITest2>()
                );
        }
Ejemplo n.º 24
0
        public void TestResolveFromTypeObjectAndKey()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest, ConcreteTest>("key");

            var obj = ioc.Resolve(typeof(ITest), "key");

            Assert.IsInstanceOfType(obj, typeof(ConcreteTest));
        }
Ejemplo n.º 25
0
        [TestCategory("SkipWhenLiveUnitTesting")] // tests a 5 second timeout
        public async Task TestAsyncSingleInstanceActivationTimeout()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest>(
                factory: Test.Timeout,
                singleInstance: true
                );

            bool started = false;

            // there shouldn't be any issues from reading from multiple contexts.
            Task <Exception> t = Task.Run(() =>
            {
                Volatile.Write(ref started, true);

                try
                {
                    ioc.Resolve <ITest>();
                    return(null);
                }
                catch (Exception ex)
                {
                    return(ex);
                }
            });

            while (!Volatile.Read(ref started))
            {
                await Task.Delay(10);
            }


            Exception fromHere;

            try
            {
                ioc.Resolve <ITest>();
                fromHere = null;
            }
            catch (Exception ex2)
            {
                fromHere = ex2;
            }

            Exception fromTask = await t;

            // one should be null, and the other should be non-null
            Assert.IsTrue((fromHere is null) != (fromTask is null));

            // only one should be null
            Exception ex = fromHere ?? fromTask;

            Assert.IsInstanceOfType(ex, typeof(ActivationException));
        }
Ejemplo n.º 26
0
        public void TestSingleInstanceRegisrationResolvesSameInstance()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest, ConcreteTest>(singleInstance: true);

            ITest test1 = ioc.Resolve <ITest>();
            ITest test2 = ioc.Resolve <ITest>();

            Assert.AreSame(test1, test2);
        }
Ejemplo n.º 27
0
        public void TestResolveObjectFromFallbackKey()
        {
            var ioc = new IocContainer();

            // no key, but should bind successfully on the fallback check.
            ioc.Bind <ITest, ConcreteTest>();

            var obj = ioc.Resolve <ObjectTakingKeyedITestWithFallback>();

            Assert.IsNotNull(obj);
        }
Ejemplo n.º 28
0
        public void TestNonSingleInstanceRegistrationResolvesNewInstance()
        {
            var ioc = new IocContainer();

            ioc.Bind <ITest, ConcreteTest>(singleInstance: false);

            ITest test1 = ioc.Resolve <ITest>();
            ITest test2 = ioc.Resolve <ITest>();

            Assert.AreNotSame(test1, test2);
        }
Ejemplo n.º 29
0
        public void TestResolveFromParentContainer()
        {
            var parent = new IocContainer();

            parent.Bind <ITest, Concrete>();

            var ioc = new IocContainer(parent);

            var obj = ioc.Resolve <ITest>();

            Assert.IsInstanceOfType(obj, typeof(Concrete));
        }
Ejemplo n.º 30
0
        public void TestParameterBindingFromParent()
        {
            var parent = new IocContainer();

            parent.Bind <ITest, Concrete>();

            var ioc = new IocContainer(parent);

            var resolved = ioc.Resolve <CtorTakesTest>();

            Assert.IsInstanceOfType(resolved, typeof(CtorTakesTest));
        }