# HG changeset patch # User cin # Date 2014-08-27 22:28:00 # Node ID 9dd6a896a385fe812d1850554fd87063b6b20c62 # Parent b4c2454d208efc0bbec92a3ea7510d8b192491e8 ServiceLocator: small refactoring, GetService method is made virtual diff --git a/Implab/ServiceLocator.cs b/Implab/ServiceLocator.cs --- a/Implab/ServiceLocator.cs +++ b/Implab/ServiceLocator.cs @@ -41,13 +41,14 @@ namespace Implab { public bool TryGetService(out T service) { AssertNotDisposed(); - try { - service = GetService(); - return true; - } catch(KeyNotFoundException) { - service = default(T); - return false; - } + var result = GetService(typeof(T), false); + if (result == null) { + service = default(T); + return false; + } else { + service = (T)result; + return true; + } } /// @@ -56,7 +57,11 @@ namespace Implab { /// Тип запрашиваемого сервиса /// Объект, реализующий сервис /// Сервис не зарегистрирован - public object GetService(Type serviceType) { + public object GetService(Type serviceType) { + return GetService (serviceType, true); + } + + public virtual object GetService(Type serviceType, bool throwOnError) { if (serviceType == null) throw new ArgumentNullException("serviceType"); AssertNotDisposed(); @@ -119,7 +124,9 @@ namespace Implab { return se.service; } - throw new Exception("Unable to create a service instance"); + if (throwOnError) + throw new Exception("Unable to create a service instance"); + return null; } ///