Home >

Castle Custom Component Activators

3. February 2009

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.

  1. Singleton(Default)
    Only one instance is created and subsequent calls to Resolve method will get the same instance.
  2. Transient
    Every call gets different instance of the service.
  3. PerWebRequest
    A call creates the instance if it is not created for the current web request. Can only be used in ASP.NET environment
  4. Pooled
    Makes a pool of component instance.
  5. Thread
    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]
                            as Configuration;
        return configuration.BuildSessionFactory();
    }
}

I really liked this approach that Castle had. I’d like to know if there’s a better of way achieving this specific scenario.

kick it on DotNetKicks.com

, , ,

Comments

2/3/2009 1:44:35 AM #
Trackback from DotNetKicks.com

Castle Custom Component Activators
10/27/2010 8:42:24 PM #
Great job on this post man! I haven't read a very informative article like this. Amazing.!
11/20/2010 3:53:09 AM #
Hello People! Your blog is awesome! I would like to visit it everyday. It's very informative and amazing
1/10/2011 4:05:18 AM #
good job with your explanations. your blog is one of my fave ever!
1/11/2011 12:47:47 PM #
Really like this website, this really helps and very useful.
2/24/2011 9:36:34 AM #
The code is quite helpful. Thanks for sharing.
3/21/2011 10:58:59 PM #
Got great informations on your blog.
3/22/2011 2:46:42 AM #
Hi. I read a few of your other posts and wanted to know if you would be interested in exchanging blogroll links?
3/22/2011 6:25:50 PM #
This article seems to be well crafted. I wish more people would ake some time and write quality articles that are much needed like this one. Thanks
3/22/2011 7:11:03 PM #
This article seems to be well crafted. I wish more people would ake some time and write quality articles that are much needed like this one. Thanks
3/22/2011 7:11:37 PM #
This article seems to be well crafted. I wish more people would ake some time and write quality articles that are much needed like this one. Thanks
3/22/2011 7:17:20 PM #
I just want to say that your blog is very nice.  Please keep writing informative articles like this.
3/22/2011 10:20:25 PM #
Please neglect almost all the terrible as well as uselss responses. Cheers to the publisher for great data!
3/23/2011 5:12:11 AM #
I'll gear this review to 2 types of people: current Zune owners who are  considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)
3/23/2011 11:15:01 AM #
This is getting a bit more subjective, but I much prefer the Zune Marketplace. The interface is colorful, has more flair, and some cool features like 'Mixview' that let you quickly see related albums, songs, or other users related to what you're listening to. Clicking on one of those will center on that item, and another set of "neighbors" will come into view, allowing you to navigate around exploring by similar artists, songs, or users. Speaking of users, the Zune "Social" is also great fun, letting you find others with shared tastes and becoming friends with them. You then can listen to a playlist created based on an amalgamation of what all your friends are listening to, which is also enjoyable. Those concerned with privacy will be relieved to know you can prevent the public from seeing your personal listening habits if you so choose.
Comments are closed