RegisterType() public method

Registers an implementation of a service using a create type callback, but only if the type is not yet registered.
Note that the actual implementation lays in the hands of the IoC technique being used.
If is null. If is null.
public RegisterType ( Type serviceType, object>.Func createServiceFunc, object tag = null, RegistrationType registrationType = RegistrationType.Singleton, bool registerIfAlreadyRegistered = true ) : void
serviceType System.Type The type of the service.
createServiceFunc object>.Func The create service function.
tag object The tag to register the service with. The default value is null.
registrationType RegistrationType The registration type. The default value is .
registerIfAlreadyRegistered bool If set to true, an older type registration is overwritten by this new one.
return void
コード例 #1
0
        public override void Prepare()
        {
            var serviceLocator = new ServiceLocator();

            serviceLocator.RegisterType<ISingleton, Singleton>(RegistrationType.Singleton);

            serviceLocator.RegisterType<ITransient, Transient>(RegistrationType.Transient);

            serviceLocator.RegisterType<ICombined, Combined>(RegistrationType.Transient);

            container = serviceLocator;
        }
コード例 #2
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
 public void ResolvesInstancesOfTypeRegisteredWithSingletonParameterToFalseFromNinjectContainer()
 {
     var serviceLocator = new ServiceLocator();
     var standardKernel = new StandardKernel();
     serviceLocator.RegisterExternalContainer(standardKernel);
     serviceLocator.RegisterType<ITestInterface, TestClass1>(registrationType: RegistrationType.Transient);
     Assert.AreNotSame(standardKernel.Get<ITestInterface>(), standardKernel.Get<ITestInterface>());
 }
コード例 #3
0
            public void ReturnsTrueForRegisteredType()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<IMessageService, MessageService>();
                var dependencyResolver = serviceLocator.ResolveType<IDependencyResolver>();

                Assert.IsTrue(dependencyResolver.CanResolve(typeof(IMessageService)));
            }
コード例 #4
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
 public void ResolvesInstancesOfTypeRegisteredWithSingletonParameterToTrueFromWindsorContainer()
 {
     var serviceLocator = new ServiceLocator();
     var windsorContainer = new WindsorContainer();
     serviceLocator.RegisterExternalContainer(windsorContainer);
     serviceLocator.RegisterType<ITestInterface, TestClass1>();
     Assert.AreSame(windsorContainer.Resolve<ITestInterface>(), windsorContainer.Resolve<ITestInterface>());
 }
コード例 #5
0
 public void GetAllInstanceJustReturnsCollectionOfWithOnylOneResolvedInstanceIfTheTypeIsRegistered()
 {
     var serviceLocator = new ServiceLocator();
     serviceLocator.RegisterType<IFooInterface, FooNonAbstractClass>();
     var serviceLocatorAdapter = new ServiceLocatorAdapter(serviceLocator);
     IFooInterface[] list = serviceLocatorAdapter.GetAllInstances<IFooInterface>().ToArray();
     Assert.AreEqual(1, list.Length);
 }
コード例 #6
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
 public void ResolvesInstancesOfTypeRegisteredWithSingletonParameterToFalseFromUnityContainer()
 {
     var serviceLocator = new ServiceLocator();
     var unityContainer = new UnityContainer();
     serviceLocator.RegisterExternalContainer(unityContainer);
     serviceLocator.RegisterType<ITestInterface, TestClass1>(registrationType: RegistrationType.Transient);
     Assert.AreNotSame(unityContainer.Resolve<ITestInterface>(), unityContainer.Resolve<ITestInterface>());
 }
コード例 #7
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            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);
            }
コード例 #8
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void InvokesTypeRegisteredEvent()
            {
                var serviceLocator = new ServiceLocator();

                TypeRegisteredEventArgs eventArgs = null;

                serviceLocator.TypeRegistered += (sender, args) => { eventArgs = args; };
                serviceLocator.RegisterType<ITestInterface, TestClass2>(registrationType: RegistrationType.Transient);

                Assert.IsNotNull(eventArgs);
                Assert.AreEqual(typeof(ITestInterface), eventArgs.ServiceType);
                Assert.AreEqual(typeof(TestClass2), eventArgs.ServiceImplementationType);
                Assert.AreEqual(RegistrationType.Transient, eventArgs.RegistrationType);
            }
コード例 #9
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ThrowsNotSupportedExceptionWhenAtLeastOneTypeIsNotRegistered()
            {
                var serviceLocator = new ServiceLocator();

                serviceLocator.RegisterType<object>();
                serviceLocator.RegisterType<ITestInterface1, TestClass1>();

                ExceptionTester.CallMethodAndExpectException<NotSupportedException>(() => serviceLocator.ResolveAllTypes(typeof(object), typeof(ITestInterface1), typeof(ITestInterface2)));
            }
コード例 #10
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ResolvesTypeUsingDependencyInjectionUsesConstructorWithMostParametersFirst()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<DependencyInjectionTestClass, DependencyInjectionTestClass>();
                var iniEntry = new IniEntry { Group = "group", Key = "key", Value = "value" };
                serviceLocator.RegisterInstance(iniEntry);
                serviceLocator.RegisterInstance(42);
                serviceLocator.RegisterInstance("hi there");

                var instance = serviceLocator.ResolveType<DependencyInjectionTestClass>();

                Assert.IsFalse(instance.UsedDefaultConstructor);
                Assert.AreEqual(iniEntry, instance.IniEntry);
                Assert.AreEqual(42, instance.IntValue);
                Assert.AreEqual("hi there", instance.StringValue);
            }
コード例 #11
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ResolveType_RegisteredAsTypeInServiceLocator()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();

                var instance = serviceLocator.ResolveType<ITestInterface>();
                Assert.IsInstanceOfType(instance, typeof(TestClass1));
            }
コード例 #12
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
 public void TheIsTypeRegisteredAsSingleton_NonSingleton()
 {
     var serviceLocator = new ServiceLocator();
     serviceLocator.RegisterType<ITestInterface, TestClass1>(registrationType: RegistrationType.Transient);
     Assert.IsFalse(serviceLocator.IsTypeRegisteredAsSingleton(typeof(ITestInterface)));
 }
コード例 #13
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
 public void TheIsTypeRegisteredAsSingleton_Generic()
 {
     var serviceLocator = new ServiceLocator();
     serviceLocator.RegisterType<ITestInterface, TestClass1>();
     Assert.IsTrue(serviceLocator.IsTypeRegisteredAsSingleton<ITestInterface>());
 }
コード例 #14
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void OverridesRegistrationWithSameTag()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType(typeof(ITestInterface), typeof(TestClass1), "1");
                serviceLocator.RegisterType(typeof(ITestInterface), typeof(TestClass1), "2");
                serviceLocator.RegisterType(typeof(ITestInterface), typeof(TestClass2), "1");

                var firstService = serviceLocator.ResolveType(typeof(ITestInterface), "1");
                Assert.AreEqual(typeof(TestClass2), firstService.GetType());
            }
コード例 #15
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            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"));
            }
コード例 #16
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void RemovesAllInstances()
            {
                var serviceLocator = new ServiceLocator { AutoRegisterTypesViaAttributes = true };
                serviceLocator.RegisterType(typeof(IFoo2Service), typeof(Foo2Service), "FooService2");

                var instance1 = serviceLocator.ResolveType(typeof(IFooService), "FooService1");
                var instance2 = serviceLocator.ResolveType(typeof(IFooService), "FooService2");
                var instance3 = serviceLocator.ResolveType(typeof(IFoo2Service), "FooService2");

                serviceLocator.RemoveAllInstances();

                Assert.AreNotEqual(instance1, serviceLocator.ResolveType(typeof(IFooService), "FooService1"));
                Assert.AreNotEqual(instance2, serviceLocator.ResolveType(typeof(IFooService), "FooService2"));
                Assert.AreNotEqual(instance3, serviceLocator.ResolveType(typeof(IFoo2Service), "FooService2"));
            }
コード例 #17
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ReturnsAllTypesWhenAllAreRegistered()
            {
                var serviceLocator = new ServiceLocator();

                serviceLocator.RegisterInstance(new object());
                serviceLocator.RegisterType<ITestInterface1, TestClass1>();
                serviceLocator.RegisterType<ITestInterface2, TestClass2>();

                var resolvedTypes = serviceLocator.ResolveAllTypes(typeof(object), typeof(ITestInterface1), typeof(ITestInterface2)).ToArray();

                Assert.AreEqual(3, resolvedTypes.Length);
                Assert.AreEqual(typeof(object), resolvedTypes[0].GetType());
                Assert.AreEqual(typeof(TestClass1), resolvedTypes[1].GetType());
                Assert.AreEqual(typeof(TestClass2), resolvedTypes[2].GetType());
            }
コード例 #18
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            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);
            }
コード例 #19
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ResolveType_Generic()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();

                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());
                Assert.IsInstanceOfType(serviceLocator.ResolveType<ITestInterface>(), typeof(TestClass1));
            }
コード例 #20
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void IsTypeRegistered_RegisteredAsTypeInServiceLocator()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();

                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
コード例 #21
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ResolvesTypeUsingDependencyInjectionFallBackToDefaultConstructor()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<DependencyInjectionTestClass, DependencyInjectionTestClass>();

                var instance = serviceLocator.ResolveType<DependencyInjectionTestClass>();

                Assert.IsTrue(instance.UsedDefaultConstructor);
            }
コード例 #22
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void AutomaticSynchronization_ResolveType()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.AutomaticallyKeepContainersSynchronized = false;
                serviceLocator.RegisterType<ITestInterface, TestClass1>();
                var ninjectContainer = new StandardKernel();
                serviceLocator.RegisterExternalContainer(ninjectContainer);
                serviceLocator.AutomaticallyKeepContainersSynchronized = true;
                serviceLocator.ResolveType<ITestInterface>();

                Assert.IsTrue(ninjectContainer.GetBindings(typeof(ITestInterface)).Any());
            }
コード例 #23
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ResolvesTypeUsingDependencyInjectionFallBackToSecondConstructor()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<DependencyInjectionTestClass, DependencyInjectionTestClass>();
                var iniEntry = new IniEntry { Group = "group", Key = "key", Value = "value" };
                serviceLocator.RegisterInstance(iniEntry);
                serviceLocator.RegisterInstance(42);

                var instance = serviceLocator.ResolveType<DependencyInjectionTestClass>();

                Assert.IsFalse(instance.UsedDefaultConstructor);
                Assert.AreEqual(iniEntry, instance.IniEntry);
                Assert.AreEqual(42, instance.IntValue);
                Assert.AreEqual(null, instance.StringValue);
            }
コード例 #24
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ReturnsFalseWhenAtLeastOneTypeIsNotRegistered()
            {
                var serviceLocator = new ServiceLocator();

                serviceLocator.RegisterType<object>();
                serviceLocator.RegisterType<ITestInterface1, TestClass1>();

                Assert.IsFalse(serviceLocator.AreAllTypesRegistered(typeof(object), typeof(ITestInterface1), typeof(ITestInterface2)));
            }
コード例 #25
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
 public void RegisterType_ImplementingTypeNull()
 {
     var serviceLocator = new ServiceLocator();
     ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => serviceLocator.RegisterType(typeof(ITestInterface), null, null, RegistrationType.Singleton, true, null));
 }
コード例 #26
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void ReturnsTrueWhenAllTypesAreRegistered()
            {
                var serviceLocator = new ServiceLocator();

                serviceLocator.RegisterType<object>();
                serviceLocator.RegisterType<ITestInterface1, TestClass1>();
                serviceLocator.RegisterType<ITestInterface2, TestClass2>();

                Assert.IsTrue(serviceLocator.AreAllTypesRegistered(typeof(object), typeof(ITestInterface1), typeof(ITestInterface2)));
            }
コード例 #27
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            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>());
            }
コード例 #28
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void RegisterType_Valid()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();

                Assert.IsTrue(serviceLocator.IsTypeRegistered<ITestInterface>());
            }
コード例 #29
0
ファイル: TypeFactoryFacts.cs プロジェクト: pars87/Catel
            public void ThrowsCircularDependencyExceptionForInvalidTypeRequestPath()
            {
                var serviceLocator = new ServiceLocator();
                var typeFactory = serviceLocator.ResolveType<ITypeFactory>();

                serviceLocator.RegisterType<X>();
                serviceLocator.RegisterType<Y>();
                serviceLocator.RegisterType<Z>();

                var ex = ExceptionTester.CallMethodAndExpectException<CircularDependencyException>(() => typeFactory.CreateInstance<X>());

                Assert.AreEqual(4, ex.TypePath.AllTypes.Length);
                Assert.AreEqual(typeof(X), ex.TypePath.FirstType.Type);
                Assert.AreEqual(typeof(X), ex.TypePath.LastType.Type);
            }
コード例 #30
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
            public void RegisterType_DoubleRegistration_ToChangeInstantiationStyle_And_Type()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterType<ITestInterface, TestClass1>();
                var testInterfaceRef1 = serviceLocator.ResolveType<ITestInterface>();
                var testInterfaceRef2 = serviceLocator.ResolveType<ITestInterface>();
                Assert.AreSame(testInterfaceRef1, testInterfaceRef2);
                Assert.AreEqual(testInterfaceRef2.GetType(), typeof(TestClass1));

                serviceLocator.RegisterType<ITestInterface, TestClass2>(registrationType: RegistrationType.Transient);
                testInterfaceRef1 = serviceLocator.ResolveType<ITestInterface>();
                testInterfaceRef2 = serviceLocator.ResolveType<ITestInterface>();
                Assert.AreNotSame(testInterfaceRef1, testInterfaceRef2);

                Assert.AreEqual(testInterfaceRef2.GetType(), typeof(TestClass2));
            }