This project has moved. For the latest updates, please go here.

Deterministic resource cleanup

Explicit cleanup is useful in the following scenarios:

  • Single-call component uses some limited resource and implements IDisposable — it should be cleaned up upon method invocation;
  • Singleton component does the same — it should be disposed of together with its owner, ZyanComponentHost;
  • Some component is registered with the use of factory and requires explicit deinitialization.


Zyan Framework supports two kinds of explicit cleanup:

  • Calling Dispose on IDisposable components;
  • Executing cleanup handler provided externally.


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).

Automatically calling Dispose on IDisposable components

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();
  }	
}

Using external method to handle clean up

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
);

Singletons vs Single-calls components

Single-call components are created and released on each remote method call. Singleton components lifetime is bound to their parent ComponentCatalog.

Summary table

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

Last edited Nov 1, 2011 at 10:41 PM by yallie, version 13

Comments

No comments yet.