Home

Castle Stronghold

Lifecycles

By Lifecycle we mean the ability to use interfaces so the component can have some methods invoked by the container in response to some phase being executed.

From the MicroKernel perspective there are two groups of lifecycles: Commission and Decommission. The former are executed upon component creation and the later upon component destruction. Out of the box, the MicroKernel supports:

What does this mean? If your component implements one of those interfaces, the MicroKernel will call them for you at the right times. For example:


[Transient]
public class MyComponent : IInitializable, IDisposable
{
    public void Initialize()
    {
    }

    public void Dispose()
    {
    }
}

Here is an example of how to use our component:


IKernel kernel = new DefaultKernel();
kernel.AddComponent("x", typeof(MyComponent));

MyComponent instance = kernel["x"] as MyComponent;

// The instance has been properly initialized.
// Supported commission methods have been run.

kernel.ReleaseComponent(instance);

// The instance has been properly disposed, and you 
// shouldn't use it anymore. Supported decommission
// methods have been run.
Google
Search WWW Search castleproject.org