public void AttributeIsUsed() { var builder = new StyletIoCBuilder(); builder.Bind<IC>().To<C3>(); builder.Bind<IC>().To<C4>(); var ioc = builder.BuildContainer(); Assert.IsInstanceOf<C4>(ioc.Get<IC>("key1")); }
public void GivenKeyOverridesAttribute() { var builder = new StyletIoCBuilder(); builder.Bind<IC>().To<C3>(); builder.Bind<IC>().To<C4>().WithKey("key2"); var ioc = builder.BuildContainer(); Assert.IsInstanceOf<C4>(ioc.Get<IC>("key2")); }
public void GetReturnsKeyedType() { var builder = new StyletIoCBuilder(); builder.Bind<IC>().To<C1>().WithKey("key1"); builder.Bind<IC>().To<C2>().WithKey("key2"); var ioc = builder.BuildContainer(); Assert.IsInstanceOf<C1>(ioc.Get<IC>("key1")); Assert.IsInstanceOf<C2>(ioc.Get<IC>("key2")); }
public void FuncFactoryWorksConstructorInjection() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf(); builder.Bind<C2>().ToSelf(); var ioc = builder.BuildContainer(); var c2 = ioc.Get<C2>(); var c1Func = c2.C1Func; Assert.IsNotNull(c1Func()); }
public void FuncFactoryOfIEnumerableWorksAsExpected() { var builder = new StyletIoCBuilder(); builder.Bind<I1>().To<C11>(); builder.Bind<I1>().To<C12>(); var ioc = builder.BuildContainer(); var func = ioc.Get<Func<IEnumerable<I1>>>(); var results = func().ToList(); Assert.AreEqual(2, results.Count); Assert.IsInstanceOf<C11>(results[0]); Assert.IsInstanceOf<C12>(results[1]); }
public void GetAllReturnsKeyedTypes() { var builder = new StyletIoCBuilder(); builder.Bind<IC>().To<C1>().WithKey("key1"); builder.Bind<IC>().To<C2>().WithKey("key1"); builder.Bind<IC>().To<C3>(); var ioc = builder.BuildContainer(); var results = ioc.GetAll<IC>("key1").ToList(); Assert.AreEqual(results.Count, 2); Assert.IsInstanceOf<C1>(results[0]); Assert.IsInstanceOf<C2>(results[1]); }
public void CompileThrowsIfFindConstructorExceptionAndThrowOnErrorIsTrue() { var builder = new StyletIoCBuilder(); builder.Bind<C2>().ToSelf(); var ioc = builder.BuildContainer(); Assert.Throws<StyletIoCFindConstructorException>(() => ioc.Compile()); }
public void CompileDoesNotThrowIfFindConstructorExceptionAndThrowOnErrorIsFalse() { var builder = new StyletIoCBuilder(); builder.Bind<C2>().ToSelf(); var ioc = builder.BuildContainer(); Assert.DoesNotThrow(() => ioc.Compile(false)); }
public void CompileSucceedsIfNoErrors() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf(); var ioc = builder.BuildContainer(); Assert.DoesNotThrow(() => ioc.Compile()); Assert.NotNull(ioc.Get<C1>()); }
public void FuncFactoryOfSingletonWorksAsExpected() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf().InSingletonScope(); var ioc = builder.BuildContainer(); var func = ioc.Get<Func<C1>>(); Assert.AreEqual(func(), func()); }
public void FuncFactoryOfTransientWorksAsExpected() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf(); var ioc = builder.BuildContainer(); var func = ioc.Get<Func<C1>>(); Assert.AreNotEqual(func(), func()); }
public void FuncFactoryWorksForGetNoKey() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf(); var ioc = builder.BuildContainer(); var func = ioc.Get<Func<C1>>(); var result = func(); Assert.IsNotNull(result); }
public void SingletonBindingsResolveGeneric() { var builder = new StyletIoCBuilder(); builder.Bind<IC1>().To<C11>().InSingletonScope(); builder.Bind<IC1>().To<C12>().InSingletonScope(); builder.Bind<IC1>().To<C13>().InSingletonScope(); builder.Bind<IC2>().To<C21>().InSingletonScope(); var ioc = builder.BuildContainer(); var results1 = ioc.GetAll<IC1>().ToList(); var results2 = ioc.GetAll<IC1>().ToList(); Assert.AreEqual(3, results1.Count); Assert.IsInstanceOf<C11>(results1[0]); Assert.IsInstanceOf<C12>(results1[1]); Assert.IsInstanceOf<C13>(results1[2]); Assert.That(results1, Is.EquivalentTo(results2)); }
public void ImplementationTransientBindingsResolveTyped() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(IC1)).To(typeof(C11)); builder.Bind(typeof(IC1)).To(typeof(C12)); builder.Bind(typeof(IC1)).To(typeof(C13)); builder.Bind(typeof(IC2)).To(typeof(C21)); var ioc = builder.BuildContainer(); var results1 = ioc.GetAll(typeof(IC1)).ToList(); var results2 = ioc.GetAll(typeof(IC1)).ToList(); Assert.AreEqual(3, results1.Count); Assert.IsInstanceOf<C11>(results1[0]); Assert.IsInstanceOf<C12>(results1[1]); Assert.IsInstanceOf<C13>(results1[2]); Assert.That(results1, Is.Not.EquivalentTo(results2)); }
public void InstanceBindingUsesInstanceToResolve() { var c1 = new C1(); var builder = new StyletIoCBuilder(); builder.Bind<I1>().ToInstance(c1); var ioc = builder.BuildContainer(); Assert.AreEqual(c1, ioc.Get<I1>()); Assert.AreEqual(c1, ioc.Get<I1>()); }
public void BuildsUpPublicFields() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf(); var ioc = builder.BuildContainer(); var subject = new Subject1(); ioc.BuildUp(subject); Assert.IsInstanceOf<C1>(subject.C1); Assert.IsNull(subject.Ignored); }
public void InstanceBindingUsesInstanceToResolve() { var c1 = new C1(); var builder = new StyletIoCBuilder(); builder.Bind <I1>().ToInstance(c1); var ioc = builder.BuildContainer(); Assert.AreEqual(c1, ioc.Get <I1>()); Assert.AreEqual(c1, ioc.Get <I1>()); }
public void SingletonRegistrationDisposesDisposableInstanceWhenParentDisposed() { var builder = new StyletIoCBuilder(); builder.Bind <C3>().ToSelf().InSingletonScope(); var ioc = builder.BuildContainer(); var c3 = ioc.Get <C3>(); ioc.Dispose(); Assert.IsTrue(c3.Disposed); }
public void GetAllReturnsSingleInstanceIfOnlyOneRegistration() { var builder = new StyletIoCBuilder(); builder.Bind <IC1>().To <C11>(); var ioc = builder.BuildContainer(); var results = ioc.GetAll <IC1>().ToList(); Assert.AreEqual(1, results.Count); Assert.IsInstanceOf <C11>(results[0]); }
public void SelfTransientBindingResolvesGeneric() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf(); var ioc = builder.BuildContainer(); var obj1 = ioc.Get<C1>(); var obj2 = ioc.Get<C1>(); Assert.That(obj1, Is.Not.Null); Assert.That(obj1, Is.Not.EqualTo(obj2)); }
public void ImplementationTransientBindingsResolveGeneric() { var builder = new StyletIoCBuilder(); builder.Bind <IC1>().To <C11>(); builder.Bind <IC1>().To <C12>(); builder.Bind <IC1>().To <C13>(); builder.Bind <IC2>().To <C21>(); var ioc = builder.BuildContainer(); var results1 = ioc.GetAll <IC1>().ToList(); var results2 = ioc.GetAll <IC1>().ToList(); Assert.AreEqual(3, results1.Count); Assert.IsInstanceOf <C11>(results1[0]); Assert.IsInstanceOf <C12>(results1[1]); Assert.IsInstanceOf <C13>(results1[2]); Assert.That(results1, Is.Not.EquivalentTo(results2)); }
public void SelfSingletonBindingResolvesTyped() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(C1)).ToSelf().InSingletonScope(); var ioc = builder.BuildContainer(); var obj1 = ioc.Get(typeof(C1)); var obj2 = ioc.Get(typeof(C1)); Assert.That(obj1, Is.Not.Null); Assert.That(obj1, Is.EqualTo(obj2)); }
public void FactoryTransientBindingResolvesGeneric() { var builder = new StyletIoCBuilder(); builder.Bind <C1>().ToFactory(c => new C1()); var ioc = builder.BuildContainer(); var obj1 = ioc.Get <C1>(); var obj2 = ioc.Get <C1>(); Assert.That(obj1, Is.Not.Null); Assert.That(obj1, Is.Not.EqualTo(obj2)); }
public void BuildsUpPublicProperties() { var builder = new StyletIoCBuilder(); builder.Bind <C1>().ToSelf(); var ioc = builder.BuildContainer(); var subject = new Subject3(); ioc.BuildUp(subject); Assert.IsInstanceOf <C1>(subject.C1); }
public void IgnoresAllImplementsWhichIsNotPossible() { var builder = new StyletIoCBuilder(); builder.Bind <I1>().ToAllImplementations(); var ioc = builder.BuildContainer(); var result = ioc.GetAll <I1>().ToList(); Assert.AreEqual(2, result.Count); Assert.IsNotInstanceOf <C13>(result[0]); Assert.IsNotInstanceOf <C13>(result[1]); }
private static IContainer BuildContainer() { var builder = new StyletIoCBuilder(); // Autobind the .Services assembly builder.Autobind(typeof(DataService).Assembly); // Bind settings as singleton builder.Bind <SettingsService>().ToSelf().InSingletonScope(); // Set instance return(builder.BuildContainer()); }
public void RespectsKeys() { var builder = new StyletIoCBuilder(); builder.Bind <C1>().ToSelf().WithKey("key"); var ioc = builder.BuildContainer(); var subject = new Subject5(); ioc.BuildUp(subject); Assert.IsInstanceOf <C1>(subject.C1); }
public void NongenericInterfaceToAllImplementations() { var builder = new StyletIoCBuilder(); builder.Bind <I1>().ToAllImplementations(); var ioc = builder.BuildContainer(); var result = ioc.GetAll <I1>().ToList(); Assert.AreEqual(2, result.Count); Assert.IsInstanceOf <C11>(result[0]); Assert.IsInstanceOf <C12>(result[1]); }
public void ImplementationSingletonBindingResolvesGeneric() { var builder = new StyletIoCBuilder(); builder.Bind <IC1>().To <C1>().InSingletonScope(); var ioc = builder.BuildContainer(); var obj1 = ioc.Get <IC1>(); var obj2 = ioc.Get <IC1>(); Assert.That(obj1, Is.Not.Null); Assert.That(obj1, Is.EqualTo(obj2)); }
public void DisposedSingletonRegistrationDoesNotRetainInstance() { var builder = new StyletIoCBuilder(); builder.Bind <C1>().ToSelf().InSingletonScope(); var ioc = builder.BuildContainer(); var weakRef = new WeakReference(ioc.Get <C1>()); ioc.Dispose(); GC.Collect(); Assert.IsFalse(weakRef.IsAlive); }
public void BuildsUpPrivateFields() { var builder = new StyletIoCBuilder(); builder.Bind <C1>().ToSelf(); var ioc = builder.BuildContainer(); var subject = new Subject2(); ioc.BuildUp(subject); Assert.IsInstanceOf <C1>(subject.GetC1()); }
public void ImplementationTransientBindingResolvesTyped() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(IC1)).To(typeof(C1)); var ioc = builder.BuildContainer(); var obj1 = ioc.Get(typeof(IC1)); var obj2 = ioc.Get(typeof(IC1)); Assert.That(obj1, Is.Not.Null); Assert.That(obj1, Is.Not.EqualTo(obj2)); }
public void AutobindingBindingsCanBeReplaced() { var builder = new StyletIoCBuilder(); builder.Autobind(); builder.Bind <C11>().ToSelf().InSingletonScope(); var ioc = builder.BuildContainer(); var result1 = ioc.Get <C11>(); var result2 = ioc.Get <C11>(); Assert.AreEqual(result2, result1); }
static Container() { var builder = new StyletIoCBuilder(); // Autobind services in the .Core assembly builder.Autobind(typeof(DataService).Assembly); // Bind settings as singleton builder.Bind <SettingsService>().ToSelf().InSingletonScope(); // Set instance Instance = builder.BuildContainer(); }
public void GenericInterfaceToAllImplementations() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(I2 <>)).ToAllImplementations(); var ioc = builder.BuildContainer(); var result = ioc.GetAll <I2 <int> >().ToList(); Assert.AreEqual(2, result.Count); Assert.IsInstanceOf <C21 <int> >(result[0]); Assert.IsInstanceOf <C22 <int> >(result[1]); }
public void BuildsUpPublicFields() { var builder = new StyletIoCBuilder(); builder.Bind <C1>().ToSelf(); var ioc = builder.BuildContainer(); var subject = new Subject1(); ioc.BuildUp(subject); Assert.IsInstanceOf <C1>(subject.C1); Assert.IsNull(subject.Ignored); }
public void BuildUpDoesNotReplaceAlreadySetProperties() { var builder = new StyletIoCBuilder(); builder.Bind <C1>().ToSelf(); var ioc = builder.BuildContainer(); var s = new Subject1(); ioc.BuildUp(s); var firstC1 = s.C1; ioc.BuildUp(s); Assert.AreEqual(s.C1, firstC1); }
protected T CreateUnitTestInstance <T>(string key = null) { var builder = new StyletIoCBuilder(); builder.Bind <Shell>().ToSelf(); builder.Bind <ToolbarBase>().To <FakeToolbar>(); builder.Bind <ShellCommandBase>().To <FakeCommand>().WithKey("startCommand"); builder.Bind <IHotkeyHandler>().To <FakeHotkeyCommander>(); builder.Bind <ContextMenuBase>().To <FakeContextMenu>(); builder.Bind <ScreenHostBase>().To <FakeScreenHost>(); builder.Bind <DialogHostBase>().To <FakeDialogHost>(); var container = builder.BuildContainer(); return(container.GetTypeOrAll <T>(key)); }
/// <summary> /// Carries out default configuration of StyletIoC. Override if you don't want to do this /// </summary> /// <param name="builder">StyletIoC builder to use to configure the container</param> protected virtual void DefaultConfigureIoC(StyletIoCBuilder builder) { // Mark these as weak-bindings, so the user can replace them if they want var viewManagerConfig = new ViewManagerConfig() { ViewFactory = this.GetInstance, ViewAssemblies = builder.Assemblies//new List<Assembly>() { this.GetType().Assembly } }; builder.Bind <ViewManagerConfig>().ToInstance(viewManagerConfig).AsWeakBinding(); // Bind it to both IViewManager and to itself, so that people can get it with Container.Get<ViewManager>() builder.Bind <IViewManager>().And <ViewManager>().To <ViewManager>().AsWeakBinding(); builder.Bind <IWindowManagerConfig>().ToInstance(this).DisposeWithContainer(false).AsWeakBinding(); builder.Bind <IWindowManager>().To <WindowManager>().InSingletonScope().AsWeakBinding(); builder.Bind <IEventAggregator>().To <EventAggregator>().InSingletonScope().AsWeakBinding(); builder.Bind <IMessageBoxViewModel>().To <MessageBoxViewModel>().AsWeakBinding(); // Stylet's assembly isn't added to the container, so add this explicitly builder.Bind <MessageBoxView>().ToSelf(); builder.Autobind(); }
public void ThrowsIfSameBindingAppearsMultipleTimes() { var builder = new StyletIoCBuilder(); builder.Bind<IC1>().To<C1>(); builder.Bind<IC1>().To<C1>(); Assert.Throws<StyletIoCRegistrationException>(() => builder.BuildContainer()); }
public void FactorySingletonBindingResolvesGeneric() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToFactory(c => new C1()).InSingletonScope(); var ioc = builder.BuildContainer(); var obj1 = ioc.Get<C1>(); var obj2 = ioc.Get<C1>(); Assert.That(obj1, Is.Not.Null); Assert.That(obj1, Is.EqualTo(obj2)); }
public void AllowsFactoryToProvideInterfaceType() { var builder = new StyletIoCBuilder(); Assert.DoesNotThrow(() => builder.Bind <I1>().ToFactory <I1>(c => new C1())); }
public void ThrowsIfRegistrationFactoryIsNUll() { var builder = new StyletIoCBuilder(); Assert.Throws <ArgumentNullException>(() => builder.Bind <C1>().ToSelf().WithRegistrationFactory(null)); }
public void ThrowsIfFactoryBoundToUnboundGeneric() { var builder = new StyletIoCBuilder(); Assert.Throws <StyletIoCRegistrationException>(() => builder.Bind(typeof(I6 <>)).ToFactory(x => new C6 <int>())); }
public void ThrowsIfNormalServiceBoundToUnboundGenericService() { var builder = new StyletIoCBuilder(); Assert.Throws <StyletIoCRegistrationException>(() => builder.Bind <I6 <int> >().To(typeof(C6 <>))); }
public void ThrowsINonGenericServiceBoundToNormalImplementation() { var builder = new StyletIoCBuilder(); Assert.Throws <StyletIoCRegistrationException>(() => builder.Bind(typeof(I6 <>)).To <C8>()); }
public void ThrowsIfTypeDoesNotImplementService() { var builder = new StyletIoCBuilder(); Assert.Throws <StyletIoCRegistrationException>(() => builder.Bind <I1>().To <C2>()); }
public void CachedFactoryInstanceExpressionWorks() { // The factory's instance expression can be cached. This ensures that that works var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToFactory(x => new C1()); builder.Bind<C2>().ToSelf(); var ioc = builder.BuildContainer(); var c1 = ioc.Get<C1>(); var c2 = ioc.Get<C2>(); Assert.NotNull(c2.C1); Assert.AreNotEqual(c1, c2.C1); }
public void IEnumerableOfFuncFactoryWorksAsExpected() { var builder = new StyletIoCBuilder(); builder.Bind<I1>().To<C11>(); builder.Bind<I1>().To<C12>(); var ioc = builder.BuildContainer(); var funcCollection = ioc.GetTypeOrAll<IEnumerable<Func<I1>>>().ToList(); var result = funcCollection[0](); Assert.AreEqual(2, funcCollection.Count); Assert.IsInstanceOf<C11>(funcCollection[0]()); Assert.IsInstanceOf<C12>(funcCollection[1]()); }
public void ThrowsIfCollectionTypeCantBeResolved() { // This test is needed to hit a condition in TryRetrieveGetAllRegistrationFromElementType // where a collection type is constructed, but is unsuitable var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf(); builder.Bind<C10>().ToSelf(); var ioc = builder.BuildContainer(); Assert.Throws<StyletIoCFindConstructorException>(() => ioc.Get<C10>()); }
public void DisposedSingletonRegistrationDoesNotRetainInstance() { var builder = new StyletIoCBuilder(); builder.Bind<C1>().ToSelf().InSingletonScope(); var ioc = builder.BuildContainer(); var weakRef = new WeakReference(ioc.Get<C1>()); ioc.Dispose(); GC.Collect(); Assert.IsFalse(weakRef.IsAlive); }
public void DoesNotThrowIfSameBindingAppearsMultipleTimesWithDifferentKeys() { var builder = new StyletIoCBuilder(); builder.Bind<IC1>().To<C1>(); builder.Bind<IC1>().To<C1>().WithKey("key"); Assert.DoesNotThrow(() => builder.BuildContainer()); }
public void ThrowsIfFactoryTypeIsUnboundGeneric() { var builder = new StyletIoCBuilder(); Assert.Throws <StyletIoCRegistrationException>(() => builder.Bind(typeof(IGenericFactory <>)).ToAbstractFactory()); }
public void ThrowsIfMoreThanOneRegistrationFound() { var builder = new StyletIoCBuilder(); builder.Bind<IC1>().To<C1>(); builder.Bind<IC1>().To<C12>(); var ioc = builder.BuildContainer(); Assert.Throws<StyletIoCRegistrationException>(() => ioc.Get<IC1>()); }
public void ThrowsIfUnboundGenericDoesNotImplementService() { var builder = new StyletIoCBuilder(); Assert.Throws <StyletIoCRegistrationException>(() => builder.Bind(typeof(I1 <>)).To(typeof(C12 <>))); }
public void ImplementationSingletonBindingResolvesGeneric() { var builder = new StyletIoCBuilder(); builder.Bind<IC1>().To<C1>().InSingletonScope(); var ioc = builder.BuildContainer(); var obj1 = ioc.Get<IC1>(); var obj2 = ioc.Get<IC1>(); Assert.That(obj1, Is.Not.Null); Assert.That(obj1, Is.EqualTo(obj2)); }
public void SingletonRegistrationDisposesDisposableInstanceWhenParentDisposed() { var builder = new StyletIoCBuilder(); builder.Bind<C3>().ToSelf().InSingletonScope(); var ioc = builder.BuildContainer(); var c3 = ioc.Get<C3>(); ioc.Dispose(); Assert.IsTrue(c3.Disposed); }
public void FuncFactoryOfAbstractFactoryWorksAsExpected() { var builder = new StyletIoCBuilder(); builder.Bind<I1>().To<C11>(); builder.Bind<I1Factory>().ToAbstractFactory(); var ioc = builder.BuildContainer(); var func = ioc.Get<Func<I1Factory>>(); Assert.IsNotNull(func); var i1 = func().GetI1(); Assert.IsInstanceOf<C11>(i1); }