public RegisterInstance ( |
||
serviceType | Type of the service. | |
instance | object | The instance. |
tag | object | The tag to register the service with. The default value is |
return | void |
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>())); }
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); }
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()); }
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)); }
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); }
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); }
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(); }); }
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>()); }
public void IsTypeRegistered_RegisteredAsInstanceInServiceLocator() { var serviceLocator = new ServiceLocator(); serviceLocator.RegisterInstance<ITestInterface>(new TestClass1()); Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface))); }
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()); }
public void ReturnsAllAvaliableInstances() { var serviceLocator = new ServiceLocator { AutoRegisterTypesViaAttributes = true }; serviceLocator.RegisterInstance(typeof(IFooService), new FooService2(), "FooService3"); Assert.AreEqual(3, serviceLocator.ResolveTypes<IFooService>().Count()); }
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)); }
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); }
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>()); }
public void RegisterInstance_InterfaceTypeNull() { var serviceLocator = new ServiceLocator(); ExceptionTester.CallMethodAndExpectException<ArgumentNullException>( () => serviceLocator.RegisterInstance(null, new TestClass1 { Name = "My Instance" }, null)); }
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>()); }
public void RegisterInstance_InstanceNull() { var serviceLocator = new ServiceLocator(); ExceptionTester.CallMethodAndExpectException<ArgumentNullException>( () => serviceLocator.RegisterInstance(typeof(ITestInterface), null, null)); }
public void RegistersTheBootTasksAndCallsCommitAsync() { var splashScreenServiceMock = new Mock<ISplashScreenService>(); var serviceLocator = new ServiceLocator(); serviceLocator.RegisterInstance<ISplashScreenService>(splashScreenServiceMock.Object); new FooBootstrapper(serviceLocator).RunWithSplashScreen<ProgressNotifyableViewModel>(); splashScreenServiceMock.Verify(service => service.Enqueue(It.IsAny<ITask>()), Times.AtLeast(12)); splashScreenServiceMock.Verify(service => service.CommitAsync<ProgressNotifyableViewModel>(null), Times.Once()); }
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); }
public void TheOpenedActionIsCalledEvenWhenThereNoViewsAvailablesInTheExpectedTimeForTheCurrentViewModelButUnlockingTheInspectionThread() { 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[] { }); serviceLocator.RegisterInstance<IDispatcherService>(dispatcherServiceMock.Object); serviceLocator.RegisterInstance<IUIVisualizerService>(visualizerServiceMock.Object); serviceLocator.RegisterInstance<IViewManager>(viewManagerMock.Object); var @event = new AutoResetEvent(false); serviceLocator.ResolveType<IUIVisualizerService>().Show(fooViewModel, () => @event.Set()); @event.WaitOne(20000); visualizerServiceMock.Verify(service => service.Show(It.Is<FooViewModel>(model => ReferenceEquals(model, fooViewModel)), null), Times.Once()); viewManagerMock.Verify(manager => manager.GetViewsOfViewModel(fooViewModel), Times.AtLeastOnce()); }
public void ThrowsArgumentExceptionForWrongServiceType() { var serviceLocator = new ServiceLocator(); var serviceType = typeof(IViewModelLocator); var serviceInstance = new ViewLocator(); ExceptionTester.CallMethodAndExpectException<ArgumentException>(() => serviceLocator.RegisterInstance(serviceType, serviceInstance)); }
public void ResolvesTypeUsingDependencyInjectionFallBackToFirstConstructor() { var serviceLocator = new ServiceLocator(); var typeFactory = serviceLocator.ResolveType<ITypeFactory>(); var iniEntry = new IniEntry { Group = "group", Key = "key", Value = "value" }; serviceLocator.RegisterInstance(iniEntry); var instance = typeFactory.CreateInstance<DependencyInjectionTestClass>(); Assert.IsFalse(instance.UsedDefaultConstructor); Assert.AreEqual(iniEntry, instance.IniEntry); Assert.AreEqual(0, instance.IntValue); Assert.AreEqual(null, instance.StringValue); }
public void CreatesTypeWhenDynamicConstructorIsAvailable() { var serviceLocator = new ServiceLocator(); var noTagDependency = new DummyDependency { Value = "no tag" }; var tagDependency = new DummyDependency { Value = "tag" }; serviceLocator.RegisterType<IDispatcherService, DispatcherService>(); serviceLocator.RegisterType<IMessageService, MessageService>(); serviceLocator.RegisterType<INavigationService, NavigationService>(); serviceLocator.RegisterType<INavigationRootService, NavigationRootService>(); serviceLocator.RegisterInstance<IDummyDependency>(noTagDependency); serviceLocator.RegisterInstance<IDummyDependency>(tagDependency, "tag"); var typeFactory = serviceLocator.ResolveType<ITypeFactory>(); var instance = typeFactory.CreateInstanceWithParametersAndAutoCompletionWithTag<AdvancedDependencyInjectionTestClass>("tag", "string", 42, 42L); Assert.IsNotNull(instance); Assert.AreEqual("string", instance.StringValue); Assert.AreEqual(42, instance.IntValue); Assert.AreEqual(42L, instance.LongValue); Assert.IsTrue(ReferenceEquals(tagDependency, instance.Dependency)); }