I have already said here that I love Castle and how excited I am about its extensibility. This post will be another one that talks about Castle Microkernel extensibility.
When you try to resolve a component from microkernel, it goes through several steps. It firsts find the appropriate IHandler instance which manages component states and coordinates component creation/destruction that is associated with the service requested. Handler then calls ILifestyleManager in order to get the object requested. The ILifestyleManager manages the lifestyle of the service, and there are several built-in ILifestyleManagers in Castle.
Only one instance is created and subsequent calls to Resolve method will get the same instance.
Every call gets different instance of the service.
A call creates the instance if it is not created for the current web request. Can only be used in ASP.NET environment
Makes a pool of component instance.
A call will create the object if it hasn’t been created for that thread. Otherwise, previously created object will be returned.
The ILifestyleManager then calls ComponentActivator in order to create object when necessary.
It has been suggested in Castle Development group that NHibernateIntegrationFacility should lazily initialize ISessionFactory. It is really a good idea and it was something that I had in mind but forgot somehow. German offered that a proxy is registered to container and via this proxy this lazy load could be achieved. This was a good idea but I thought there should be a better way in Castle to handle this kind of situation. Then I remembered something called ComponentActivator and even though I didn’t use it previously, its name gave some idea.
Previously the session factory initialized when the NH facility is initialized,
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
Kernel.AddComponentInstance( id, typeof(ISessionFactory), sessionFactory );
Instead of this, I created custom ComponentModel that sets the CustomComponentActivator which will be used to create the object instance
var model = new ComponentModel(id, typeof(ISessionFactory), typeof(Empty));
model.LifestyleType = LifestyleType.Singleton;
model.ExtendedProperties[Constants.SessionFactoryConfiguration] = cfg;
model.CustomComponentActivator = typeof (SessionFactoryActivator);
Kernel.AddCustomComponent( model );
I will have access to that ComponentModel in the activator, and since I provide cfg(Nhibernate.Cfg.Configuration) via ExtendedProperties, I’ll be able to initiate the SessionFactory.
public class SessionFactoryActivator : DefaultComponentActivator
public SessionFactoryActivator(ComponentModel model, IKernel kernel,
ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction)
: base(model, kernel, onCreation, onDestruction)
public override object Create(CreationContext context)
var configuration = Model.ExtendedProperties[Constants.SessionFactoryConfiguration]
I really liked this approach that Castle had. I’d like to know if there’s a better of way achieving this specific scenario.