annotate Implab/ServiceLocator.cs @ 68:9dd6a896a385

ServiceLocator: small refactoring, GetService method is made virtual
author cin
date Thu, 28 Aug 2014 02:28:00 +0400
parents fe33f4e02ad5
children 48763f3b5db8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
1 using System;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
2 using System.Collections.Generic;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
3 using System.Linq;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4 using System.Web;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6 namespace Implab {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
7 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
8 /// Коллекция сервисов, позволяет регистрировать и получать сервисы.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
9 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
10 public class ServiceLocator: Component, IServiceLocator, IServiceProvider {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
11 // запись об сервисе
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12 struct ServiceEntry {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13 public object service; // сервис
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
14 public bool shared; // признак того, что сервис НЕ нужно освобождать
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15 public Func<object> activator; // активатор сервиса при первом обращении
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
16 public List<Type> associated; // ссылки на текущую запись
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
17 public Type origin; // ссылка на оригинальную запись о сервисе
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
18 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
19
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
20 // словарь существующих сервисов
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
21 Dictionary<Type, ServiceEntry> m_services = new Dictionary<Type,ServiceEntry>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
22
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
23 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
24 /// Получает объект предоставляющий сервис <typeparamref name="T"/>.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
25 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
26 /// <typeparam name="T">Тип запрашиваемого сервиса</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
27 /// <returns>Объект, реализующий сервис</returns>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
28 /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
29 public T GetService<T>() {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
30 return (T)GetService(typeof(T));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
31 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
32
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
33
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
34 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
35 /// Пытается получить указанный сервис, в случае, если компонента не предоставляет требуемый сервис
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36 /// не возникает исключений.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
38 /// <typeparam name="T">Тип требуемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
39 /// <param name="service">Объект реализующий сервис, или <c>default(T)</c> если такового нет.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40 /// <returns><c>true</c> - сервис найден, <c>false</c> - сервис не зарегистрирован.</returns>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41 public bool TryGetService<T>(out T service) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
42 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
43
68
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
44 var result = GetService(typeof(T), false);
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
45 if (result == null) {
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
46 service = default(T);
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
47 return false;
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
48 } else {
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
49 service = (T)result;
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
50 return true;
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
51 }
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
52 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
53
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
54 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
55 /// Получает объект предоставляющий сервис <paramref name="serviceType"/>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
56 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
57 /// <param name="serviceType">Тип запрашиваемого сервиса</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
58 /// <returns>Объект, реализующий сервис</returns>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
59 /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception>
68
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
60 public object GetService(Type serviceType) {
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
61 return GetService (serviceType, true);
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
62 }
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
63
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
64 public virtual object GetService(Type serviceType, bool throwOnError) {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
65 if (serviceType == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
66 throw new ArgumentNullException("serviceType");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
67 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
68
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
69 ServiceEntry se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
70 if (!m_services.TryGetValue(serviceType, out se)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
71 // ищем ближайщий объект, реализующий нужный сервис
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
72 Type pt = null;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
73 foreach (var t in m_services.Keys)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
74 if (serviceType.IsAssignableFrom(t) && (pt == null || t.IsAssignableFrom(pt)))
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
75 pt = t;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
76 if (pt == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
77 throw new ApplicationException(String.Format("{0} doesn't provide {1} service",this,serviceType));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
78
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
79 var pe = m_services[pt];
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
80
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
81 // найденная запись может ссылаться на оригинальную запись с сервисом
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
82 if(pe.origin != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
83 pt = pe.origin;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
84 pe = m_services[pt];
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
85 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
86
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
87 // добавляем список с обратными ссылками
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
88 if (pe.associated == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
89 pe.associated = new List<Type>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
90
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
91 pe.associated.Add(serviceType);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
92
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
93 // обновляем родительскую запись
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
94 m_services[pt] = pe;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
95
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
96 // создаем запись со ссылкой
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
97 se = new ServiceEntry {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
98 service = pe.service,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
99 origin = pt,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
100 shared = true // предотвращаем множественные попытки освобождения
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
101 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
102
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
103 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
104 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
105
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
106 // запись содержит в себе информацию о сервисе
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
107 if (se.service != null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
108 return se.service;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
109
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
110 // текущая запись является ссылкой
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
111 if (se.origin != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
112 se.service = GetService(se.origin);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
113 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
114 return se.service;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
115 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
116
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
117 // текущая запись не является ссылкой и не имеет информации о сервисе
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
118 // она должна сожержать информацию об активации
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
119 if (se.activator != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
120 se.service = se.activator();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
121
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
122 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
123
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
124 return se.service;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
125 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
126
68
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
127 if (throwOnError)
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
128 throw new Exception("Unable to create a service instance");
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
129 return null;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
130 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
131
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
132 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
133 /// Регистрирует фабрику для активации сервиса по первому требованию.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
134 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
135 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
136 /// <param name="activator">Фабрика для создания/получения объекта, предоставляющего сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
137 /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
138 /// <remarks>При освобождении сервис-локатора, сервисы полученные в результате активации также будут освобождены.</remarks>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
139 public void Register<T>(Func<T> activator) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
140 if (activator == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
141 throw new ArgumentNullException("activator");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
142
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
143 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
144
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
145 Unregister(typeof(T));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
146
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
147 m_services[typeof(T)] = new ServiceEntry {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
148 activator = () => activator() as object
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
149 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
150 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
151
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
152 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
153 /// Регистрирует объект, предоставляющий сервис.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
154 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
155 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
156 /// <param name="service">Объект, предоставляющий сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
157 /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
158 /// <remarks>Сервис-локатором не управляет временем жизни объекта для зарегистрированного сервиса.</remarks>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
159 public void Register<T>(T service) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
160 Register(service, true);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
161 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
162
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
163 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
164 /// Регистрирует объект, предоставляющий сервис.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
165 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
166 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
167 /// <param name="service">Объект, предоставляющий сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
168 /// <param name="shared">Признак того, что объект является разделяемым и сервис-локатор не должен его освобождать.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
169 /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
170 public void Register<T>(T service, bool shared) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
171 if (service == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
172 throw new ArgumentNullException("service");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
173
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
174 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
175
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
176 Unregister(typeof(T));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
177
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
178 m_services[typeof(T)] = new ServiceEntry { service = service, shared = shared };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
179 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
180
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
181 public void Unregister(Type serviceType) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
182 if (serviceType == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
183 throw new ArgumentNullException("serviceType");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
184
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
185 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
186
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
187 ServiceEntry se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
188 if (m_services.TryGetValue(serviceType, out se)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
189 // освобождаем ресурсы
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
190 if (se.service != null && !se.shared)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
191 ((IDisposable)se.service).Dispose();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
192 m_services.Remove(serviceType);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
193
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
194 // убираем связанные записи
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
195 if (se.associated != null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
196 foreach (var item in se.associated)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
197 m_services.Remove(item);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
198 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
199 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
200
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
201 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
202 /// Освобождает зарегистрированные сервисы (которые требуется освобоить).
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
203 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
204 /// <param name="disposing">Призанак того, что нужно освободить ресурсы.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
205 protected override void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
206 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
207
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
208 foreach (var entry in m_services.Values)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
209 if (!entry.shared && entry.service is IDisposable)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
210 ((IDisposable)entry.service).Dispose();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
211
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
212 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
213 base.Dispose(disposing);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
214 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
215 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
216 }