<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eric Hauser's Blog &#187; Castle</title>
	<atom:link href="http://erichauser.net/tag/castle/feed/" rel="self" type="application/rss+xml" />
	<link>http://erichauser.net</link>
	<description>J2EE, .NET, and general technology</description>
	<lastBuildDate>Fri, 23 Apr 2010 05:00:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ActiveRecord Flush Control</title>
		<link>http://erichauser.net/2009/01/27/activerecord-flush-control/</link>
		<comments>http://erichauser.net/2009/01/27/activerecord-flush-control/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 20:56:18 +0000</pubDate>
		<dc:creator>ehauser</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Active Record]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://erichauser.net/2009/01/27/activerecord-flush-control/</guid>
		<description><![CDATA[There are a lot of times in your application where you will load a lot of entities in a single session (to display a lot of data on a page, build a report, etc).  Since you are not planning on doing a mutable operation on an entity, there is a big saving that you can [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of times in your application where you will load a lot of entities in a single session (to display a lot of data on a page, build a report, etc).  Since you are not planning on doing a mutable operation on an entity, there is a big saving that you can get from not flushing the session.  To get an idea on everything that happens when you do a flush, check out the <a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Event/Default/AbstractFlushingEventListener.cs">AbstractionFlushingEventListener</a> from the NHibernate source.</p>
<p>If you are using explicit SessionScope’s, not flushing the session is trivial:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> (<span style="color: #0000ff">new</span> SessionScope(FlushAction.Never))</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span>     repository.DoSomeReadOperation();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span> }</pre>
</div>
</div>
<p>This will execute the operation without flushing the session at the end.</p>
<p>Another way to control this behavior is by using an interceptor.  For example, if you are using the Castle’s Automated Transaction Facility, you probably already have a way of determining whether or not your methods do mutable operations by their transaction attributes.  You can then use an interceptor to control flushing:</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> UnitOfWorkInterceptor : IInterceptor</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   2:</span> {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ITransactionManager transactionManager;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   4:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   5:</span>     <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   6:</span>     <span style="color: #008000">/// Initializes a new instance of the &lt;see cref="UnitOfWorkInterceptor"/&gt; class.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   7:</span>     <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">   8:</span>     <span style="color: #008000">/// &lt;param name="transactionManager"&gt;The transaction manager.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">public</span> UnitOfWorkInterceptor(ITransactionManager transactionManager)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  10:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  11:</span>         <span style="color: #0000ff">this</span>.transactionManager = transactionManager;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  12:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  13:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  14:</span>     <span style="color: #cc6633">#region</span> Implementation of IInterceptor</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  15:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  16:</span>     <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  17:</span>     <span style="color: #008000">/// Intercepts the specified invocation.</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  18:</span>     <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  19:</span>     <span style="color: #008000">/// &lt;param name="invocation"&gt;The invocation.&lt;/param&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  20:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Intercept(IInvocation invocation)</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  21:</span>     {</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  22:</span>         ITransaction transaction = transactionManager.CurrentTransaction;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  23:</span>         FlushAction flushAction = transaction == <span style="color: #0000ff">null</span> ? FlushAction.Never : FlushAction.Config;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  24:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  25:</span>         ActiveRecordUnitOfWork.Before(flushAction);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  26:</span>         invocation.Proceed();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  27:</span>         ActiveRecordUnitOfWork.After();</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  28:</span>     }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  29:</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #606060">  30:</span>     <span style="color: #cc6633">#endregion</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;"><span style="color: #606060">  31:</span> }</pre>
</div>
</div>
<p>Note that in this example, interceptor ordering is important.  If you register the unit of work interceptor before the transaction interceptor in the ATM facility, then transactionManager.CurrentTransaction will always return null.  (The ActiveRecordUnitOfWork class in the above example is just a facade for controlling the session in a thread local as discussed in this post <a href="http://erichauser.net/2008/08/06/activerecord-session-scope-and-wcf-redux/">http://erichauser.net/2008/08/06/activerecord-session-scope-and-wcf-redux/</a>).  If you do not want to tie your flushing to transactions, then you can always create your own metadata and read that metadata at runtime in the interceptor.  The ATM facility code is a great example of how to inspect metadata on initialization and use that metadata in your interceptor.</p>
<p>Hopefully, this little trick will some increased performance for your queries that return a lot of results.</p>
]]></content:encoded>
			<wfw:commentRss>http://erichauser.net/2009/01/27/activerecord-flush-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Castle Validator Enhancements</title>
		<link>http://erichauser.net/2008/08/28/castle-validator-enhancements/</link>
		<comments>http://erichauser.net/2008/08/28/castle-validator-enhancements/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 00:30:57 +0000</pubDate>
		<dc:creator>ehauser</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[Validator]]></category>

		<guid isPermaLink="false">http://erichauser.net/?p=75</guid>
		<description><![CDATA[I have contributed a couple of enhancements to Castle.Components.Validator that have been committed to the trunk.  Besides using attributes, validations can now be supplied in code using the [ValidateSelf] attribute:

Each of the above validations *could* be done by using an attribute and a custom validator, but expressing validations in code is much simpler for these [...]]]></description>
			<content:encoded><![CDATA[<p>I have contributed a couple of enhancements to Castle.Components.Validator that have been committed to the trunk.  Besides using attributes, validations can now be supplied in code using the [ValidateSelf] attribute:</p>
<p><a href="http://erichauser.net/wp-content/uploads/validateself.jpg"><img class="alignnone size-full wp-image-76" title="validateself" src="http://erichauser.net/wp-content/uploads/validateself.jpg" alt="" /></a></p>
<p>Each of the above validations *could* be done by using an attribute and a custom validator, but expressing validations in code is much simpler for these types of one off validations.  You can have as many methods decorated with [ValidateSelf] on an object you want as long as they have the above method signature (void return and one ErrorSummary parameter).  You can also specify the RunWhen and ExecutionOrder just like regular validators.</p>
<p>The second enhancement is the IValidationContributor interface.  This allows you contribute to the validation of an object beyond the default validation.  The interface is fairly simplistic:</p>
<p><a href="http://erichauser.net/wp-content/uploads/ivalidatorcontributor.jpg"><img class="alignnone size-full wp-image-77" title="ivalidatorcontributor" src="http://erichauser.net/wp-content/uploads/ivalidatorcontributor.jpg" alt="" /></a></p>
<p>You can extend AbstractValidationContributor so that you can perform initialization for a given type.  The SelfValidationContributor implements the logic for recognizing and executing the self validation feature above.  You can write custom contributors that can be injected into the DefaultValidatorRunner for things like retrieving validations from the container and invoking them on the object. </p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://erichauser.net/2008/08/28/castle-validator-enhancements/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>NServiceBus Facility Enhancements</title>
		<link>http://erichauser.net/2008/08/23/nservicebus-facility-enhancements/</link>
		<comments>http://erichauser.net/2008/08/23/nservicebus-facility-enhancements/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 14:59:41 +0000</pubDate>
		<dc:creator>ehauser</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[Idempotent]]></category>
		<category><![CDATA[NServiceBus]]></category>

		<guid isPermaLink="false">http://erichauser.net/?p=62</guid>
		<description><![CDATA[I have added a couple of additions to Ayende&#8217;s NServiceBus Facility:

Message handlers and sagas are automatically registered with the container for the defined assemblies
Message handlers are proxied
You can register IHandlerFilter instances with the kernel that allow for interception of messages as they are processed
Using my replay strategy implementation, you can decorate a message handler as [Idempotent(Attemps = 5, [...]]]></description>
			<content:encoded><![CDATA[<p>I have added a couple of additions to <a title="NServiceBus Facility" href="http://www.ayende.com/Blog/archive/2008/07/13/Putting-the-container-to-work-Refactoring-NServiceBus-configuration.aspx">Ayende&#8217;s NServiceBus Facility</a>:</p>
<ul>
<li>Message handlers and sagas are automatically registered with the container for the defined assemblies</li>
<li>Message handlers are proxied</li>
<li>You can register IHandlerFilter instances with the kernel that allow for interception of messages as they are processed</li>
<li>Using my replay strategy implementation, you can decorate a message handler as [Idempotent(Attemps = 5, Delay = 2000)].  That means that a message will be retried when an exception is thrown processing the message 5 times, delaying 2 seconds between each retry.  This could also be extended so that depending on the type of exception that is being thrown, a default retry strategy is applied.</li>
</ul>
<p><a href="http://erichauser.net/wp-content/uploads/idempotent_attribute1.jpg"><img class="alignnone size-full wp-image-71" title="idempotent_attribute1" src="http://erichauser.net/wp-content/uploads/idempotent_attribute1.jpg" alt="" /></a></p>
<ul>
<li>The bus is started automatically once all handler dependencies have been satisfied</li>
</ul>
<p>There were a couple of interesting things that I found when implementing this.  One, NServiceBus retrieives handlers from the kernel using the concrete class instance (looking back Ayende noted this as &#8220;yuck&#8221;).  That means that we cannot proxy the interface, but instead have to create a class proxy for our message handlers.  That means that when we the message handlers, we have to verify that the message handler methods are virtual and give a nice error if they are not:</p>
<p><a href="http://erichauser.net/wp-content/uploads/registerhandlers.jpg"><img class="alignnone size-full wp-image-65" title="registerhandlers" src="http://erichauser.net/wp-content/uploads/registerhandlers.jpg" alt="" /></a></p>
<p>Very easy to do with Castle&#8217;s fluent interface for registration.  Sagas, on the other hand, have to be registered in the container by each of their interfaces because saga instances are retrieve by calling builder.Build&lt;ISaga&lt;SomeMessage&gt;&gt;().</p>
<p>Out of the box, the way to do message interception with NSB is to have message handlers chained in a specified order.  The IHandlerFilter provides another method for message interception.  If you have a new filter to add, all you have to do is register the IHandlerFilter instance with the container:</p>
<p><a href="http://erichauser.net/wp-content/uploads/handlerfilter1.jpg"><img class="alignnone size-full wp-image-67" title="handlerfilter1" src="http://erichauser.net/wp-content/uploads/handlerfilter1.jpg" alt="" /></a></p>
<p>The filters follow the chain of responsibility pattern, so they can be short circuited &#8212; which is the same as calling bus.DoNotContinueDispatchingCurrentMessageToHandlers().</p>
<p>I have attached the updates to the post: <a href="http://erichauser.net/wp-content/uploads/nservicebus-fullduplex-xml-update.zip">nservicebus-fullduplex-xml-update</a>.  Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://erichauser.net/2008/08/23/nservicebus-facility-enhancements/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
