##// END OF EJS Templates
ServiceLocator: small refactoring, GetService method is made virtual
cin -
r68:9dd6a896a385 default
parent child
Show More
@@ -41,12 +41,13 namespace Implab {
41 41 public bool TryGetService<T>(out T service) {
42 42 AssertNotDisposed();
43 43
44 try {
45 service = GetService<T>();
46 return true;
47 } catch(KeyNotFoundException) {
44 var result = GetService(typeof(T), false);
45 if (result == null) {
48 46 service = default(T);
49 47 return false;
48 } else {
49 service = (T)result;
50 return true;
50 51 }
51 52 }
52 53
@@ -57,6 +58,10 namespace Implab {
57 58 /// <returns>Объект, реализующий сервис</returns>
58 59 /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception>
59 60 public object GetService(Type serviceType) {
61 return GetService (serviceType, true);
62 }
63
64 public virtual object GetService(Type serviceType, bool throwOnError) {
60 65 if (serviceType == null)
61 66 throw new ArgumentNullException("serviceType");
62 67 AssertNotDisposed();
@@ -119,7 +124,9 namespace Implab {
119 124 return se.service;
120 125 }
121 126
127 if (throwOnError)
122 128 throw new Exception("Unable to create a service instance");
129 return null;
123 130 }
124 131
125 132 /// <summary>
General Comments 0
You need to be logged in to leave comments. Login now