annotate Implab/ServiceLocator.cs @ 120:f1b897999260 v2

improved asyncpool usability working on batch operations on asyncqueue
author cin
date Mon, 12 Jan 2015 05:19:52 +0300
parents 8beee0d11de6
children
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
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
4 namespace Implab {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
5 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
6 /// Коллекция сервисов, позволяет регистрировать и получать сервисы.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
7 /// </summary>
117
8beee0d11de6 pool refactoring
cin
parents: 88
diff changeset
8 public class ServiceLocator: Disposable, IServiceLocator, IServiceProvider {
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
9 // запись о сервисе
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
10 struct ServiceEntry : IDisposable {
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
11 public object service; // сервис
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
12 public bool shared; // признак того, что сервис НЕ нужно освобождать
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
13 public Func<object> activator; // активатор сервиса при первом обращении
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
14 public Action<object> cleanup; // функция для очистки сервиса
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
15 public List<Type> associated; // ссылки на текущую запись
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
16 public Type origin; // ссылка на оригинальную запись о сервисе
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
17
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
18 #region IDisposable implementation
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
19
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
20 public void Dispose() {
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
21 if (shared)
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
22 return;
88
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
23 if (cleanup != null) {
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
24 if (service != null)
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
25 cleanup(service);
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
26 } else
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
27 Safe.Dispose(service);
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
28 }
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
29
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
30 #endregion
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
31 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
32
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
33 // словарь существующих сервисов
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
34 readonly Dictionary<Type, ServiceEntry> m_services = new Dictionary<Type,ServiceEntry>();
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
35
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
36 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
37 /// Получает объект предоставляющий сервис <typeparamref name="T"/>.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
38 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
39 /// <typeparam name="T">Тип запрашиваемого сервиса</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
40 /// <returns>Объект, реализующий сервис</returns>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
41 /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
42 public T GetService<T>() {
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
43 object result;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
44 if (TryGetService(typeof(T), out result))
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
45 return (T)result;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
46 throw new ApplicationException (String.Format ("{0} doesn't provide {1} service", this, typeof(T)));
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
47 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
48
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
49
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
50 /// <summary>
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 /// <typeparam name="T">Тип требуемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
55 /// <param name="service">Объект реализующий сервис, или <c>default(T)</c> если такового нет.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
56 /// <returns><c>true</c> - сервис найден, <c>false</c> - сервис не зарегистрирован.</returns>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
57 public bool TryGetService<T>(out T service) {
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
58 object result;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
59 if (TryGetService(typeof(T), out result)) {
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
60 service = (T)result;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
61 return true;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
62 }
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
63 service = default(T);
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
64 return false;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
65 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
66
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
67 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
68 /// Получает объект предоставляющий сервис <paramref name="serviceType"/>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
69 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
70 /// <param name="serviceType">Тип запрашиваемого сервиса</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
71 /// <returns>Объект, реализующий сервис</returns>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
72 /// <exception cref="KeyNotFoundException">Сервис не зарегистрирован</exception>
68
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
73 public object GetService(Type serviceType) {
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
74 object result;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
75 if (TryGetService(serviceType, out result))
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
76 return result;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
77 throw new ApplicationException (String.Format ("{0} doesn't provide {1} service", this, serviceType));
68
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
78 }
9dd6a896a385 ServiceLocator: small refactoring, GetService method is made virtual
cin
parents: 40
diff changeset
79
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
80 /// <summary>
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
81 /// Пытается получить требуемый сервис или совместимый с ним.
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
82 /// </summary>
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
83 /// <returns><c>true</c>, если сервис был найден, <c>false</c> в противном случае..</returns>
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
84 /// <param name="serviceType">Тип запрашиваемого сервиса.</param>
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
85 /// <param name="service">Искомый сервис.</param>
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
86 public virtual bool TryGetService(Type serviceType, out object service) {
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
87 Safe.ArgumentNotNull(serviceType, "serviceType");
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
88 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
89
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
90 ServiceEntry se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
91 if (!m_services.TryGetValue(serviceType, out se)) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
92 // ищем ближайщий объект, реализующий нужный сервис
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
93 Type pt = null;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
94 foreach (var t in m_services.Keys)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
95 if (serviceType.IsAssignableFrom(t) && (pt == null || t.IsAssignableFrom(pt)))
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
96 pt = t;
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
97
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
98 if (pt == null) {
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
99 // нет нужного сервиса
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
100 service = null;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
101 return false;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
102 }
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
103
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
104 var pe = m_services[pt];
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(pe.origin != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
108 pt = pe.origin;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
109 pe = m_services[pt];
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 if (pe.associated == null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
114 pe.associated = new List<Type>();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
115
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
116 pe.associated.Add(serviceType);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
117
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
118 // обновляем родительскую запись
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
119 m_services[pt] = pe;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
120
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
121 // создаем запись со ссылкой
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
122 se = new ServiceEntry {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
123 service = pe.service,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
124 origin = pt,
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
125 shared = true // предотвращаем множественные попытки освобождения
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
126 };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
127
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
128 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
129 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
130
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
131 // запись содержит в себе информацию о сервисе
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
132 if (se.service != null) {
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
133 service = se.service;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
134 return true;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
135 }
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
136
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
137 // текущая запись является ссылкой
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
138 if (se.origin != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
139 se.service = GetService(se.origin);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
140 m_services[serviceType] = se;
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
141 service = se.service;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
142 return true;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
143 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
144
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
145 // текущая запись не является ссылкой и не имеет информации о сервисе
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
146 // она должна сожержать информацию об активации
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
147 if (se.activator != null) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
148 se.service = se.activator();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
149
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
150 m_services[serviceType] = se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
151
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
152 service = se.service;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
153 return true;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
154 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
155
69
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
156 service = null;
48763f3b5db8 service locator refactoring
cin
parents: 68
diff changeset
157 return false;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
158 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
159
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
160 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
161 /// Регистрирует фабрику для активации сервиса по первому требованию.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
162 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
163 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
164 /// <param name="activator">Фабрика для создания/получения объекта, предоставляющего сервис.</param>
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
165 /// <param name = "cleanup">Метод для освобождения экземпляра сервиса, будет вызыван при освобождении сервис-локатора.</param>
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
166 /// <remarks>При освобождении сервис-локатора, сервисы полученные в результате активации также будут освобождены.</remarks>
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
167 public void Register<T>(Func<T> activator, Action<T> cleanup) {
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
168 Safe.ArgumentNotNull(activator, "activator");
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
169
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
170 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
171
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
172 Unregister(typeof(T));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
173
88
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
174 var serviceEntry = new ServiceEntry();
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
175 serviceEntry.activator = () => activator();
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
176 if (cleanup != null)
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
177 serviceEntry.cleanup = instance => cleanup((T)instance);
c4877ae77cee ServiceLocator: fixed services cleanup
cin
parents: 87
diff changeset
178 m_services[typeof(T)] = serviceEntry;
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
179 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
180
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
181 public void Register<T>(Func<T> activator) {
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
182 Register(activator, null);
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
183 }
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
184
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
185 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
186 /// Регистрирует объект, предоставляющий сервис.
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
187 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
188 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
189 /// <param name="service">Объект, предоставляющий сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
190 /// <exception cref="InvalidOperationException">Указанный сервис уже зарегистрирован.</exception>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
191 /// <remarks>Сервис-локатором не управляет временем жизни объекта для зарегистрированного сервиса.</remarks>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
192 public void Register<T>(T service) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
193 Register(service, true);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
194 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
195
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
196 /// <summary>
87
79badb3ed195 minor fixes in the service locator class
cin
parents: 86
diff changeset
197 /// Регистрирует объект, предоставляющий сервис. Повторная регистрация отменяет уже существующую.
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
198 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
199 /// <typeparam name="T">Тип регистрируемого сервиса.</typeparam>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
200 /// <param name="service">Объект, предоставляющий сервис.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
201 /// <param name="shared">Признак того, что объект является разделяемым и сервис-локатор не должен его освобождать.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
202 public void Register<T>(T service, bool shared) {
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
203 Safe.ArgumentNotNull(service, "service");
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
204
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
205 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
206
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
207 Unregister(typeof(T));
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
208
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
209 m_services[typeof(T)] = new ServiceEntry { service = service, shared = shared };
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
210 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
211
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
212 public void Unregister(Type serviceType) {
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
213 Safe.ArgumentNotNull(serviceType, "serviceType");
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
214
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
215 AssertNotDisposed();
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
216
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
217 ServiceEntry se;
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
218 if (m_services.TryGetValue(serviceType, out se)) {
87
79badb3ed195 minor fixes in the service locator class
cin
parents: 86
diff changeset
219 if (se.origin != null) {
79badb3ed195 minor fixes in the service locator class
cin
parents: 86
diff changeset
220 var pe = m_services[se.origin];
79badb3ed195 minor fixes in the service locator class
cin
parents: 86
diff changeset
221 pe.associated.Remove(serviceType);
79badb3ed195 minor fixes in the service locator class
cin
parents: 86
diff changeset
222 }
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
223 // освобождаем ресурсы
87
79badb3ed195 minor fixes in the service locator class
cin
parents: 86
diff changeset
224 se.Dispose();
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
225 m_services.Remove(serviceType);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
226
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
227 // убираем связанные записи
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
228 if (se.associated != null)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
229 foreach (var item in se.associated)
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
230 m_services.Remove(item);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
231 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
232 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
233
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
234 /// <summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
235 /// Освобождает зарегистрированные сервисы (которые требуется освобоить).
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
236 /// </summary>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
237 /// <param name="disposing">Призанак того, что нужно освободить ресурсы.</param>
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
238 protected override void Dispose(bool disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
239 if (disposing) {
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
240
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
241 foreach (var entry in m_services.Values)
86
b33832ab0262 ServiceLocator: added a cleanup callback to the service registration method
cin
parents: 69
diff changeset
242 entry.Dispose();
40
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
243
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
244 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
245 base.Dispose(disposing);
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
246 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
247 }
fe33f4e02ad5 improved tracing
cin
parents:
diff changeset
248 }