changeset 68:9dd6a896a385

ServiceLocator: small refactoring, GetService method is made virtual
author cin
date Thu, 28 Aug 2014 02:28:00 +0400
parents b4c2454d208e
children 48763f3b5db8
files Implab/ServiceLocator.cs
diffstat 1 files changed, 16 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- 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<T>(out T service) {
             AssertNotDisposed();
 
-            try {
-                service = GetService<T>();
-                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;
+			}
         }
 
         /// <summary>
@@ -56,7 +57,11 @@
         /// <param name="serviceType">Тип запрашиваемого сервиса</param>
         /// <returns>Объект, реализующий сервис</returns>
         /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception>
-        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;
         }
 
         /// <summary>