本文整理汇总了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>()));
}
示例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);
}
示例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());
}
示例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));
}
示例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);
}
示例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);
}
示例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();
});
}
示例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>());
}
示例9: IsTypeRegistered_RegisteredAsInstanceInServiceLocator
public void IsTypeRegistered_RegisteredAsInstanceInServiceLocator()
{
var serviceLocator = new ServiceLocator();
serviceLocator.RegisterInstance<ITestInterface>(new TestClass1());
Assert.IsTrue(serviceLocator.IsTypeRegistered(typeof(ITestInterface)));
}
示例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());
}
示例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());
}
示例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));
}
示例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);
}
示例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>());
}
示例15: RegisterInstance_InterfaceTypeNull
public void RegisterInstance_InterfaceTypeNull()
{
var serviceLocator = new ServiceLocator();
ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(
() => serviceLocator.RegisterInstance(null, new TestClass1 { Name = "My Instance" }, null));
}