/// <summary> /// Gets the services that depend on the specified service type. /// </summary> /// <param name="service">The service type.</param> /// <returns> /// A collection containing a mapping between the demanded dependency and the service that would /// serve for this dependency. /// </returns> private IEnumerable <KeyValuePair <Type, KernelService> > GetDependentServices(KernelService service) { var result = new List <KeyValuePair <Type, KernelService> >(); foreach (var map in m_Services) { var dependencyHolder = map.Value as IHaveServiceDependencies; if (dependencyHolder != null) { foreach (var dependency in dependencyHolder.ServicesToConnectTo()) { if (dependency.IsAssignableFrom(service.GetType())) { result.Add(new KeyValuePair <Type, KernelService>(dependency, map.Value)); break; } } } } return(result); }
/// <summary> /// Gets the services that depend on the specified service type. /// </summary> /// <param name="service">The service type.</param> /// <returns> /// A collection containing a mapping between the demanded dependency and the service that would /// serve for this dependency. /// </returns> private IEnumerable<KeyValuePair<Type, KernelService>> GetDependentServices(KernelService service) { var result = new List<KeyValuePair<Type, KernelService>>(); foreach (var map in m_Services) { var dependencyHolder = map.Value as IHaveServiceDependencies; if (dependencyHolder != null) { foreach (var dependency in dependencyHolder.ServicesToConnectTo()) { if (dependency.IsAssignableFrom(service.GetType())) { result.Add(new KeyValuePair<Type, KernelService>(dependency, map.Value)); break; } } } } return result; }
/// <summary> /// Installs the specified service. /// </summary> /// <param name="service">The service which should be installed.</param> /// <remarks> /// <para> /// Only services that are 'installed' can be used by the service manager. /// Services that have not been installed are simply unknown to the service /// manager. /// </para> /// <para> /// Note that only one instance for each <c>Type</c> can be provided to /// the service manager. /// </para> /// </remarks> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="service"/> is <see langword="null" />. /// </exception> public void Install(KernelService service) { { Enforce.Argument(() => service); } if ((m_State != StartupState.NotStarted) && (m_State != StartupState.Started)) { throw new KernelNotInInstallReadyStateException(); } if (m_Services.ContainsKey(service.GetType())) { throw new ServiceTypeAlreadyInstalledException(); } var dependencyHolder = service as IHaveServiceDependencies; if (dependencyHolder != null) { if (dependencyHolder.ServicesToConnectTo().Contains(dependencyHolder.GetType())) { throw new ServiceCannotDependOnItselfException(); } if (dependencyHolder.ServicesToConnectTo().Contains(typeof(KernelService))) { throw new ServiceCannotDependOnGenericKernelServiceException(); } var selectedService = GetInstalledDependencies(dependencyHolder.ServicesToConnectTo()); foreach (var map in selectedService) { if (!m_Connections.ContainsKey(service)) { m_Connections.Add(service, new List <ConnectionMap>()); } var connection = m_Connections[service]; connection.Add(new ConnectionMap(map.Key, map.Value)); dependencyHolder.ConnectTo(map.Value); } } var selectedServices = GetDependentServices(service); foreach (var map in selectedServices) { if (!m_Connections.ContainsKey(map.Value)) { m_Connections.Add(map.Value, new List <ConnectionMap>()); } var connection = m_Connections[map.Value]; connection.Add(new ConnectionMap(map.Key, service)); var dependent = map.Value as IHaveServiceDependencies; Debug.Assert(dependent != null, "The service should be able to handle dependencies."); dependent.ConnectTo(service); } m_Services.Add(service.GetType(), service); }
/// <summary> /// Installs the specified service. /// </summary> /// <param name="service">The service which should be installed.</param> /// <remarks> /// <para> /// Only services that are 'installed' can be used by the service manager. /// Services that have not been installed are simply unknown to the service /// manager. /// </para> /// <para> /// Note that only one instance for each <c>Type</c> can be provided to /// the service manager. /// </para> /// </remarks> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="service"/> is <see langword="null" />. /// </exception> public void Install(KernelService service) { { Enforce.Argument(() => service); } if ((m_State != StartupState.NotStarted) && (m_State != StartupState.Started)) { throw new KernelNotInInstallReadyStateException(); } if (m_Services.ContainsKey(service.GetType())) { throw new ServiceTypeAlreadyInstalledException(); } var dependencyHolder = service as IHaveServiceDependencies; if (dependencyHolder != null) { if (dependencyHolder.ServicesToConnectTo().Contains(dependencyHolder.GetType())) { throw new ServiceCannotDependOnItselfException(); } if (dependencyHolder.ServicesToConnectTo().Contains(typeof(KernelService))) { throw new ServiceCannotDependOnGenericKernelServiceException(); } var selectedService = GetInstalledDependencies(dependencyHolder.ServicesToConnectTo()); foreach (var map in selectedService) { if (!m_Connections.ContainsKey(service)) { m_Connections.Add(service, new List<ConnectionMap>()); } var connection = m_Connections[service]; connection.Add(new ConnectionMap(map.Key, map.Value)); dependencyHolder.ConnectTo(map.Value); } } var selectedServices = GetDependentServices(service); foreach (var map in selectedServices) { if (!m_Connections.ContainsKey(map.Value)) { m_Connections.Add(map.Value, new List<ConnectionMap>()); } var connection = m_Connections[map.Value]; connection.Add(new ConnectionMap(map.Key, service)); var dependent = map.Value as IHaveServiceDependencies; Debug.Assert(dependent != null, "The service should be able to handle dependencies."); dependent.ConnectTo(service); } m_Services.Add(service.GetType(), service); }