changeset 88:c4877ae77cee v2

ServiceLocator: fixed services cleanup
author cin
date Tue, 07 Oct 2014 10:58:38 +0400
parents 79badb3ed195
children ce0171cacec4
files Implab/ServiceLocator.cs
diffstat 1 files changed, 10 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Implab/ServiceLocator.cs	Mon Oct 06 18:11:23 2014 +0400
+++ b/Implab/ServiceLocator.cs	Tue Oct 07 10:58:38 2014 +0400
@@ -20,13 +20,11 @@
             public void Dispose() {
                 if (shared)
                     return;
-                if (cleanup != null)
-                    cleanup(service);
-                else {
-                    var d = service as IDisposable;
-                    if (d != null)
-                        d.Dispose();
-                }
+                if (cleanup != null) {
+                    if (service != null)
+                        cleanup(service);
+                } else
+                    Safe.Dispose(service);
             }
 
             #endregion
@@ -173,10 +171,11 @@
 
             Unregister(typeof(T));
             
-            m_services[typeof(T)] = new ServiceEntry {
-                activator = () => activator(),
-                cleanup = instance => cleanup((T)instance)
-            };
+            var serviceEntry = new ServiceEntry();
+            serviceEntry.activator = () => activator();
+            if (cleanup != null)
+                serviceEntry.cleanup = instance => cleanup((T)instance);
+            m_services[typeof(T)] = serviceEntry;
         }
 
         public void Register<T>(Func<T> activator) {