comparison Implab/ServiceLocator.cs @ 88:c4877ae77cee v2

ServiceLocator: fixed services cleanup
author cin
date Tue, 07 Oct 2014 10:58:38 +0400
parents 79badb3ed195
children 8beee0d11de6
comparison
equal deleted inserted replaced
87:79badb3ed195 88:c4877ae77cee
18 #region IDisposable implementation 18 #region IDisposable implementation
19 19
20 public void Dispose() { 20 public void Dispose() {
21 if (shared) 21 if (shared)
22 return; 22 return;
23 if (cleanup != null) 23 if (cleanup != null) {
24 cleanup(service); 24 if (service != null)
25 else { 25 cleanup(service);
26 var d = service as IDisposable; 26 } else
27 if (d != null) 27 Safe.Dispose(service);
28 d.Dispose();
29 }
30 } 28 }
31 29
32 #endregion 30 #endregion
33 } 31 }
34 32
171 169
172 AssertNotDisposed(); 170 AssertNotDisposed();
173 171
174 Unregister(typeof(T)); 172 Unregister(typeof(T));
175 173
176 m_services[typeof(T)] = new ServiceEntry { 174 var serviceEntry = new ServiceEntry();
177 activator = () => activator(), 175 serviceEntry.activator = () => activator();
178 cleanup = instance => cleanup((T)instance) 176 if (cleanup != null)
179 }; 177 serviceEntry.cleanup = instance => cleanup((T)instance);
178 m_services[typeof(T)] = serviceEntry;
180 } 179 }
181 180
182 public void Register<T>(Func<T> activator) { 181 public void Register<T>(Func<T> activator) {
183 Register(activator, null); 182 Register(activator, null);
184 } 183 }