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