public void Test_CanLoad_AndResolve_BindToInstance() { StandardDependencyResolver resolver = new StandardDependencyResolver(); List <DependencyRegistration> dependencies = new List <DependencyRegistration>(); IAsSingletonSampleDependency instance = new AsSingletonSampleDependencyImpl(); dependencies.Add(DependencyRegistration.BindToInstance(typeof(IAsSingletonSampleDependency), instance)); resolver.Load(dependencies); Assert.IsTrue(resolver.CanResolve(typeof(IAsSingletonSampleDependency))); Assert.IsTrue(resolver.CanResolve <IAsSingletonSampleDependency>()); Assert.AreSame(instance, resolver.TryResolve <IAsSingletonSampleDependency>()); Assert.AreSame(resolver.TryResolve <IAsSingletonSampleDependency>(), resolver.TryResolve <IAsSingletonSampleDependency>()); }
private static void Assert_DependenciesCanBeResolved(StandardDependencyResolver resolver) { Assert.IsTrue(resolver.CanResolve(typeof(IAsSingletonSampleDependency))); Assert.IsTrue(resolver.CanResolve(typeof(IAsThreadSingletonSampleDependency))); Assert.IsTrue(resolver.CanResolve(typeof(IAsTransientSampleDependency))); Assert.IsFalse(resolver.CanResolve(typeof(IDependencyNotRegisteredWithResolver))); Assert.IsTrue(resolver.CanResolve <IAsSingletonSampleDependency>()); Assert.IsTrue(resolver.CanResolve <IAsThreadSingletonSampleDependency>()); Assert.IsTrue(resolver.CanResolve <IAsTransientSampleDependency>()); Assert.IsFalse(resolver.CanResolve <IDependencyNotRegisteredWithResolver>()); Assert.IsInstanceOf <AsSingletonSampleDependencyImpl>(resolver.TryResolve(typeof(IAsSingletonSampleDependency))); Assert.IsInstanceOf <AsSingletonSampleDependencyImpl>(resolver.TryResolve <IAsSingletonSampleDependency>()); Assert.IsInstanceOf <AsThreadSingletonSampleDependencyImpl>(resolver.TryResolve(typeof(IAsThreadSingletonSampleDependency))); Assert.IsInstanceOf <AsThreadSingletonSampleDependencyImpl>(resolver.TryResolve <IAsThreadSingletonSampleDependency>()); Assert.IsInstanceOf <AsTransientSampleDependencyImpl>(resolver.TryResolve(typeof(IAsTransientSampleDependency))); Assert.IsInstanceOf <AsTransientSampleDependencyImpl>(resolver.TryResolve <IAsTransientSampleDependency>()); Assert.IsNull(resolver.TryResolve(typeof(IDependencyNotRegisteredWithResolver))); Assert.IsNull(resolver.TryResolve <IDependencyNotRegisteredWithResolver>()); }
private static void Assert_DependenciesCorrectlyResolved(int nTestThreads, StandardDependencyResolver resolver) { //Check that singletons are resolved as such Assert.AreSame(resolver.TryResolve <IAsSingletonSampleDependency>(), resolver.TryResolve <IAsSingletonSampleDependency>()); //Check that thread singletons are resolved as such List <Task> threads = new List <Task>(); ConcurrentDictionary <int, IAsThreadSingletonSampleDependency> threadInstances = new ConcurrentDictionary <int, IAsThreadSingletonSampleDependency>(); for (int i = 0; i < nTestThreads; i++) { threads.Add(Task.Run(() => { IAsThreadSingletonSampleDependency dep = resolver .TryResolve <IAsThreadSingletonSampleDependency>(); Assert.AreSame(dep, resolver .TryResolve <IAsThreadSingletonSampleDependency>()); threadInstances.AddOrUpdate(Thread.CurrentThread.ManagedThreadId, (key) => dep, (key, oldDep) => dep); })); } Task.WaitAll(threads.ToArray()); IAsThreadSingletonSampleDependency[] checkThreadInstances = threadInstances .Values .ToArray(); for (int i = 1; i < checkThreadInstances.Length; i++) { Assert.AreNotSame(checkThreadInstances[i - 1], checkThreadInstances[i]); } //Check that transients are resolved as such Assert.AreNotSame(resolver.TryResolve <IAsTransientSampleDependency>(), resolver.TryResolve <IAsTransientSampleDependency>()); }