Explicit cleanup is useful in the following scenarios:
Zyan Framework supports two kinds of explicit cleanup:
Zyan automaticly disposes all instances which are created by Zyan (so Zyan is the owner). Externally-owned components are not disposed.
Registering component instance will make your component externally-owned. The only exception is registering component instance together
with the cleanup handler. In that case Zyan will assume that component ownership is transferred to Zyan (see the summary table below).
Implement IDisposable interface to indicate that your component needs early cleanup. Using IDisposable pattern can simplify your component code:
instead of allocating and freeing limited resource on every method call you may allocate it in the constructor and deallocate in Dispose method:
// Non-IDisposable component: limited resource is allocated on every method call class Service : IService { public void Method1() { using (var resource = new LimitedResource()) { resource.Use(); // do something with the allocated resource } } public void Method2() { using (var resource = new LimitedResource()) { resource.Use(); } } } // IDisposable component: limited resource is allocated in component constructor class Service : IService, IDisposable { IDisposable resource = new LimitedResource(); void IDisposable.Dispose() { resource.Dispose(); } public void Method1() { resource.Use(); // methods look simpler } public void Method2() { resource.Use(); } }
Provide cleanup delegate with your component registration, so Zyan can release resources if IDisposable pattern is not applicable.
For example, if your component instance lifetime is managed by some IoC container such as
MEF.
host.RegisterComponent<IService>( () => container.Resolve<IService>(), // factory method service => container.Release(service) // cleanup delegate );
Single-call components are created and released on each remote method call. Singleton components lifetime is bound to their parent ComponentCatalog.
The following table summarizes Zyan behaviour in resource handling:
Registration | Cleanup |
---|---|
catalog.RegisterComponent<IService>(() => new Service(), ActivationType.SingleCall); | Disposed |
catalog.RegisterComponent<IService, Service>(ActivationType.SingleCall); | Disposed |
catalog.RegisterComponent<IService>(() => new Service(), ActivationType.Singleton); | Disposed |
catalog.RegisterComponent<IService, Service>(ActivationType.Singleton); | Disposed |
catalog.RegisterComponent<IService, Service>(serviceInstance); | Not disposed |
catalog.RegisterComponent<IService>(() =>
new Service(), ActivationType.SingleCall, v => ((Service)v).Release()); |
Cleanup delegate is called |
catalog.RegisterComponent<IService, Service>( ActivationType.SingleCall, v => ((Service)v).Release()); |
Cleanup delegate is called |
catalog.RegisterComponent<IService>(() =>
new Service(), ActivationType.Singleton, v => ((Service)v).Release()); |
Cleanup delegate is called |
catalog.RegisterComponent<IService, Service>( ActivationType.Singleton, v => ((Service)v).Release()); |
Cleanup delegate is called |
catalog.RegisterComponent<IService, Service>( serviceInstance, v => ((Service)v).Release()); |
Cleanup delegate is called |