diff --git a/ICD.Common.Utils/Services/ServiceProvider.cs b/ICD.Common.Utils/Services/ServiceProvider.cs index 4a8326e..734ace4 100644 --- a/ICD.Common.Utils/Services/ServiceProvider.cs +++ b/ICD.Common.Utils/Services/ServiceProvider.cs @@ -132,6 +132,21 @@ namespace ICD.Common.Services AddService(typeof(TService), service); } + /// + /// Registers a service instance to the type given. + /// + /// + /// + [PublicAPI] + public static bool TryAddService(TService service) + { + // ReSharper disable once CompareNonConstrainedGenericWithNull + if (service == null) + throw new ArgumentNullException("service"); + + return TryAddService(typeof(TService), service); + } + /// /// Registers a service instance to the type given. /// @@ -149,6 +164,23 @@ namespace ICD.Common.Services Instance.AddServiceInstance(tService, service); } + /// + /// Registers a service instance to the type given. + /// + /// + /// + [PublicAPI] + public static bool TryAddService(Type tService, object service) + { + if (tService == null) + throw new ArgumentNullException("tService"); + + if (service == null) + throw new ArgumentNullException("service"); + + return Instance.TryAddServiceInstance(tService, service); + } + /// /// Attempts to remove the given service from the given type. /// @@ -259,6 +291,37 @@ namespace ICD.Common.Services } } + /// + /// Adds the given service under the given type. + /// + /// + /// + private bool TryAddServiceInstance(Type tService, object service) + { + if (tService == null) + throw new ArgumentNullException("tService"); + + if (service == null) + throw new ArgumentNullException("service"); + + try + { + m_ServicesSection.Enter(); + + if (m_Services.ContainsKey(tService)) + { + return false; + } + + m_Services.Add(tService, service); + } + finally + { + m_ServicesSection.Leave(); + } + return true; + } + /// /// Removes the given service from the given type. ///