using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataPro.Core.ServiceManager
{
///
/// Service manager allows a component to publish an implementation of a singleton interface (service) that
/// other components can retrieve reference to without knowing who published it
///
public static class ServiceManager
{
///
/// Stores all published interfaces
///
private static readonly Dictionary Services = new Dictionary();
///
/// Publishes a service
///
/// interface type of service
/// implementation of service
public static void Publish(T item) where T : class
{
if (Services.ContainsKey(typeof(T)))
{
throw new ArgumentException(string.Format("{0}: has already been published", typeof(T).Name));
}
Services.Add(typeof(T), item);
SendServicePublishedEvent(typeof(T), true);
}
///
/// Publishes a list of services
///
/// class that will implement all the listed interfaces
/// list of interfaces
/// true to avoid errors on already published interfaces
public static void Publish(object item, IEnumerable interfaceList, bool skipPublishedInterfaces)
{
foreach (var t in interfaceList)
{
if (Exists(t))
{
if (!skipPublishedInterfaces)
{
// service already published and caller indicated we should error out
throw new ArgumentException(string.Format("{0}: has already been published", t.Name));
}
}
else
{
// service doesn't exist so publish it
Services.Add(t, item);
SendServicePublishedEvent(t, true);
}
}
}
///
/// Returns true if specifed interface has been published, false if not
///
/// type of interface to check
/// true if interface has been published; false if not published
public static bool Exists() where T : class
{
return Services.ContainsKey(typeof(T));
}
///
/// Returns true if specifed interface has been published, false if not
///
/// type of interface to check
/// true if interface has been published; false if not published
public static bool Exists(Type t)
{
return Services.ContainsKey(t);
}
///
/// Returns published service
///
/// interface type of service
/// published service or exception if not currently published
public static T Get() where T : class
{
if (!Services.ContainsKey(typeof(T)))
{
throw new ArgumentException(string.Format("{0}: has not been published", typeof(T).Name));
}
return Services[typeof(T)] as T;
}
///
/// Clears specified service
///
/// interface type of service to clear
public static void Clear() where T : class
{
if (Services.ContainsKey(typeof(T)))
{
SendServicePublishedEvent(typeof(T), false);
Services.Remove(typeof(T));
}
}
///
/// Clears list of published interfaces
///
/// list of interfaces to unpublish
public static void Clear(IEnumerable interfaceList)
{
foreach (var t in interfaceList)
{
if (!Services.ContainsKey(t)) continue;
SendServicePublishedEvent(t, false);
Services.Remove(t);
}
}
///
/// Sends a IServicePublishedEvent through the Event Manger to let subscribers know when
/// a service is published or unpublished
///
/// type of interface
/// true if being published, false if being unpublished
private static void SendServicePublishedEvent(Type type, bool published)
{
EventManager.EventManager.Publish(new ServicePublishedEvent
{
IsPublished = published,
ServiceType = type
});
}
}
}