<?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>Why Can&#039;t You Code? &#187; dotnet</title>
	<atom:link href="http://www.whycantyoucode.com/tag/dotnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whycantyoucode.com</link>
	<description>The highest form of ignorance is when you reject something you don&#039;t know anything about.</description>
	<lastBuildDate>Thu, 25 Feb 2010 14:39:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Extension method of the day: Inline using() { } block in C#</title>
		<link>http://www.whycantyoucode.com/2009/11/extension-method-of-the-day-inline-using-block-in-c/</link>
		<comments>http://www.whycantyoucode.com/2009/11/extension-method-of-the-day-inline-using-block-in-c/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 11:20:47 +0000</pubDate>
		<dc:creator>Andrei Alecu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[disposable]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[extension method]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[using]]></category>

		<guid isPermaLink="false">http://www.whycantyoucode.com/?p=113</guid>
		<description><![CDATA[Linq-to-SQL is our ORM of choice until the Entity Framework matures &#8211; and in our projects we use a factory method to get a new reference to our database data context. Here’s what I mean:
var db = Database.GetDataContext();

  The DataContext class implements IDisposable, which means that (usually) it should be wrapped in an using [...]]]></description>
			<content:encoded><![CDATA[<p>Linq-to-SQL is our <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank"><acronym title="Object-relational mapping">ORM</acronym></a> of choice until the Entity Framework matures &#8211; and in our projects we use a factory method to get a new reference to our database data context. Here’s what I mean:</p>
<pre class="brush: csharp;">var db = Database.GetDataContext();</pre>
<p>
  <br />The DataContext class implements <code>IDisposable</code>, which means that (usually) it should be wrapped in an <code>using</code> block: </p>
<p></p>
<pre class="brush: csharp;">using (var db = Database.GetDataContext())
{
    ... code that uses the database here ...
}</pre>
<p>We discovered that we frequently have one liners inside the <code>using</code> block, and in most of these cases, we just return some quick value from the database. </p>
<p>This made me think if there was something I could do about this to make it into a more compact construct. The result is an extension method that lets me do all of this on one line, while also taking care of object disposal.</p>
<p><strong>Introducing &lt;IDisposable&gt;.Using() extension method:</strong></p>
<pre class="brush: csharp;">int userCount = Database.GetDataContext().Using(db =&gt; db.Users.Count()); // get a quick user count</pre>
<p>
  <br />Of course, this applies to any object implementing <code>IDisposable</code>, not just Linq-to-SQL – so it has lots of possible uses. Here’s another example: </p>
<p></p>
<pre class="brush: csharp;">

foreach (string file in Directory.GetFiles(@&quot;C:\TextFiles&quot;, &quot;*.txt&quot;))
{

    string firstLine = new StreamReader(file).Using(f =&gt; f.ReadLine());
    Console.WriteLine(firstLine);
}
</pre>
<p>We’re reading the first line of every file and making sure the file gets closed immediately afterwards. The extension method takes care of disposing the <code>StreamReader</code>, and allows you to focus on the functionality rather than the implementation.</p>
<p>That’s what functional programming is all about!</p>
<p><strong>Extension method follows:</strong></p>
<pre class="brush: csharp;">public static class DisposableExtensions
{
   /// &lt;summary&gt;
   /// Runs a delegate on the specified &lt;see cref=&quot;IDisposable&quot;/&gt; and returns its value.
   /// &lt;/summary&gt;
   /// &lt;typeparam name=&quot;T&quot;&gt;Type of the object implementing &lt;see cref=&quot;IDisposable&quot;/&gt;&lt;/typeparam&gt;
   /// &lt;typeparam name=&quot;TOut&quot;&gt;The type of the return value.&lt;/typeparam&gt;
   /// &lt;param name=&quot;disposable&quot;&gt;The object implementing &lt;see cref=&quot;IDisposable&quot;/&gt;.&lt;/param&gt;
   /// &lt;param name=&quot;func&quot;&gt;A delegate that takes &lt;paramref name=&quot;disposable&quot;/&gt; as a parameter and returns a value.&lt;/param&gt;
   /// &lt;returns&gt;Value returned by &lt;paramref name=&quot;func&quot;/&gt; delegate.&lt;/returns&gt;
   public static TOut Using&lt;T, TOut&gt;(this T disposable, Func&lt;T, TOut&gt; func) where T : IDisposable
   {
       using (disposable)
       {
           return func(disposable);
       }
   }

   /// &lt;summary&gt;
   /// Runs a delegate on the specified &lt;see cref=&quot;IDisposable&quot;/&gt; and does not return anything.
   /// &lt;/summary&gt;
   /// &lt;typeparam name=&quot;T&quot;&gt;Type of the object implementing &lt;see cref=&quot;IDisposable&quot;/&gt;&lt;/typeparam&gt;
   /// &lt;param name=&quot;disposable&quot;&gt;The object implementing &lt;see cref=&quot;IDisposable&quot;/&gt;.&lt;/param&gt;
   /// &lt;param name=&quot;action&quot;&gt;A delegate that takes &lt;paramref name=&quot;disposable&quot;/&gt; as a parameter.&lt;/param&gt;
   public static void Using&lt;T&gt;(this T disposable, Action&lt;T&gt; action) where T : IDisposable
   {
       using (disposable)
       {
           action(disposable);
       }
   }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.whycantyoucode.com/2009/11/extension-method-of-the-day-inline-using-block-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
