コード例 #1
0
        public void GetRegisteredServiceCallsInitializeOnService(WorkbenchServiceType type, IWorkbenchService service)
        {
            Workbench.Instance.AddToCache(type, service);

            Workbench.Instance.GetRegisteredService(type);

            service.Received(1).Initialize();
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        public IWorkbenchService GetRegisteredService(WorkbenchServiceType serviceType)
        {
            if (!_isInitialized)
            {
                // Trigger initializations over Workbench Services on demand
                InitializeServices();
            }

            _singletonCache.TryGetValue(serviceType, out var service);

            return(service);
        }
コード例 #6
0
        public void GetRegisteredServiceReturnsWhatWasAddedByAddToCache(WorkbenchServiceType type, IWorkbenchService service)
        {
            Workbench.Instance.AddToCache(type, service);

            Assert.Same(service, Workbench.Instance.GetRegisteredService(type));
        }