104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
namespace ln.application.service
|
|
{
|
|
public class ServiceContainer : IEnumerable<ServiceDefinition>
|
|
{
|
|
public Application Application { get; }
|
|
|
|
HashSet<ServiceDefinition> serviceDefinitions = new HashSet<ServiceDefinition>();
|
|
|
|
public ServiceContainer(Application application)
|
|
{
|
|
Application = application;
|
|
}
|
|
|
|
public void Add(ServiceDefinition serviceDefinition)
|
|
{
|
|
serviceDefinitions.Add(serviceDefinition);
|
|
}
|
|
public void Remove(ServiceDefinition serviceDefinition)
|
|
{
|
|
serviceDefinitions.Remove(serviceDefinition);
|
|
}
|
|
|
|
public ServiceDefinition this[string serviceClassName]
|
|
{
|
|
get
|
|
{
|
|
foreach (ServiceDefinition serviceDefinition in serviceDefinitions)
|
|
{
|
|
if (serviceDefinition.ServiceClassName.Equals(serviceClassName))
|
|
return serviceDefinition;
|
|
}
|
|
throw new KeyNotFoundException();
|
|
}
|
|
}
|
|
public ServiceDefinition this[string assemblyName,string serviceClassName]
|
|
{
|
|
get
|
|
{
|
|
foreach (ServiceDefinition serviceDefinition in serviceDefinitions)
|
|
{
|
|
if (serviceDefinition.ServiceClassName.Equals(serviceClassName) && Object.Equals(serviceDefinition.AssemblyName,assemblyName))
|
|
return serviceDefinition;
|
|
}
|
|
throw new KeyNotFoundException();
|
|
}
|
|
}
|
|
public ServiceDefinition this[Type serviceType]
|
|
{
|
|
get
|
|
{
|
|
return this[serviceType.Assembly.FullName,serviceType.FullName];
|
|
}
|
|
}
|
|
|
|
public void Start(ServiceDefinition serviceDefinition)
|
|
{
|
|
if (!serviceDefinitions.Contains(serviceDefinition))
|
|
throw new NotSupportedException("ServiceDefinition not known to ServiceContainer");
|
|
|
|
ServiceDefinition sd = this[serviceDefinition.AssemblyName, serviceDefinition.ServiceClassName];
|
|
sd.Start(this.Application);
|
|
}
|
|
public void Stop(ServiceDefinition serviceDefinition)
|
|
{
|
|
if (!serviceDefinitions.Contains(serviceDefinition))
|
|
throw new NotSupportedException("ServiceDefinition not known to ServiceContainer");
|
|
|
|
ServiceDefinition sd = this[serviceDefinition.AssemblyName, serviceDefinition.ServiceClassName];
|
|
sd.Stop();
|
|
}
|
|
public void Load(ServiceDefinition serviceDefinition)
|
|
{
|
|
if (!serviceDefinitions.Contains(serviceDefinition))
|
|
throw new NotSupportedException("ServiceDefinition not known to ServiceContainer");
|
|
|
|
ServiceDefinition sd = this[serviceDefinition.AssemblyName, serviceDefinition.ServiceClassName];
|
|
sd.Load();
|
|
}
|
|
public void Unload(ServiceDefinition serviceDefinition)
|
|
{
|
|
if (!serviceDefinitions.Contains(serviceDefinition))
|
|
throw new NotSupportedException("ServiceDefinition not known to ServiceContainer");
|
|
|
|
ServiceDefinition sd = this[serviceDefinition.AssemblyName, serviceDefinition.ServiceClassName];
|
|
sd.Unload();
|
|
}
|
|
|
|
|
|
|
|
public IEnumerator<ServiceDefinition> GetEnumerator()
|
|
{
|
|
return ((IEnumerable<ServiceDefinition>)serviceDefinitions).GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return ((IEnumerable<ServiceDefinition>)serviceDefinitions).GetEnumerator();
|
|
}
|
|
}
|
|
}
|