# HG changeset patch # User cin # Date 1409178480 -14400 # Node ID 9dd6a896a385fe812d1850554fd87063b6b20c62 # Parent b4c2454d208efc0bbec92a3ea7510d8b192491e8 ServiceLocator: small refactoring, GetService method is made virtual diff -r b4c2454d208e -r 9dd6a896a385 Implab/ServiceLocator.cs --- a/Implab/ServiceLocator.cs Fri Aug 22 17:47:20 2014 +0400 +++ b/Implab/ServiceLocator.cs Thu Aug 28 02:28:00 2014 +0400 @@ -41,13 +41,14 @@ 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 @@ /// Тип запрашиваемого сервиса /// Объект, реализующий сервис /// Сервис не зарегистрирован - 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 @@ 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; } ///