Esempio n. 1
0
        /// <summary>
        /// Add service to the service manager container
        /// </summary>
        /// <typeparam name="T">Service type</typeparam>
        /// <param name="service">Service instance</param>
        /// <param name="type">
        /// Optional type to register the instance for. In Visual Studio
        /// some global services are registered as 'SVsService` while
        /// actual interface type is IVsService.
        /// </param>
        public virtual IServiceManager AddService <T>(T service, Type type = null) where T : class
        {
            _disposeToken.ThrowIfDisposed();

            type = type ?? typeof(T);
            Check.ArgumentNull(nameof(service), service);
            Check.InvalidOperation(() => _s.TryAdd(type, service), "Service already exists");
            ServiceAdded?.Invoke(this, new ServiceContainerEventArgs(type));
            return(this);
        }
Esempio n. 2
0
        /// <summary>
        /// Add service to the service manager container
        /// </summary>
        /// <typeparam name="T">Service type</typeparam>
        /// <param name="service">Service instance</param>
        public virtual IServiceManager AddService <T>(T service) where T : class
        {
            _disposeToken.ThrowIfDisposed();

            var type = typeof(T);

            Check.ArgumentNull(nameof(service), service);
            Check.Operation(() => _s.TryAdd(type, service));
            ServiceAdded?.Invoke(this, new ServiceContainerEventArgs(type));
            return(this);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds on-demand created service
        /// </summary>
        /// <param name="type">Service type</param>
        /// <param name="factory">Optional creator function. If not provided, reflection with default constructor will be used.</param>
        /// <param name="parameters">Factory parameters</param>
        public virtual IServiceManager AddService(Type type, Func <object> factory)
        {
            Check.ArgumentNull(nameof(type), type);
            lock (_lock) {
                Check.InvalidOperation(() => _services.ContainsKey(type));
                Check.InvalidOperation(() => _deferredServices.ContainsKey(type));
                _deferredServices[type] = factory;
            }

            ServiceAdded?.Invoke(this, new ServiceContainerEventArgs(type));
            return(this);
        }
Esempio n. 4
0
        /// <summary>
        /// Add service to the service manager container
        /// </summary>
        /// <typeparam name="T">Service type</typeparam>
        /// <param name="service">Service instance</param>
        public virtual IServiceManager AddService <T>(T service) where T : class
        {
            var type = typeof(T);

            Check.ArgumentNull(nameof(service), service);
            lock (_lock) {
                Check.InvalidOperation(() => _services.ContainsKey(type));
                _services[type] = service;
            }
            ServiceAdded?.Invoke(this, new ServiceContainerEventArgs(type));
            return(this);
        }
Esempio n. 5
0
        /// <summary>
        /// Adds on-demand created service
        /// </summary>
        /// <param name="factory">Optional creator function. If not provided, reflection with default constructor will be used.</param>
        public virtual IServiceManager AddService <T>(Func <T> factory) where T : class
        {
            _disposeToken.ThrowIfDisposed();

            var type = typeof(T);
            var lazy = factory != null
                ? new Lazy <object>(() => factory())
                : new Lazy <object>(() => Activator.CreateInstance(type));

            Check.InvalidOperation(() => _s.TryAdd(type, lazy), "Service already exists");
            ServiceAdded?.Invoke(this, new ServiceContainerEventArgs(typeof(T)));
            return(this);
        }
Esempio n. 6
0
        protected void AddService <T>(T serviceInstance) where T : class
        {
            var added = false;

            lock (_lock) {
                if (GetService <T>(false) == null)
                {
                    _servicesByType.Add(typeof(T), serviceInstance);
                    added = true;
                }
            }

            if (added)
            {
                ServiceAdded?.Invoke(this, new ServiceManagerEventArgs(typeof(T), serviceInstance));
            }
        }
Esempio n. 7
0
 private void OnServiceAdded(ServiceEventArgs e)
 {
     ServiceAdded?.Invoke(this, e);
 }
Esempio n. 8
0
 internal void OnServiceAdded(EntityApp app, Type serviceType, object serviceInstance)
 {
     ServiceAdded?.Invoke(this, new ServiceEventArgs(app, serviceType, serviceInstance));
 }
Esempio n. 9
0
 public void AddService(object service)
 {
     ImmutableInterlocked.Update(ref services, p => p.Add(service));
     ServiceAdded?.Invoke(this, service);
 }
Esempio n. 10
0
 /// <summary>
 /// Add a HEROsModService to the ServiceController
 /// </summary>
 /// <param name="service">Service to add</param>
 public void AddService(HEROsModService service)
 {
     _services.Add(service);
     ServiceAdded?.Invoke(service);
 }