public void GetRegisteredServiceCallsInitializeOnService(WorkbenchServiceType type, IWorkbenchService service) { Workbench.Instance.AddToCache(type, service); Workbench.Instance.GetRegisteredService(type); service.Received(1).Initialize(); }
/// <summary> /// Adds a <see cref="IWorkbenchService"/> to the cache. /// </summary> /// <param name="type">The type of the service being added.</param> /// <param name="service">The actual service instance.</param> public void AddToCache(WorkbenchServiceType type, IWorkbenchService service) { if (_singletonCache.ContainsKey(type)) { var message = $"The SingletonType '{type}' has been already set. Only one Workbench service of a given type is allowed."; throw new ArgumentException(message, nameof(type)); } _singletonCache.Add(type, service); }
public void AddToCacheSameServiceTypeTwiceThrows(WorkbenchServiceType type, IWorkbenchService service1, IWorkbenchService service2) { Workbench.Instance.AddToCache(type, service1); // throws with the same service Assert.ThrowsAny <Exception>(() => Workbench.Instance.AddToCache(type, service1)); // and with a different service Assert.ThrowsAny <Exception>(() => Workbench.Instance.AddToCache(type, service2)); }
private T GetService <T>(WorkbenchServiceType type, bool mandatory = true) { if (!_singletonCache.TryGetValue(type, out var service)) { if (mandatory) { throw new ArgumentException($"No Workbench service of type '{type.ToString()}' was injected on Startup."); } } return((T)service); }
public IWorkbenchService GetRegisteredService(WorkbenchServiceType serviceType) { if (!_isInitialized) { // Trigger initializations over Workbench Services on demand InitializeServices(); } _singletonCache.TryGetValue(serviceType, out var service); return(service); }
public void GetRegisteredServiceReturnsWhatWasAddedByAddToCache(WorkbenchServiceType type, IWorkbenchService service) { Workbench.Instance.AddToCache(type, service); Assert.Same(service, Workbench.Instance.GetRegisteredService(type)); }