Example #1
0
        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>());
        }
Example #2
0
        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>());
        }