<?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: A Simple Closure To Handle Try/Catch Around Transactions</title>
	<atom:link href="http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/</link>
	<description></description>
	<lastBuildDate>Tue, 02 Apr 2013 20:36:56 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: J.P. Hamilton</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-50</link>
		<dc:creator>J.P. Hamilton</dc:creator>
		<pubDate>Fri, 04 Apr 2008 17:07:33 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-50</guid>
		<description>This is a great pattern! I have been using something like it since anonymous delegates came out. I&#039;m sure you know this, but there are many other circumstances where you can apply this pattern. I find it handy for encapsulating repetive loops where I do some sort of pre- or post- operations on some collection of objects (over and over again). </description>
		<content:encoded><![CDATA[<p>This is a great pattern! I have been using something like it since anonymous delegates came out. I&#8217;m sure you know this, but there are many other circumstances where you can apply this pattern. I find it handy for encapsulating repetive loops where I do some sort of pre- or post- operations on some collection of objects (over and over again). </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ray Houston</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-49</link>
		<dc:creator>Ray Houston</dc:creator>
		<pubDate>Wed, 02 Apr 2008 12:11:23 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-49</guid>
		<description>@Chad &amp; @Evan - When I first worked with lambdas all I saw was noise, but it was because I wasn&#039;t used to seeing them. Hell, when I first looked at Ruby I thought the same thing. After working with lambdas just a little, the &#039;delegate&#039; keyword became the noise.</description>
		<content:encoded><![CDATA[<p>@Chad &#038; @Evan &#8211; When I first worked with lambdas all I saw was noise, but it was because I wasn&#8217;t used to seeing them. Hell, when I first looked at Ruby I thought the same thing. After working with lambdas just a little, the &#8216;delegate&#8217; keyword became the noise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Myers</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-48</link>
		<dc:creator>Chad Myers</dc:creator>
		<pubDate>Wed, 02 Apr 2008 11:42:39 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-48</guid>
		<description>@Evan - RE: Lambda 
From a technical purity and lambda calculus perspective, I can see how it might be offensive. But when you start using them extensively like Jeremy and I are doing, having to put &#039;delegate&#039; all over the place becomes obviously worse. Especially when you have multiple arguments to the anonymous method and you have to explicitly call them out:

delegate(Product p1, Product p2){ return p1.Code.CompareTo(p2.Code); }

vs.

(p1, p2)=&gt; p1.Code.CompareTo(p2.Code);

</description>
		<content:encoded><![CDATA[<p>@Evan &#8211; RE: Lambda<br />
From a technical purity and lambda calculus perspective, I can see how it might be offensive. But when you start using them extensively like Jeremy and I are doing, having to put &#8216;delegate&#8217; all over the place becomes obviously worse. Especially when you have multiple arguments to the anonymous method and you have to explicitly call them out:</p>
<p>delegate(Product p1, Product p2){ return p1.Code.CompareTo(p2.Code); }</p>
<p>vs.</p>
<p>(p1, p2)=> p1.Code.CompareTo(p2.Code);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ray Houston</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-47</link>
		<dc:creator>Ray Houston</dc:creator>
		<pubDate>Wed, 02 Apr 2008 11:38:29 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-47</guid>
		<description>@Evan - I see where you&#039;re coming from but I&#039;m not sure I agree in this case. Saying With.Transaction may be slightly prettier than context.WithTransaction (or whatever your term is) but it doesn&#039;t seem worth the trouble.

BTW, You could make IDomainContext implement IWith. Then you could return that instance from your With property and get rid of the helper class.</description>
		<content:encoded><![CDATA[<p>@Evan &#8211; I see where you&#8217;re coming from but I&#8217;m not sure I agree in this case. Saying With.Transaction may be slightly prettier than context.WithTransaction (or whatever your term is) but it doesn&#8217;t seem worth the trouble.</p>
<p>BTW, You could make IDomainContext implement IWith. Then you could return that instance from your With property and get rid of the helper class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evan</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-46</link>
		<dc:creator>Evan</dc:creator>
		<pubDate>Wed, 02 Apr 2008 05:09:41 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-46</guid>
		<description>@Ray

Fluenty things often involve a compromise in terms of design purity.  It&#039;s a tradeoff.

You can move the With property onto a layer supertype to clean up the dryness (since service layer classes manage transactions), but that&#039;s a tradeoff on the DI side, since constructor injection in a base class is messy.

If you want to see a similar design, you can see Fowler&#039;s writeup on the Notification Pattern.  His usage of failIfNullOrBlank() on the layer supertype is IMO a very similar design (as the With), but without delegates of course.

http://martinfowler.com/eaaDev/Notification.html

@Chad

I haven&#039;t moved to C# 3.0 land yet.  I&#039;m still in 2.0.  :-)

I do have to say that the non-lambda approach has less noi()s=&gt;e..

Also, trying to cram a lambda where an anonymous method is natural seems a bit awkward to me.</description>
		<content:encoded><![CDATA[<p>@Ray</p>
<p>Fluenty things often involve a compromise in terms of design purity.  It&#8217;s a tradeoff.</p>
<p>You can move the With property onto a layer supertype to clean up the dryness (since service layer classes manage transactions), but that&#8217;s a tradeoff on the DI side, since constructor injection in a base class is messy.</p>
<p>If you want to see a similar design, you can see Fowler&#8217;s writeup on the Notification Pattern.  His usage of failIfNullOrBlank() on the layer supertype is IMO a very similar design (as the With), but without delegates of course.</p>
<p><a href="http://martinfowler.com/eaaDev/Notification.html" rel="nofollow">http://martinfowler.com/eaaDev/Notification.html</a></p>
<p>@Chad</p>
<p>I haven&#8217;t moved to C# 3.0 land yet.  I&#8217;m still in 2.0.  <img src='http://lostechies.com/rayhouston/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I do have to say that the non-lambda approach has less noi()s=>e..</p>
<p>Also, trying to cram a lambda where an anonymous method is natural seems a bit awkward to me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Myers</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-45</link>
		<dc:creator>Chad Myers</dc:creator>
		<pubDate>Wed, 02 Apr 2008 01:20:35 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-45</guid>
		<description>@Evan: That seems like an awful lot of code and indirection just to get the With syntax.

Since this, so far in this discussion, is solely for the With.Transaction() usage, what if you just had an extension method on IDataContext:

context.WithTransaction(()=&gt;
{
    // blah blah
});

</description>
		<content:encoded><![CDATA[<p>@Evan: That seems like an awful lot of code and indirection just to get the With syntax.</p>
<p>Since this, so far in this discussion, is solely for the With.Transaction() usage, what if you just had an extension method on IDataContext:</p>
<p>context.WithTransaction(()=><br />
{<br />
    // blah blah<br />
});</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ray Houston</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-44</link>
		<dc:creator>Ray Houston</dc:creator>
		<pubDate>Wed, 02 Apr 2008 00:44:08 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-44</guid>
		<description>@Evan - something feels dirty about having to create an instance method of With on MyService. You&#039;d have to create that on every class that you use it on. Wouldn&#039;t that be anti DRY?</description>
		<content:encoded><![CDATA[<p>@Evan &#8211; something feels dirty about having to create an instance method of With on MyService. You&#8217;d have to create that on every class that you use it on. Wouldn&#8217;t that be anti DRY?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evan</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-43</link>
		<dc:creator>Evan</dc:creator>
		<pubDate>Wed, 02 Apr 2008 00:11:38 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-43</guid>
		<description>@Chad

There are a couple ways.  Considering transactions are typically managed by the Service Layer, you can finagle it in through there.

I&#039;m assuming there&#039;s some type of IContextFactory or something:

public interface IMyService
{
   void DoSomething();
}

public class MyService : IMyService
{
   public MyService(IDbContextFactory factory)
   {
      _dbFactory = factory;
   }

   private IDbContextFactory  _dbFactory;

   protected IWith With
   {
      get { return new WithHelper(_dbFactory.Create()); }
   }

   public void DoSomething()
   {
      With.Transaction(delegate {
         //do transactional work
      });
   }
}

At which point you&#039;d convert the Transaction method of my previous example into an instance member, instead of a static one.

public interface IWith
{
   void Transaction(VoidDelegate work);
}

public class WithHelper : IWith
{
   public WithHelper(IDbContext context)
   {
      _dbContext = context;
   }

   private IDbContext _dbContext;

   public void Transaction(VoidDelegate work)
   {
      using (IDbTransaction tx = _dbContext.CreateTx())
      {
         work();
         tx.Complete();
      }
   }
}

You can also move the With property onto an abstract layer supertype if you like.  It will make constructor injection tricky though, you might look at Setter injection instead (which I don&#039;t prefer, but it&#039;s a choice).  Also make sure you are taking advantage of IDisposable (since we are talking about resources which should be properly managed and disposed of).

You can take this same approach (using a delegate to fill a hole in the middle) to create an alternative to a Template Method.  I built one recently (but not for transactions), where their was a fluent api wrapping the helper (ie..the Transaction(VoidDelegate work) returned an interface)..

In my case, It worked really well for enforcing the DRYness of some error handling code that was needed in multiple places.</description>
		<content:encoded><![CDATA[<p>@Chad</p>
<p>There are a couple ways.  Considering transactions are typically managed by the Service Layer, you can finagle it in through there.</p>
<p>I&#8217;m assuming there&#8217;s some type of IContextFactory or something:</p>
<p>public interface IMyService<br />
{<br />
   void DoSomething();<br />
}</p>
<p>public class MyService : IMyService<br />
{<br />
   public MyService(IDbContextFactory factory)<br />
   {<br />
      _dbFactory = factory;<br />
   }</p>
<p>   private IDbContextFactory  _dbFactory;</p>
<p>   protected IWith With<br />
   {<br />
      get { return new WithHelper(_dbFactory.Create()); }<br />
   }</p>
<p>   public void DoSomething()<br />
   {<br />
      With.Transaction(delegate {<br />
         //do transactional work<br />
      });<br />
   }<br />
}</p>
<p>At which point you&#8217;d convert the Transaction method of my previous example into an instance member, instead of a static one.</p>
<p>public interface IWith<br />
{<br />
   void Transaction(VoidDelegate work);<br />
}</p>
<p>public class WithHelper : IWith<br />
{<br />
   public WithHelper(IDbContext context)<br />
   {<br />
      _dbContext = context;<br />
   }</p>
<p>   private IDbContext _dbContext;</p>
<p>   public void Transaction(VoidDelegate work)<br />
   {<br />
      using (IDbTransaction tx = _dbContext.CreateTx())<br />
      {<br />
         work();<br />
         tx.Complete();<br />
      }<br />
   }<br />
}</p>
<p>You can also move the With property onto an abstract layer supertype if you like.  It will make constructor injection tricky though, you might look at Setter injection instead (which I don&#8217;t prefer, but it&#8217;s a choice).  Also make sure you are taking advantage of IDisposable (since we are talking about resources which should be properly managed and disposed of).</p>
<p>You can take this same approach (using a delegate to fill a hole in the middle) to create an alternative to a Template Method.  I built one recently (but not for transactions), where their was a fluent api wrapping the helper (ie..the Transaction(VoidDelegate work) returned an interface)..</p>
<p>In my case, It worked really well for enforcing the DRYness of some error handling code that was needed in multiple places.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Myers</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-42</link>
		<dc:creator>Chad Myers</dc:creator>
		<pubDate>Tue, 01 Apr 2008 11:54:59 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-42</guid>
		<description>In .net 3.5, please use the System.Action delegate though, and don&#039;t make a new one (VoidDelegate).  But for .NET 2.0, that&#039;s what you have to do.

@Evan: How do you get the context into the With() class and/or the Transaction() function?</description>
		<content:encoded><![CDATA[<p>In .net 3.5, please use the System.Action delegate though, and don&#8217;t make a new one (VoidDelegate).  But for .NET 2.0, that&#8217;s what you have to do.</p>
<p>@Evan: How do you get the context into the With() class and/or the Transaction() function?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evan</title>
		<link>http://lostechies.com/rayhouston/2008/03/28/a-simple-closure-to-handle-try-catch-around-transactions/#comment-41</link>
		<dc:creator>Evan</dc:creator>
		<pubDate>Tue, 01 Apr 2008 06:46:02 +0000</pubDate>
		<guid isPermaLink="false">/blogs/rhouston/archive/2008/03/27/a-simple-closure-to-handle-try-catch-around-transactions.aspx#comment-41</guid>
		<description>You can also go this route (warning, blog code):

With.Transaction(delegate {
  //do something
});

public delegate void VoidDelegate();

public class With
{
   public static void Transaction(VoidDelegate work)
   {
      using(ITransaction trans = context.BeginTransaction())
      {
         work();
         trans.Complete();
      }
   }
}

I&#039;m omitting the dependency stuff for context, but you should get the gist.  You can do this in 2.0, or spiffy it up with 3.0 features if you like.

I&#039;ve seen people refer to this as the Hole in the Middle pattern.</description>
		<content:encoded><![CDATA[<p>You can also go this route (warning, blog code):</p>
<p>With.Transaction(delegate {<br />
  //do something<br />
});</p>
<p>public delegate void VoidDelegate();</p>
<p>public class With<br />
{<br />
   public static void Transaction(VoidDelegate work)<br />
   {<br />
      using(ITransaction trans = context.BeginTransaction())<br />
      {<br />
         work();<br />
         trans.Complete();<br />
      }<br />
   }<br />
}</p>
<p>I&#8217;m omitting the dependency stuff for context, but you should get the gist.  You can do this in 2.0, or spiffy it up with 3.0 features if you like.</p>
<p>I&#8217;ve seen people refer to this as the Hole in the Middle pattern.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
