<?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 : castle</title><link>http://devlicio.us/blogs/tuna_toksoz/archive/tags/castle/default.aspx</link><description>Tags: castle</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><item><title>Fluent Interface for NH Facility – Take 1</title><link>http://devlicio.us/blogs/tuna_toksoz/archive/2009/06/27/fluent-interface-for-nh-facility-take-1.aspx</link><pubDate>Sat, 27 Jun 2009 11:39:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:48777</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/tuna_toksoz/rsscomments.aspx?PostID=48777</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/tuna_toksoz/commentapi.aspx?PostID=48777</wfw:comment><comments>http://devlicio.us/blogs/tuna_toksoz/archive/2009/06/27/fluent-interface-for-nh-facility-take-1.aspx#comments</comments><description>&lt;p&gt;We’ve been getting many requests on having fluent configuration for NHibernate Integration Facility, and as I like programmatic configuration more than XML configuration (did I mention that I hate XML?), I decided to work on it. After 2-3 hours, I got the below more or less working&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;container.Register(
Fluently.ConfigureFacility()
    .Id(&amp;quot;nhibernateFacility&amp;quot;)
    .DefaultConfigurationBuilder&amp;lt;DefaultConfigurationBuilder&amp;gt;()
    .DefaultConfigurationPersister&amp;lt;DefaultConfigurationPersister&amp;gt;()
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias(&amp;quot;myAlias&amp;quot;)
                        .Id(&amp;quot;myId&amp;quot;)
                        .UsingConfiguration(
                        FactoryConfigurator.DefaultBuilder()
                            .ConnectionProvider(&amp;quot;…………………………&amp;quot;)
                            .ConnectionDriver(&amp;quot;…………………………&amp;quot;)
                            .ConnectionString(&amp;quot;…………………………&amp;quot;)
                            .Dialect(&amp;quot;…………………………&amp;quot;)
                            .ProxyFactory(&amp;quot;…………………………&amp;quot;)
                            .Assemblies(&amp;quot;…………………………&amp;quot;)))
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias(&amp;quot;myAlias&amp;quot;)
                        .Id(&amp;quot;myId&amp;quot;)
                        .UsingConfiguration(
                        FactoryConfigurator.XmlBuilder().File(&amp;quot;myFile.xml&amp;quot;))));&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The pieces in Italic are not necessary to write as they have their defaults, also we have some generic overloads for things like Dialect, ProxyFactory and ConnectionProvider.&lt;/p&gt;

&lt;p&gt;What the above interface does is that it actually converts all the above configuration to IConfiguration and add them to the container as Facility Configuration, this is actually what is done behind the scenes when you use XML configuration. &lt;/p&gt;

&lt;p&gt;I asked for a review over the syntax from several tweeps (&lt;a href="http://twitter.com/kkozmic"&gt;@kkozmic&lt;/a&gt;, &lt;a href="http://twitter.com/dagda1"&gt;@dagda1&lt;/a&gt;, &lt;a href="http://twitter.com/mikehadlow"&gt;@mikehadlow&lt;/a&gt;, &lt;a href="http://twitter.com/chriscanal"&gt;@chriscanal&lt;/a&gt;) and from one other NH Facility user, German Schuager, and got great feedback.&lt;/p&gt;

&lt;p&gt;One of the issues that they pointed out with the above interface is that it is less discoverable. You have to find Fluently class, and then FactoryConfigurator class. Another issue is that it feels less natural to configure the facility like this. Instead, they prefer the configuration take place right on the facility.&lt;/p&gt;

&lt;p&gt;This one is what &lt;a href="http://blog.schuager.com"&gt;German Schuager&lt;/a&gt; has suggested&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;container.AddFacility&amp;lt;NHibernateFacility&amp;gt;(&amp;quot;nhibernateFacility&amp;quot;, cfg =&amp;gt; cfg
        .DefaultConfigurationBuilder&amp;lt;DefaultConfigurationBuilder&amp;gt;()
        .DefaultConfigurationPersister&amp;lt;DefaultConfigurationPersister&amp;gt;()
        .AddFactory(&amp;quot;id1&amp;quot;, f =&amp;gt; f
                .Alias(&amp;quot;myAlias&amp;quot;)
                .UsingConfiguration&amp;lt;DefaultBuilder&amp;gt;(c =&amp;gt; c
                        .ConnectionProvider(&amp;quot;………&amp;quot;)
                        .ConnectionDriver(&amp;quot;………&amp;quot;)
                        .ConnectionString(&amp;quot;………&amp;quot;)
                        .Dialect(&amp;quot;………&amp;quot;)
                        .ProxyFactory(&amp;quot;………&amp;quot;)
                        .Assemblies(&amp;quot;………&amp;quot;)
                ))
        .AddFactory(&amp;quot;id2&amp;quot;, f =&amp;gt; f
                .Alias(&amp;quot;alias&amp;quot;)
                .UsingConfiguration&amp;lt;XmlBuilder&amp;gt;(c =&amp;gt; c
                        .ReadFrom(&amp;quot;myfile.xml&amp;quot;)
                ))
 });&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and similar one from &lt;a href="http://kozmic.pl"&gt;Krzysztof Kozmic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Looks like i was the only one that is fan of Castle Microkernel style Fluent Interface. &lt;/p&gt;

&lt;p&gt;Let’s see what I’ll come up for the second take, If I ever do it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=48777" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category><category domain="http://devlicio.us/blogs/tuna_toksoz/archive/tags/castle/default.aspx">castle</category></item><item><title>Castle NHibernate Facility – Configuration</title><link>http://devlicio.us/blogs/tuna_toksoz/archive/2009/04/06/castle-nhibernate-facility-configuration.aspx</link><pubDate>Mon, 06 Apr 2009 12:56:47 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:45392</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/tuna_toksoz/rsscomments.aspx?PostID=45392</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/tuna_toksoz/commentapi.aspx?PostID=45392</wfw:comment><comments>http://devlicio.us/blogs/tuna_toksoz/archive/2009/04/06/castle-nhibernate-facility-configuration.aspx#comments</comments><description>&lt;p&gt;Castle’s extensibility points provide you many ways to make your life easier. One and most widely used extensibility point is the facilities. By using facilities, you can integrate various other frameworks and technologies easily. &lt;/p&gt; &lt;p&gt;I am going to talk about NHibernate Integration Facility, which is currently lead by me. It was originally written by Hamilton Verissimo, and its current shape is more or less the same as what he wrote. &lt;/p&gt; &lt;p&gt;The purpose of this post is to get feedback and shape the documentation accordingly.&lt;/p&gt; &lt;p&gt;Now, after an historical introduction, lets talk about the facility’s purpose, and in as the first part of the documentation, the configuration.&lt;/p&gt; &lt;p&gt;&lt;font size="3"&gt;&lt;strong&gt;The purpose &lt;br /&gt;&lt;/strong&gt;&lt;/font&gt;The purpose of this facility to provide an easy way to integrate NHibernate into MicroKernel backed applications. The facility provides a nice way to manage multiple session factories, a good way to manage sessions and it also provides some other structures that can be used to further integrate other frameworks such as Fluent NHibernate. It also plays nicely with other Castle services such as Transaction Management. &lt;br /&gt;&lt;/p&gt; &lt;p&gt;&lt;font size="3"&gt;&lt;br /&gt;&lt;strong&gt;The Configuration&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;Currently, the facility can only be configured via XML, but it provides some extension points that can be used to configure it programmatically. The traditional way of configuring it is as given below.&amp;nbsp; &lt;/p&gt;&lt;pre class="xml" name="code"&gt;&amp;lt;configuration&amp;gt;
    &amp;lt;facilities&amp;gt;
        &amp;lt;facility
          id=&amp;quot;nhibernatefacility&amp;quot;
          type=&amp;quot;Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration&amp;quot;
          [optional: configurationBuilder=&amp;quot;Your custom configuration builder&amp;quot;] [optional: isWeb=&amp;quot;Your custom configuration builder&amp;quot;]&amp;gt;
            &amp;lt;factory id=&amp;quot;sessionFactory1&amp;quot;&amp;gt;
                &amp;lt;settings&amp;gt;
                    &amp;lt;item key=&amp;quot;connection.provider&amp;quot;&amp;gt;NHibernate.Connection.DriverConnectionProvider&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;connection.driver_class&amp;quot;&amp;gt;NHibernate.Driver.SqlClientDriver&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;connection.connection_string&amp;quot;&amp;gt;Data Source=.;Initial Catalog=test;Integrated Security=SSPI&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;dialect&amp;quot;&amp;gt;NHibernate.Dialect.MsSql2000Dialect&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle&amp;lt;/item&amp;gt;
                &amp;lt;/settings&amp;gt;
                &amp;lt;assemblies&amp;gt;
                    &amp;lt;assembly&amp;gt;YourAssembly.Name.Here&amp;lt;/assembly&amp;gt;
                &amp;lt;/assemblies&amp;gt;
            &amp;lt;/factory&amp;gt;
        &amp;lt;/facility&amp;gt;
    &amp;lt;/facilities&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;isWeb:&lt;/strong&gt; In case of web application, you should set this to true, implement IContainerAccessor in your HttpApplication (the global.asax) also add following lines to your &amp;lt;httpModules&amp;gt; section. &lt;/p&gt;&lt;pre name="code" class="xml"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="xml" name="code"&gt;&amp;lt;httpModules&amp;gt;
    &amp;lt;add name=&amp;quot;NHibernateSessionWebModule&amp;quot;
             type=&amp;quot;Castle.Facilities.NHibernateIntegration.Components.SessionWebModule, Castle.Facilities.NHibernateIntegration&amp;quot;/&amp;gt;
&amp;lt;/httpModules&amp;gt;&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;As it can bee seen here, it is not very much different from the way we configure NHibernate. What happens behind the scenes are that: Facility creates an NHibernate Configuration, registers the SessionManager, and make those ready to be resolved. &lt;br /&gt;&lt;br /&gt;The facility provides you some way to modify the Configuration that is created behind the scenes. The point is called &lt;strong&gt;IConfigurationContributor&lt;/strong&gt;. By implementing classes that derive from that interface and registering them to container, you’ll be able to modify the NHibernate Configuration, just before the SessionFactory is resolved. The interface is simple &lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;public interface IConfigurationContributor
{
    void Process(string name,Configuration config);
}&lt;/pre&gt;
&lt;p&gt;The name corresponds to the id in the configuration. &lt;br /&gt;&lt;br /&gt;You can also Configure NHibernate Configuration via the IConfigurationBuilder interface. Currently, there are 2 built in ConfigurationBuilders, namely DefaultConfigurationBuilder and XmlConfigurationBuilder. &lt;br /&gt;The default one is the one that parses the thing you see above while XmlConfigurationBuilder uses NH’s native xmls. &lt;br /&gt;&lt;br /&gt;In the next post, I will hopefully show to integrate FluentNHibernate using custom ConfigurationBuilder &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=45392" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category><category domain="http://devlicio.us/blogs/tuna_toksoz/archive/tags/castle/default.aspx">castle</category></item></channel></rss>