IsTypeRegistered() public method

Determines whether the specified service type is registered.
Note that the actual implementation lays in the hands of the IoC technique being used.
The is null.
public IsTypeRegistered ( Type serviceType, object tag = null ) : bool
serviceType System.Type The type of the service.
tag object The tag to register the service with. The default value is null.
return bool
        public void InitializeServiceLocatorFromNonDefaultConfiguration()
        {
            Configuration openExeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var configurationSection = openExeConfiguration.GetSection<IoCConfigurationSection>("ioc", "catel");
            var serviceLocatorConfiguration = configurationSection.GetServiceLocatorConfiguration("test");
            Assert.IsNotNull(serviceLocatorConfiguration);

            var serviceLocator = new ServiceLocator();
            serviceLocatorConfiguration.Configure(serviceLocator);

            Assert.AreEqual(serviceLocatorConfiguration.SupportDependencyInjection, serviceLocator.SupportDependencyInjection);
            foreach (Registration registration in serviceLocatorConfiguration)
            {
                serviceLocator.IsTypeRegistered(registration.InterfaceType);
                if (registration.RegistrationType == RegistrationType.Singleton)
                {
                    serviceLocator.IsTypeRegisteredAsSingleton(registration.InterfaceType);
                }
            }
        }
Example #2
0
            public void RegisterExternalContainer_ValidContainerAndTestTypeRetrieval()
            {
                var serviceLocator = new ServiceLocator();
                var ninjectContainer = new StandardKernel();
                serviceLocator.RegisterExternalContainer(ninjectContainer);
                ninjectContainer.Bind<ITestInterface>().To<TestClass1>();

                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
Example #3
0
            public void ResolveType_UnregisteredType()
            {
                var serviceLocator = new ServiceLocator();

                Assert.IsFalse(serviceLocator.IsTypeRegistered<ITestInterface>());
                ExceptionTester.CallMethodAndExpectException<NotSupportedException>(() => serviceLocator.ResolveType(typeof(ITestInterface)));
            }
Example #4
0
            public void ResolveType_Generic()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();

                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());
                Assert.IsInstanceOfType(serviceLocator.ResolveType<ITestInterface>(), typeof(TestClass1));
            }
Example #5
0
            public void ResoleType_Generic_SingletonLifestyle()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();
                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());

                var firstInstance = serviceLocator.ResolveType<ITestInterface>();
                Assert.IsInstanceOfType(firstInstance, typeof(TestClass1));

                var secondInstance = serviceLocator.ResolveType<ITestInterface>();
                Assert.IsInstanceOfType(secondInstance, typeof(TestClass1));

                Assert.AreSame(firstInstance, secondInstance);
            }
Example #6
0
            public void AllowsTheSameInterfaceDefinedTwiceWithDifferentTags()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType(typeof(ITestInterface), typeof(TestClass1), "1");
                serviceLocator.RegisterType(typeof(ITestInterface), typeof(TestClass1), "2");

                Assert.IsFalse(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface), "1"));
                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface), "2"));
            }
Example #7
0
 public void DoesNotRegisterTheImplementationTypesAsTheInterfaceTypesByDefault()
 {
     var serviceLocator = new ServiceLocator();
     Assert.IsFalse(serviceLocator.IsTypeRegistered<IFooService>());
     Assert.IsFalse(serviceLocator.IsTypeRegistered<IFooService2>());
 }
Example #8
0
            public void RegistersTheImplementationTypesAsInterfaceTypesIfIsSetToTrue()
            {
                var serviceLocator = new ServiceLocator { AutoRegisterTypesViaAttributes = true };

                Assert.IsTrue(serviceLocator.IsTypeRegistered<IFooService>());
                Assert.IsTrue(serviceLocator.IsTypeRegisteredAsSingleton<IFooService>());

                Assert.IsTrue(serviceLocator.IsTypeRegistered<IFooService2>());
                Assert.IsFalse(serviceLocator.IsTypeRegisteredAsSingleton<IFooService2>());

                var resolveType = serviceLocator.ResolveType<IFooService>();
                Assert.IsInstanceOfType(resolveType, typeof(FooService));

                var resolveType2 = serviceLocator.ResolveType<IFooService2>();
                Assert.IsInstanceOfType(resolveType2, typeof(FooService2));
            }
Example #9
0
            public void RegisterInstance_Valid()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass1 { Name = "My Instance" });

                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());

                var instance = serviceLocator.ResolveType<ITestInterface>();
                Assert.AreEqual("My Instance", instance.Name);
            }
Example #10
0
            public void IsTypeRegistered_UnregisteredTypeRegisteredViaMissingTypeEvent()
            {
                var serviceLocator = new ServiceLocator();
                Assert.IsFalse(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));

                serviceLocator.MissingType += (sender, e) => e.ImplementingType = typeof(TestClass1);
                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
Example #11
0
            public void IsTypeRegistered_RegisteredAsTypeInServiceLocator()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();

                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
Example #12
0
            public void IsTypeRegistered_RegisteredAsInstanceInServiceLocator()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass1());

                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
Example #13
0
            public void IsTypeRegistered_UnregisteredTypeRegisteredInstanceViaMissingType()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.MissingType += (sender, args) =>
                    {
                        args.ImplementingInstance = new TestClass1();
                    };

                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
Example #14
0
            public void IsTypeRegistered_UnregisteredType()
            {
                var serviceLocator = new ServiceLocator();

                Assert.IsFalse(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
Example #15
0
 public void IsTypeRegistered_Null()
 {
     var serviceLocator = new ServiceLocator();
     ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => serviceLocator.IsTypeRegistered(null));
 }
Example #16
0
            public void ExportInstancesToExternalContainers_ExternalContainerHasNoInstanceRegistered()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.AutomaticallyKeepContainersSynchronized = false;

                var ninjectContainer = new StandardKernel();

                serviceLocator.RegisterExternalContainer(ninjectContainer);
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass1());

                serviceLocator.RegisterExternalContainer(ninjectContainer);

                Assert.IsFalse(ninjectContainer.GetBindings(typeof(ITestInterface)).Any());
                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());

                serviceLocator.ExportInstancesToExternalContainers();

                Assert.IsTrue(ninjectContainer.GetBindings(typeof(ITestInterface)).Any());
                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());
            }
Example #17
0
            public void ExportsBothInstancesAndTypes()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.AutomaticallyKeepContainersSynchronized = false;

                var ninjectContainer = new StandardKernel();

                serviceLocator.RegisterExternalContainer(ninjectContainer);
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass1());
                serviceLocator.RegisterType<INotifyPropertyChanged, TestClass1>();

                serviceLocator.RegisterExternalContainer(ninjectContainer);

                Assert.IsFalse(ninjectContainer.GetBindings(typeof(INotifyPropertyChanged)).Any());
                Assert.IsFalse(ninjectContainer.GetBindings(typeof(ITestInterface)).Any());
                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());

                serviceLocator.ExportToExternalContainers();

                Assert.IsTrue(ninjectContainer.GetBindings(typeof(INotifyPropertyChanged)).Any());
                Assert.IsTrue(ninjectContainer.GetBindings(typeof(ITestInterface)).Any());
                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());
            }
Example #18
0
            public void RegisterType_Valid()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();

                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());
            }
Example #19
0
 public void DoesNotRegisterTheImplementationTypesAsTheInterfaceTypesIfIsSetToFalse()
 {
     var serviceLocator = new ServiceLocator { AutoRegisterTypesViaAttributes = false };
     Assert.IsFalse(serviceLocator.IsTypeRegistered<IFooService>());
     Assert.IsFalse(serviceLocator.IsTypeRegistered<IFooService2>());
 }
Example #20
0
 public void ResoleType_Of_Non_Registered_Non_Abstract_Class_Without_Registration()
 {
     var serviceLocator = new ServiceLocator();
     var dependencyInjectionTestClass = serviceLocator.ResolveType<DependencyInjectionTestClass>();
     Assert.IsNotNull(dependencyInjectionTestClass);
     Assert.IsFalse(serviceLocator.IsTypeRegistered(typeof(DependencyInjectionTestClass)));
 }
Example #21
0
 public void DoesNotRegisterAModuleIfModuleManagerDoesNotNotifyThe100PercentOfLoadProgress()
 {
     var serviceLocator = new ServiceLocator();
     var moduleManagerMock = new Mock<IModuleManager>();
     moduleManagerMock.Setup(manager => manager.Run()).Raises(manager => manager.ModuleDownloadProgressChanged += null, new ModuleDownloadProgressChangedEventArgs(new ModuleInfo("FooModule", typeof(FooModule).FullName), 100, 50));
     serviceLocator.RegisterInstance<IModuleManager>(moduleManagerMock.Object);
     new FooBootstrapper(serviceLocator).Run();
     Assert.IsFalse(serviceLocator.IsTypeRegistered<FooModule>());
 }
Example #22
0
            public void ResoleType_Generic_TransientLifestyle()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>(registrationType: RegistrationType.Transient);
                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());

                var firstInstance = serviceLocator.ResolveType<ITestInterface>();
                Assert.IsInstanceOfType(firstInstance, typeof(TestClass1));

                var secondInstance = serviceLocator.ResolveType<ITestInterface>();
                Assert.IsInstanceOfType(secondInstance, typeof(TestClass1));

                Assert.AreNotSame(firstInstance, secondInstance);
            }