Example #1
0
        public void RegisterAndResolveInstanceByInterface()
        {
            var serviceLocator = new ServiceLocator();
            var a = new A();

            serviceLocator.RegisterInstance(typeof(IA), a);
            var result = serviceLocator.GetService(typeof (IA));

            Assert.AreEqual(a, result);
        }
Example #2
0
        public void RegisterAndResolveTypeByClass()
        {
            var serviceLocator = new ServiceLocator();

            serviceLocator.RegisterSingleton(typeof(A), typeof(A));
            var result = serviceLocator.GetService(typeof(A));

            Assert.NotNull(result);
            Assert.IsInstanceOf<A>(result);
        }
Example #3
0
        public void AvoidCircularReferences()
        {
            var serviceLocator = new ServiceLocator();

            serviceLocator.RegisterSingleton(typeof(IA), typeof(A));
            serviceLocator.RegisterSingleton(typeof(IB), typeof(B));
            serviceLocator.RegisterSingleton(typeof(IC), typeof(C));
            var result = (IC) serviceLocator.GetService(typeof(IC));

            Assert.NotNull(result);
            Assert.IsInstanceOf<C>(result);
            Assert.AreEqual(3, result.UsedConstructor);
        }
Example #4
0
        public void ResolveAdHocTransientInstantIfNotRegistered()
        {
            var serviceLocator = new ServiceLocator();

            var result = serviceLocator.GetService(typeof(A));
            var result2 = serviceLocator.GetService(typeof(A));

            Assert.NotNull(result);
            Assert.IsInstanceOf<A>(result);
            Assert.NotNull(result2);
            Assert.IsInstanceOf<A>(result2);
            Assert.AreNotEqual(result, result2);
        }
Example #5
0
        public void UseProperConstructorWithOneParameter()
        {
            var serviceLocator = new ServiceLocator();

            serviceLocator.RegisterSingleton(typeof(IA), typeof(A));
            serviceLocator.RegisterSingleton(typeof(IC), typeof(C));
            var result = (IC)serviceLocator.GetService(typeof(IC));

            Assert.NotNull(result);
            Assert.IsInstanceOf<C>(result);
            Assert.AreEqual(2, result.UsedConstructor);
        }
Example #6
0
        public void Transient()
        {
            var serviceLocator = new ServiceLocator();

            serviceLocator.RegisterTransient(typeof(IA), typeof(A));
            var result = serviceLocator.GetService(typeof(IA));
            var result2 = serviceLocator.GetService(typeof(IA));

            Assert.NotNull(result);
            Assert.IsInstanceOf<A>(result);
            Assert.NotNull(result2);
            Assert.IsInstanceOf<A>(result2);
            Assert.AreNotEqual(result, result2);
        }
Example #7
0
        public void SingletonInstance()
        {
            var serviceLocator = new ServiceLocator();

            serviceLocator.RegisterSingleton(typeof(IA), typeof(A));
            var result = serviceLocator.GetService(typeof(IA));
            var result2 = serviceLocator.GetService(typeof(IA));

            Assert.NotNull(result);
            Assert.IsInstanceOf<A>(result);
            Assert.AreEqual(result, result2);
        }
Example #8
0
 public RecursiveResolver(ServiceLocator locator, Type implementedType, Type implementingType)
 {
     _locator = locator;
     _implementedType = implementedType;
     _implementingType = implementingType;
 }