annotate Implab/ServiceLocator.cs @ 64:a809805210d1

small refactoring
author cin
date Wed, 25 Jun 2014 04:44:53 +0400
parents fe33f4e02ad5
children 9dd6a896a385
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
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
44 try {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
45 service = GetService<T>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
46 return true;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
47 } catch(KeyNotFoundException) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
48 service = default(T);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
49 return false;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
50 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
51 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
52
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
53 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
54 /// Получает объект предоставляющий сервис <paramref name="serviceType"/>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
55 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
56 /// <param name="serviceType">Тип запрашиваемого сервиса</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
57 /// <returns>Объект, реализующий сервис</returns>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
58 /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
59 public object GetService(Type serviceType) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
60 if (serviceType == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
61 throw new ArgumentNullException("serviceType");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
62 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
63
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
64 ServiceEntry se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
65 if (!m_services.TryGetValue(serviceType, out se)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
66 // ищем ближайщий объект, реализующий нужный сервис
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
67 Type pt = null;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
68 foreach (var t in m_services.Keys)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
69 if (serviceType.IsAssignableFrom(t) && (pt == null || t.IsAssignableFrom(pt)))
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
70 pt = t;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
71 if (pt == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
72 throw new ApplicationException(String.Format("{0} doesn't provide {1} service",this,serviceType));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
73
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
74 var pe = m_services[pt];
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
75
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
76 // найденная запись может ссылаться на оригинальную запись с сервисом
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
77 if(pe.origin != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
78 pt = pe.origin;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
79 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 // добавляем список с обратными ссылками
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
83 if (pe.associated == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
84 pe.associated = new List<Type>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
85
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
86 pe.associated.Add(serviceType);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
87
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
88 // обновляем родительскую запись
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
89 m_services[pt] = pe;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
90
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
91 // создаем запись со ссылкой
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
92 se = new ServiceEntry {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
93 service = pe.service,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
94 origin = pt,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
95 shared = true // предотвращаем множественные попытки освобождения
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
96 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
97
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
98 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
99 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
100
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
101 // запись содержит в себе информацию о сервисе
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
102 if (se.service != null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
103 return se.service;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
104
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
105 // текущая запись является ссылкой
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
106 if (se.origin != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
107 se.service = GetService(se.origin);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
108 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
109 return se.service;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
110 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
111
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
112 // текущая запись не является ссылкой и не имеет информации о сервисе
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
113 // она должна сожержать информацию об активации
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
114 if (se.activator != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
115 se.service = se.activator();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
116
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
117 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
118
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
119 return se.service;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
120 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
121
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
122 throw new Exception("Unable to create a service instance");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
123 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
124
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
125 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
126 /// Регистрирует фабрику для активации сервиса по первому требованию.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
127 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
128 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
129 /// <param name="activator">Фабрика для создания/получения объекта, предоставляющего сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
130 /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
131 /// <remarks>При освобождении сервис-локатора, сервисы полученные в результате активации также будут освобождены.</remarks>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
132 public void Register<T>(Func<T> activator) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
133 if (activator == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
134 throw new ArgumentNullException("activator");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
135
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
136 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
137
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
138 Unregister(typeof(T));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
139
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
140 m_services[typeof(T)] = new ServiceEntry {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
141 activator = () => activator() as object
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
142 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
143 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
144
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
145 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
146 /// Регистрирует объект, предоставляющий сервис.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
147 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
148 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
149 /// <param name="service">Объект, предоставляющий сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
150 /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
151 /// <remarks>Сервис-локатором не управляет временем жизни объекта для зарегистрированного сервиса.</remarks>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
152 public void Register<T>(T service) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
153 Register(service, true);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
154 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
155
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
156 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
157 /// Регистрирует объект, предоставляющий сервис.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
158 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
159 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
160 /// <param name="service">Объект, предоставляющий сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
161 /// <param name="shared">Признак того, что объект является разделяемым и сервис-локатор не должен его освобождать.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
162 /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
163 public void Register<T>(T service, bool shared) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
164 if (service == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
165 throw new ArgumentNullException("service");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
166
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
167 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
168
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
169 Unregister(typeof(T));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
170
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
171 m_services[typeof(T)] = new ServiceEntry { service = service, shared = shared };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
172 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
173
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
174 public void Unregister(Type serviceType) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
175 if (serviceType == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
176 throw new ArgumentNullException("serviceType");
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
177
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
178 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
179
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
180 ServiceEntry se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
181 if (m_services.TryGetValue(serviceType, out se)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
182 // освобождаем ресурсы
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
183 if (se.service != null && !se.shared)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
184 ((IDisposable)se.service).Dispose();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
185 m_services.Remove(serviceType);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
186
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
187 // убираем связанные записи
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
188 if (se.associated != null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
189 foreach (var item in se.associated)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
190 m_services.Remove(item);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
191 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
192 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
193
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
194 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
195 /// Освобождает зарегистрированные сервисы (которые требуется освобоить).
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
196 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
197 /// <param name="disposing">Призанак того, что нужно освободить ресурсы.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
198 protected override void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
199 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
200
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
201 foreach (var entry in m_services.Values)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
202 if (!entry.shared && entry.service is IDisposable)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
203 ((IDisposable)entry.service).Dispose();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
204
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
205 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
206 base.Dispose(disposing);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
207 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
208 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
209 }