UPDATE: This facility made its way into Castle Microkernel, with name OnCreateFacility. I also made it possible to specify more than one actions.
In one of Joshua Flanagan's recent post he mentioned about how they handle application configuration and I have to say that I liked their way. I also liked how SM can post-modify an object created, and looked for a way to do it in Castle. As many other stuff, I was able to achieve the same effect with a custom Facility.
If I go further in the details, I had to catch ComponentCreated event of Kernel.
public class EnrichWithFacility:AbstractFacility
public const string ExtendWithPropertyKey = "extendwith";
protected override void Init()
Kernel.ComponentCreated += new Kernel_ComponentCreated;
void Kernel_ComponentCreated(ComponentModel model, object instance)
var action = model.ExtendedProperties[ExtendWithPropertyKey] as ExtendComponentDelegate;
Whenever a component is created, I will catch it and ask if there is any EnrichWith registered for the ComponentModel, and if there is any, invoke the action.
I also added a fluent registration extensions (Castle style!) in order to make it easy to register enrichments.
.EnrichWith((kernel, instance) => ((IService) instance).I++));
The code for the facility, fluent registration interface,and the tests can be found on our never-ending blog engine, BlogSharp codebase.
If you like the post, please kick and/or shout it.
07-14-2009 4:30 PM