RegisterInstance() public method

Registers a specific instance of a service.
The is null. The is null. The is not of the right type.
public RegisterInstance ( Type serviceType, object instance, object tag = null ) : void
serviceType System.Type Type of the service.
instance object The instance.
tag object The tag to register the service with. The default value is null.
return void
Ejemplo n.º 1
0
        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>()));
        }
Ejemplo n.º 2
0
            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);
            }
Ejemplo n.º 3
0
            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());
            }
Ejemplo n.º 4
0
            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));
            }
Ejemplo n.º 5
0
            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);
            }
Ejemplo n.º 6
0
            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();
                    });
            }
Ejemplo n.º 8
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>());
 }
Ejemplo n.º 9
0
            public void IsTypeRegistered_RegisteredAsInstanceInServiceLocator()
            {
                var serviceLocator = new ServiceLocator();
                serviceLocator.RegisterInstance<ITestInterface>(new TestClass1());

                Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
            }
Ejemplo n.º 10
0
            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());
            }
Ejemplo n.º 11
0
 public void ReturnsAllAvaliableInstances()
 {
     var serviceLocator = new ServiceLocator { AutoRegisterTypesViaAttributes = true };
     serviceLocator.RegisterInstance(typeof(IFooService), new FooService2(), "FooService3");
     Assert.AreEqual(3, serviceLocator.ResolveTypes<IFooService>().Count());
 }
Ejemplo n.º 12
0
            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));
            }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
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>());
            }
Ejemplo n.º 15
0
 public void RegisterInstance_InterfaceTypeNull()
 {
     var serviceLocator = new ServiceLocator();
     ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(
         () => serviceLocator.RegisterInstance(null, new TestClass1 { Name = "My Instance" }, null));
 }
Ejemplo n.º 16
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>());
            }
Ejemplo n.º 17
0
 public void RegisterInstance_InstanceNull()
 {
     var serviceLocator = new ServiceLocator();
     ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(
         () => serviceLocator.RegisterInstance(typeof(ITestInterface), null, null));
 }
Ejemplo n.º 18
0
 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());
 }
Ejemplo n.º 19
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);
            }
            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());
            }
Ejemplo n.º 21
0
            public void ThrowsArgumentExceptionForWrongServiceType()
            {
                var serviceLocator = new ServiceLocator();
                var serviceType = typeof(IViewModelLocator);
                var serviceInstance = new ViewLocator();

                ExceptionTester.CallMethodAndExpectException<ArgumentException>(() => serviceLocator.RegisterInstance(serviceType, serviceInstance));
            }
Ejemplo n.º 22
0
            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);
            }
Ejemplo n.º 23
0
            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));
            }