<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Refactoring C# Style</title>
	<atom:link href="http://www.agileatwork.com/refactoring-c-style/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.agileatwork.com/refactoring-c-style/</link>
	<description>by Michael Valenty</description>
	<lastBuildDate>Tue, 14 May 2013 22:00:08 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Keith</title>
		<link>http://www.agileatwork.com/refactoring-c-style/comment-page-1/#comment-889</link>
		<dc:creator>Keith</dc:creator>
		<pubDate>Wed, 08 Dec 2010 08:51:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.agileatwork.com/?p=524#comment-889</guid>
		<description>Nice job, I had a similar situation. After reading &lt;a href=&quot;http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions&quot; rel=&quot;nofollow&quot;&gt;Ayende&#039;s article on implicit transactions&lt;/a&gt; we made some modifications that created a transaction if one was not present. This resulted in some code duplication and after reading your blog entry I created some NHib extension methods to help remedy the situation.

Duplicate code stank:
&lt;pre class=&quot;prettyprint&quot;&gt;
public IState Find(StateAbbreviationQuery query)
{
	if (session.Transaction.IsActive)
	{
		var stateDto = session.CreateCriteria().Add(Restrictions.Eq(&quot;Key&quot;, query.Abbreviation)).UniqueResult();
		
		return locationFactory.Create(stateDto);
	}

	using (var tx = session.BeginTransaction())
	{
		var stateDto = session.CreateCriteria().Add(Restrictions.Eq(&quot;Key&quot;, query.Abbreviation)).UniqueResult();

		IState state = locationFactory.Create(session.Get(stateDto.StateId));
		tx.Commit();
		return state;
	}
}
&lt;/pre&gt;
I created a few extension methods to remove the duplication...

&lt;pre class=&quot;prettyprint&quot;&gt;
public static class SessionExtensions
{
	public static T Execute(this ISession session, Func f)
	{
		if (session.Transaction.IsActive)
			return f.Invoke();

		using (var tx = session.BeginTransaction())
		{
			var result = f.Invoke();
			tx.Commit();
			return result;
		}
	}

	public static void Execute(this ISession session, Action a)
	{
		if (session.Transaction.IsActive)
			a.Invoke();

		using (var tx = session.BeginTransaction())
		{
			a.Invoke();
			tx.Commit();
		}
	}
}
&lt;/pre&gt;
... And the resulting method:

&lt;pre class=&quot;prettyprint&quot;&gt;
public IState Find(StateAbbreviationQuery query)
{
	var stateDto = session.Execute(() =&gt; session
		.CreateCriteria()
		.Add(Restrictions.Eq(&quot;Key&quot;, query.Abbreviation))
		.UniqueResult());

	return locationFactory.Create(stateDto);
}
&lt;/pre&gt;
I will definitely get refined but, it is much cleaner. Thanks for the ideas.</description>
		<content:encoded><![CDATA[<p>Nice job, I had a similar situation. After reading <a href="http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions" rel="nofollow">Ayende&#8217;s article on implicit transactions</a> we made some modifications that created a transaction if one was not present. This resulted in some code duplication and after reading your blog entry I created some NHib extension methods to help remedy the situation.</p>
<p>Duplicate code stank:</p>
<pre class="prettyprint">
public IState Find(StateAbbreviationQuery query)
{
	if (session.Transaction.IsActive)
	{
		var stateDto = session.CreateCriteria().Add(Restrictions.Eq("Key", query.Abbreviation)).UniqueResult();

		return locationFactory.Create(stateDto);
	}

	using (var tx = session.BeginTransaction())
	{
		var stateDto = session.CreateCriteria().Add(Restrictions.Eq("Key", query.Abbreviation)).UniqueResult();

		IState state = locationFactory.Create(session.Get(stateDto.StateId));
		tx.Commit();
		return state;
	}
}
</pre>
<p>I created a few extension methods to remove the duplication&#8230;</p>
<pre class="prettyprint">
public static class SessionExtensions
{
	public static T Execute(this ISession session, Func f)
	{
		if (session.Transaction.IsActive)
			return f.Invoke();

		using (var tx = session.BeginTransaction())
		{
			var result = f.Invoke();
			tx.Commit();
			return result;
		}
	}

	public static void Execute(this ISession session, Action a)
	{
		if (session.Transaction.IsActive)
			a.Invoke();

		using (var tx = session.BeginTransaction())
		{
			a.Invoke();
			tx.Commit();
		}
	}
}
</pre>
<p>&#8230; And the resulting method:</p>
<pre class="prettyprint">
public IState Find(StateAbbreviationQuery query)
{
	var stateDto = session.Execute(() =&gt; session
		.CreateCriteria()
		.Add(Restrictions.Eq("Key", query.Abbreviation))
		.UniqueResult());

	return locationFactory.Create(stateDto);
}
</pre>
<p>I will definitely get refined but, it is much cleaner. Thanks for the ideas.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
