using System; using System.Collections.Generic; using System.Linq; using GreenshotPlugin.Interfaces; namespace GreenshotPlugin.Core { /// /// A really cheap and simple DI system /// public class SimpleServiceProvider : IServiceLocator { private readonly Dictionary> _services = new Dictionary>(); public static IServiceLocator Current { get; } = new SimpleServiceProvider(); public IEnumerable GetAllInstances() { var typeOfService = typeof(TService); if (!_services.TryGetValue(typeOfService, out var results)) { yield break; } foreach (TService result in results) { yield return result; } } public TService GetInstance() { return GetAllInstances().SingleOrDefault(); } public void AddService(IEnumerable services) { var serviceType = typeof(TService); if (!_services.TryGetValue(serviceType, out var currentServices)) { currentServices = new List(); _services.Add(serviceType, currentServices); } foreach (var service in services) { currentServices.Add(service); } } public void AddService(params TService[] services) { AddService(services.AsEnumerable()); } } }