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); }
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)); }
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>() ); }
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") ); }
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>() ); }
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>(); }
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>() ); }
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); }
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); }
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); }
public void FactoryWorks() { sut.Bind <IFactory>().ToFactory(); var fact = sut.Get <IFactory>(); Assert.Equal("Foo", fact.CreateBO("Foo").P); }
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); }
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); }
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; }
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); }
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); }
public void TestCycleInClassBoundByClassWithItself() { var ioc = new IocContainer(); ioc.Bind <SelfCyclic, SelfCyclic>(); Assert.ThrowsException <ActivationException>( () => ioc.Resolve <SelfCyclic>() ); }
public void TestCycleInClassBoundByInterfaceWithItself() { var ioc = new IocContainer(); ioc.Bind <ICyclicA, SelfCyclicThroughInterface>(); Assert.ThrowsException <ActivationException>( () => ioc.Resolve <SelfCyclicThroughInterface>() ); }
public void TestResolveFromInterfaceBoundToConcreteTypeWithKey() { var ioc = new IocContainer(); ioc.Bind <ITest, ConcreteTest>("key"); ITest obj = ioc.Resolve <ITest>("key"); Assert.IsInstanceOfType(obj, typeof(ConcreteTest)); }
public void TestResolveFromTypeMissingCtorParameterBinding() { var ioc = new IocContainer(); ioc.Bind <ITest, ConcreteTest>(); Assert.ThrowsException <ActivationException>( () => ioc.Resolve <ObjectTakingITestAndITest2>() ); }
public void TestResolveFromTypeObjectAndKey() { var ioc = new IocContainer(); ioc.Bind <ITest, ConcreteTest>("key"); var obj = ioc.Resolve(typeof(ITest), "key"); Assert.IsInstanceOfType(obj, typeof(ConcreteTest)); }
[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)); }
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); }
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); }
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); }
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)); }
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)); }