当前位置: 首页>>代码示例>>C#>>正文


C# ServiceLocator.RegisterInstance方法代码示例

本文整理汇总了C#中Catel.IoC.ServiceLocator.RegisterInstance方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceLocator.RegisterInstance方法的具体用法?C# ServiceLocator.RegisterInstance怎么用?C# ServiceLocator.RegisterInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Catel.IoC.ServiceLocator的用法示例。


在下文中一共展示了ServiceLocator.RegisterInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Constructor_InjectedServiceLocator

        public void Constructor_InjectedServiceLocator()
        {
            var serviceLocator = new ServiceLocator();
            var messageService = new MessageService();
            serviceLocator.RegisterInstance<IMessageService>(messageService);

            var navigationService = new NavigationService();
            serviceLocator.RegisterInstance<INavigationService>(navigationService);

            var viewModel = new TestViewModel(serviceLocator);
            Assert.AreEqual(messageService, viewModel.GetService<IMessageService>());
            Assert.IsTrue(ReferenceEquals(messageService, viewModel.GetService<IMessageService>()));
            Assert.AreEqual(navigationService, viewModel.GetService<INavigationService>());
            Assert.IsTrue(ReferenceEquals(navigationService, viewModel.GetService<INavigationService>()));
        }
开发者ID:pars87,项目名称:Catel,代码行数:15,代码来源:ViewModelBaseTest.cs

示例2: DisposesAllDisposableInstances

            public void DisposesAllDisposableInstances()
            {
                var isDisposed = false;

                var disposable = new Disposable();
                disposable.Disposed += (sender, e) => isDisposed = true;

                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterInstance(typeof(Disposable), disposable);

                serviceLocator.Dispose();

                Assert.IsTrue(isDisposed);
            }
开发者ID:yshbchenlie,项目名称:Catel,代码行数:14,代码来源:ServiceLocatorFacts.cs

示例3: AutomaticSynchronization_RegisterInstance

            public void AutomaticSynchronization_RegisterInstance()
            {
                var serviceLocator = new ServiceLocator();
                var ninjectContainer = new StandardKernel();
                serviceLocator.RegisterExternalContainer(ninjectContainer);
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass1());

                Assert.IsTrue(ninjectContainer.GetBindings(typeof(ITestInterface)).Any());
            }
开发者ID:pars87,项目名称:Catel,代码行数:9,代码来源:ServiceLocatorFacts.cs

示例4: ResolveType_RegisteredAsInstanceInServiceLocator

            public void ResolveType_RegisteredAsInstanceInServiceLocator()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass2 { Name = "instance test" });

                var instance = serviceLocator.ResolveType<ITestInterface>();
                Assert.AreEqual("instance test", instance.Name);

                instance.Name = "changed name";
                var newInstance = serviceLocator.ResolveType<ITestInterface>();
                Assert.AreEqual("changed name", newInstance.Name);
                Assert.AreEqual(instance, newInstance);
                Assert.IsTrue(object.ReferenceEquals(instance, newInstance));
            }
开发者ID:pars87,项目名称:Catel,代码行数:14,代码来源:ServiceLocatorFacts.cs

示例5: InvokesTypeRegisteredEvent

            public void InvokesTypeRegisteredEvent()
            {
                var serviceLocator = new ServiceLocator();

                TypeRegisteredEventArgs eventArgs = null;

                serviceLocator.TypeRegistered += (sender, args) => { eventArgs = args; };
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass2());

                Assert.IsNotNull(eventArgs);
                Assert.AreEqual(typeof(ITestInterface), eventArgs.ServiceType);
                Assert.AreEqual(typeof(TestClass2), eventArgs.ServiceImplementationType);
                Assert.AreEqual(RegistrationType.Singleton, eventArgs.RegistrationType);
            }
开发者ID:pars87,项目名称:Catel,代码行数:14,代码来源:ServiceLocatorFacts.cs

示例6: ResolvesTypeUsingDependencyInjectionUsesConstructorWithMostParametersFirst

            public void ResolvesTypeUsingDependencyInjectionUsesConstructorWithMostParametersFirst()
            {
                var serviceLocator = new ServiceLocator();
                var typeFactory = serviceLocator.ResolveType<ITypeFactory>();

                var iniEntry = new IniEntry { Group = "group", Key = "key", Value = "value" };
                serviceLocator.RegisterInstance(iniEntry);
                serviceLocator.RegisterInstance(42);
                serviceLocator.RegisterInstance("hi there");

                var instance = typeFactory.CreateInstance<DependencyInjectionTestClass>();

                Assert.IsFalse(instance.UsedDefaultConstructor);
                Assert.AreEqual(iniEntry, instance.IniEntry);
                Assert.AreEqual(42, instance.IntValue);
                Assert.AreEqual("hi there", instance.StringValue);
            }
开发者ID:pars87,项目名称:Catel,代码行数:17,代码来源:TypeFactoryFacts.cs

示例7: TheOpenedActionIsCalledWhenViewManagerHaveRegisteredAViewForTheViewModel

            public void TheOpenedActionIsCalledWhenViewManagerHaveRegisteredAViewForTheViewModel()
            {
                 var serviceLocator = new ServiceLocator();
                var fooViewModel = new FooViewModel(serviceLocator);

                var dispatcherServiceMock = new Mock<IDispatcherService>();
                dispatcherServiceMock.Setup(service => service.Invoke(It.IsAny<Action>())).Callback((Action action) => action.Invoke());
                var visualizerServiceMock = new Mock<IUIVisualizerService>();
                visualizerServiceMock.Setup(service => service.Show(It.Is<FooViewModel>(model => ReferenceEquals(model, fooViewModel)), null)).Returns(true);
                var viewManagerMock = new Mock<IViewManager>();
                viewManagerMock.Setup(manager => manager.GetViewsOfViewModel(fooViewModel)).Returns(new IView[] { new FooViewModelView(fooViewModel) });
                
                serviceLocator.RegisterInstance<IDispatcherService>(dispatcherServiceMock.Object);
            	serviceLocator.RegisterInstance<IUIVisualizerService>(visualizerServiceMock.Object);
            	serviceLocator.RegisterInstance<IViewManager>(viewManagerMock.Object);
            
                serviceLocator.ResolveType<IUIVisualizerService>().Show(fooViewModel, () =>
                    {
                        visualizerServiceMock.Verify(service => service.Show(It.Is<FooViewModel>(model => ReferenceEquals(model, fooViewModel)), null), Times.Once());
                        viewManagerMock.Verify(manager => manager.GetViewsOfViewModel(fooViewModel), Times.AtLeastOnce());  
                        this.EnqueueTestComplete();
                    });
            }
开发者ID:pars87,项目名称:Catel,代码行数:23,代码来源:UIVisualizerServiceExtensionsTests.cs

示例8: DoesNotRegisterAModuleIfModuleManagerDoesNotNotifyThe100PercentOfLoadProgress

 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>());
 }
开发者ID:pars87,项目名称:Catel,代码行数:9,代码来源:BootstrapperBaseTests.cs

示例9: IsTypeRegistered_RegisteredAsInstanceInServiceLocator

            public void IsTypeRegistered_RegisteredAsInstanceInServiceLocator()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass1());

                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
开发者ID:pars87,项目名称:Catel,代码行数:7,代码来源:ServiceLocatorFacts.cs

示例10: ReturnsAllTypesWhenAllAreRegistered

            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());
            }
开发者ID:pars87,项目名称:Catel,代码行数:15,代码来源:ServiceLocatorFacts.cs

示例11: ReturnsAllAvaliableInstances

 public void ReturnsAllAvaliableInstances()
 {
     var serviceLocator = new ServiceLocator { AutoRegisterTypesViaAttributes = true };
     serviceLocator.RegisterInstance(typeof(IFooService), new FooService2(), "FooService3");
     Assert.AreEqual(3, serviceLocator.ResolveTypes<IFooService>().Count());
 }
开发者ID:pars87,项目名称:Catel,代码行数:6,代码来源:ServiceLocatorFacts.cs

示例12: ResolvesUsingPreferredTagAndDataContext

            public void ResolvesUsingPreferredTagAndDataContext()
            {
                var serviceLocator = new ServiceLocator();

                var noTagDependency = new DummyDependency
                {
                    Value = "no tag"
                };

                var tagDependency = new DummyDependency
                {
                    Value = "tag"
                };

                serviceLocator.RegisterInstance<IDummyDependency>(noTagDependency);
                serviceLocator.RegisterInstance<IDummyDependency>(tagDependency, "tag");

                var typeFactory = serviceLocator.ResolveType<ITypeFactory>();

                var viewModelFactory = new ViewModelFactory(typeFactory);
                var viewModel = viewModelFactory.CreateViewModel<TestViewModel>(5, "tag");

                Assert.IsTrue(ReferenceEquals(tagDependency, viewModel.Dependency));
            }
开发者ID:Catel,项目名称:Catel,代码行数:24,代码来源:ViewModelFactoryFacts.cs

示例13: GetInstanceOfAutoPopulateRegionBehaviorReturnsAlwaysANewOne

        public void GetInstanceOfAutoPopulateRegionBehaviorReturnsAlwaysANewOne()
        {
            var serviceLocator = new ServiceLocator();
            serviceLocator.RegisterType<IRegionViewRegistry, RegionViewRegistry>();

            var adapter = new ServiceLocatorAdapter(serviceLocator);
            serviceLocator.RegisterInstance<IServiceLocator>(adapter);

            var behavior1 = adapter.GetInstance<AutoPopulateRegionBehavior>();
            var behavior2 = adapter.GetInstance<AutoPopulateRegionBehavior>();

            Assert.AreNotSame(behavior1, behavior2);
        }
开发者ID:pars87,项目名称:Catel,代码行数:13,代码来源:ServiceLocatorAdapterTests.cs

示例14: ExportInstancesToExternalContainers_ExternalContainerHasNoInstanceRegistered

            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>());
            }
开发者ID:pars87,项目名称:Catel,代码行数:20,代码来源:ServiceLocatorFacts.cs

示例15: RegisterInstance_InterfaceTypeNull

 public void RegisterInstance_InterfaceTypeNull()
 {
     var serviceLocator = new ServiceLocator();
     ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(
         () => serviceLocator.RegisterInstance(null, new TestClass1 { Name = "My Instance" }, null));
 }
开发者ID:pars87,项目名称:Catel,代码行数:6,代码来源:ServiceLocatorFacts.cs


注:本文中的Catel.IoC.ServiceLocator.RegisterInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。