<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tuna Toksoz : windsor</title><link>http://devlicio.us/blogs/tuna_toksoz/archive/tags/windsor/default.aspx</link><description>Tags: windsor</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Implementing EnrichWith(of StructureMap) with Castle</title><link>http://devlicio.us/blogs/tuna_toksoz/archive/2009/07/14/implementing-enrichwith-of-structuremap-with-castle.aspx</link><pubDate>Tue, 14 Jul 2009 13:30:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:49379</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/tuna_toksoz/rsscomments.aspx?PostID=49379</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/tuna_toksoz/commentapi.aspx?PostID=49379</wfw:comment><comments>http://devlicio.us/blogs/tuna_toksoz/archive/2009/07/14/implementing-enrichwith-of-structuremap-with-castle.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;UPDATE: This facility made its way into Castle Microkernel, with name OnCreateFacility. I also made it possible to specify more than one actions. &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In one of &lt;a href="http://www.lostechies.com/blogs/joshuaflanagan/archive/2009/07/12/how-we-handle-application-configuration.aspx"&gt;Joshua Flanagan&amp;#39;s recent post&lt;/a&gt; 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. &lt;/p&gt;
&lt;p&gt;If I go further in the details, I had to catch ComponentCreated event of Kernel.&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;public class EnrichWithFacility:AbstractFacility
{
	public const string ExtendWithPropertyKey = &amp;quot;extendwith&amp;quot;;
	protected override void Init()
	{
		Kernel.ComponentCreated += new Kernel_ComponentCreated;
	}
	void Kernel_ComponentCreated(ComponentModel model, object instance)
	{
		if(model.ExtendedProperties.Contains(ExtendWithPropertyKey))
		{
			var action = model.ExtendedProperties[ExtendWithPropertyKey] as ExtendComponentDelegate;
			action(this.Kernel, instance);
		}
	}
}&lt;/pre&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;I also added a fluent registration extensions (Castle style!) in order to make it easy to register enrichments.&lt;/p&gt;
&lt;pre name="code" class="c-sharp"&gt;container.Register(Component.For&amp;lt;IService&amp;gt;().ImplementedBy&amp;lt;MyService&amp;gt;()
			 .EnrichWith((kernel, instance) =&amp;gt; ((IService) instance).I++));&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code for &lt;a href="http://code.google.com/p/blogsharp/source/browse/trunk/BlogSharp.CastleExtensions/Facilities/EnrichFacility/EnrichWithFacility.cs?spec=svn112&amp;amp;r=108"&gt;the facility&lt;/a&gt;, &lt;a href="http://code.google.com/p/blogsharp/source/browse/trunk/BlogSharp.CastleExtensions/Facilities/EnrichFacility/FluentRegistration.cs?spec=svn112&amp;amp;r=108"&gt;fluent registration interface&lt;/a&gt;,and &lt;a href="http://code.google.com/p/blogsharp/source/browse/trunk/BlogSharp.CastleExtensions.Tests/Facilities/EnrichWithFacilityTests.cs?spec=svn112&amp;amp;r=108"&gt;the tests&lt;/a&gt; can be found on our never-ending blog engine, BlogSharp codebase.&lt;/p&gt;
&lt;p&gt;If you like the post, please kick and/or shout it.&lt;/p&gt;
&lt;p&gt;
  &lt;br /&gt;&lt;a rev="vote-for" href="http://dotnetshoutout.com/Implementing-EnrichWithof-StructureMap-with-Castle-Tuna-Toksoz-Devlicious"&gt;&lt;img src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fdevlicio.us%2Fblogs%2Ftuna_toksoz%2Farchive%2F2009%2F07%2F14%2Fimplementing-enrichwith-of-structuremap-with-castle.aspx" alt="Shout it" style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=49379" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/tuna_toksoz/archive/tags/castle/default.aspx">castle</category><category domain="http://devlicio.us/blogs/tuna_toksoz/archive/tags/microkernel/default.aspx">microkernel</category><category domain="http://devlicio.us/blogs/tuna_toksoz/archive/tags/windsor/default.aspx">windsor</category></item></channel></rss>