<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Software Rockstar</title>
	
	<link>http://www.softwarerockstar.com</link>
	<description>Coaching and mentoring on a journey from a Developer to an IT Leader</description>
	<lastBuildDate>Mon, 22 Apr 2013 15:51:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/SoftwareRockstarArticles" /><feedburner:info uri="softwarerockstararticles" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>32.948974</geo:lat><geo:long>-96.709163</geo:long><feedburner:emailServiceId>SoftwareRockstarArticles</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Dependency Frameworks and Parameterized Constructors</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/SwVmCZ-v5y8/</link>
		<comments>http://www.softwarerockstar.com/2012/04/dependency-frameworks-and-parameterized-constructors/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 01:04:04 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Principles]]></category>
		<category><![CDATA[DI]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Initialize]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[IWorker]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[OOA/OOD]]></category>
		<category><![CDATA[public class]]></category>
		<category><![CDATA[public interface]]></category>
		<category><![CDATA[public void]]></category>
		<category><![CDATA[public Worker]]></category>
		<category><![CDATA[sourcecode]]></category>
		<category><![CDATA[void]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=747</guid>
		<description><![CDATA[Most dependency injection frameworks support some kind of object initialization through parameterized constructors. The question is whether or not it&#8217;s a good idea to use that feature of DI frameworks. Consider the following code: public interface IWorker { void DoWork(); } public class Worker : IWorker { private List &#60; string&#62; _payLoad; public Worker(IList &#60; [...]]]></description>
			<content:encoded><![CDATA[<p>Most dependency injection frameworks support some kind of object initialization through parameterized constructors.  The question is whether or not it&#8217;s a good idea to use that feature of DI frameworks.</p>
<p>Consider the following code:<br />
<span id="more-747"></span></p>
<pre class="brush: csharp">
public interface IWorker
{
    void DoWork();
}

public class Worker : IWorker
{
    private List &lt; string&gt; _payLoad;
    
    public Worker(IList &lt; string&gt; payLoad)
    {
        if (payLoad == null || payLoad.Count() == 0)
            throw new ArgumentException();
            
        _payLoad = payLoad.ToList();
    }
    
    public void DoWork()
    {
        foreach (var item in _payLoad)
        {
            // Do work
        }
    }
}
</pre>
<p>When we use a DI container such as Unity or Castle Windsor to instantiate a Worker object, we must write some specialized code to pass in the constructor parameter (payLoad in our example).  The problem I see with this is that since parameterized constructors can&#8217;t be mandated through interface IWorker, the code is only guaranteed to work with the default implementation of IWorker if one is provided.  That&#8217;s pretty ugly, and pretty much defeats the purpose of using a DI framework.  One of the key benefits of using a DI framework is that a new &#8220;implementation&#8221; can be &#8220;injected&#8221; into code at any time by only making subtle changes to the configuration.</p>
<p>In most cases the code above should be refactored as below:</p>
<pre class="brush: csharp">
public interface IWorker
{
    void DoWork(IList &lt; string&gt; payLoad);
}

public class Worker : IWorker
{
    public void DoWork(IList &lt; string&gt; payLoad)
    {
        if (payLoad == null || payLoad.Count() == 0)
            throw new ArgumentException();
    
        foreach (var item in payLoad.ToList())
        {
            // Do work
        }
    }
}
</pre>
<p>The above code is not only more concise, but is also more portable.  In the event when payLoad needs to be used by more than one Worker method, we can write create an explicit Initialize method to initialize the object.  The code can be refactored as follows:</p>
<pre class="brush: csharp">
public interface IWorker
{
    void DoWork();
    int GetCount();
}

public class Worker : IWorker
{
    private List &lt; string&gt; _payLoad;
    
    public void Initialize(IList &lt; string&gt; payLoad)
    {
        if (payLoad == null || payLoad.Count() == 0)
            throw new ArgumentException();
    
        _payLoad = payLoad.ToList();
    }
    
    public void DoWork()
    {
        if (_payLoad == null)
            throw new InvalidOperationException();
    
        foreach (var item in _payLoad)
        {
            // Do work
        }
    }
    
    public int GetCount()
    {
        if (_payLoad == null)
            throw new InvalidOperationException();
    
        return _payLoad.Count;
    }    
    
}
</pre>
<p>There are some very specialized instances when parameterized constructors need to be used in conjunction with DI frameworks, but those should be exceptions and must be used sparingly with deliberate intent.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=SwVmCZ-v5y8:LrYPr9Vh6XY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=SwVmCZ-v5y8:LrYPr9Vh6XY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=SwVmCZ-v5y8:LrYPr9Vh6XY:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=SwVmCZ-v5y8:LrYPr9Vh6XY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=SwVmCZ-v5y8:LrYPr9Vh6XY:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/SwVmCZ-v5y8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2012/04/dependency-frameworks-and-parameterized-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2012/04/dependency-frameworks-and-parameterized-constructors/</feedburner:origLink></item>
		<item>
		<title>MEF: Easily Creating Plug-in Based Apps In .NET</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/zL7cmD0rHr0/</link>
		<comments>http://www.softwarerockstar.com/2012/04/mef-easily-creating-plugin-apps/#comments</comments>
		<pubDate>Sat, 21 Apr 2012 18:26:39 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[catalog]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[IStyledConsoleWriter]]></category>
		<category><![CDATA[MEF]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[public interface]]></category>
		<category><![CDATA[sourcecode]]></category>
		<category><![CDATA[string text]]></category>
		<category><![CDATA[text]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=651</guid>
		<description><![CDATA[MEF (Managed Extensibility Framework) is an awesome framework that allows easily loading types at runtime making your apps plugin-ready.   Using reflection magic, MEF makes it super easy for any app to accept plugins. Following are 5 easy steps involved in making your app plugin-ready using MEF: Define an interface for plugins.  This is the interface [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/dd460648.aspx" target="_blank">MEF</a> (Managed Extensibility Framework) is an awesome framework that allows easily loading types at runtime making your apps plugin-ready.   Using reflection magic, MEF makes it super easy for any app to accept plugins.</p>
<p>Following are 5 easy steps involved in making your app plugin-ready using MEF:</p>
<ol>
<li>Define an interface for plugins.  This is the interface that each plugin must implement.</li>
<li>Define a metadata interface.  This interface tells your app out of so many plugins which one is the correct plugin for a given piece of functionality.</li>
<li>Define one or more concrete plugin classes.</li>
<li>Decorate each class with some attributes.</li>
<li>In your app write a few lines of code to invoke MEF, and let the magic happen.</li>
</ol>
<p><span id="more-651"></span>While it may not be very useful in the real world, let’s say that we have an extensible app that can write provided text to Console using different styles.  For example input string of “style=warning|This is a warning!” will reproduce text “This is a warning” with red background and white foreground colors.  Since our app is extensible we can create new style pluins at any time, place them in appropriate folder, and start using new styles without ever having to recompile our main app.</p>
<p>In order to achieve this, let’s first define an interface that all our plugins must implement:</p>
<pre class="brush: csharp">
public interface IStyledConsoleWriter
{
    void Write(string text);
}
</pre>
<p>Since our app is extensible, we can have many plugins that implement IStyledConsoleWriter.  How shall the code then know at runtime which plugin to use for which style?  That’s super easy with MEF!  All we have to do is define a metadata interface that will tell our code which plugin to load:</p>
<pre class="brush: csharp">
public interface IStyledConsoleWriterMetadata
{
    string StyleName { get; }
}
</pre>
<p>Once we have these two interfaces, we can create as many plugins as we need using these interfaces such as follows:</p>
<pre class="brush: csharp">
[Export(typeof(IStyledConsoleWriter))]
[ExportMetadata(&quot;StyleName &quot;, &quot;warning&quot;)]
public class StyleProcessor : IStyledConsoleWriter
{
    public string Write(string text)
    {
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine();
        Console.ResetColor();
    }
}
</pre>
<p>In our main app, we can define a variable such as this:</p>
<pre class="brush: csharp">
[ImportMany]
private IEnumerable&lt; Lazy&lt;IStyledConsoleWriter, IStyledConsoleWriterMetadata&gt;&gt; _plugins;
</pre>
<p>Once MEF does it’s magic, the _plugins variable will allow us to enumerate through all plugins and use the one suitable for our need.  In order for MEF to do it’s magic we need to initialize it like this:</p>
<pre class="brush: csharp">
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetEntryAssembly()));
var container = new CompositionContainer(catalog);

container.ComposeParts(this);
</pre>
<p>Essentially what we are doing in above code is that we are initializing MEF with plugins that are dynamically loaded from the same folder as our main app.  Once the above code executes, our _plugins enumerable is initialized which we can query to find the plugin that we need to use using the metadata properties in IStyledConsoleWriterMetadata.</p>
<p>For example, we can have a method in our main code such as this:</p>
<pre class="brush: csharp">
private void ProcessWarningStyle(string payLoad)
{
    var q = (from x in _plugins
        where x.Metadata.StyleName == “warning”
        select x.Value).FirstOrDefault();

    q.Write(payLoad);
}
</pre>
<p>The above code queries our _plugins enumerable to find a plugin that implements the warning style.  Once we find such plugin, we call it’s Write method to write to Console with that style.  That’s it!</p>
<p>There is definitely much more you can do with MEF, but hopefully this will get you started quickly.  If you would like to dig deep, you can refer to the <a href="http://msdn.microsoft.com/en-us/library/dd460648.aspx" target="_blank">official MEF documentation</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=zL7cmD0rHr0:0-nIDCvflMU:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=zL7cmD0rHr0:0-nIDCvflMU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=zL7cmD0rHr0:0-nIDCvflMU:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=zL7cmD0rHr0:0-nIDCvflMU:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=zL7cmD0rHr0:0-nIDCvflMU:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/zL7cmD0rHr0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2012/04/mef-easily-creating-plugin-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2012/04/mef-easily-creating-plugin-apps/</feedburner:origLink></item>
		<item>
		<title>Finding Distinct Elements In A List(T)</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/ggOs6J--Bj8/</link>
		<comments>http://www.softwarerockstar.com/2011/08/finding-distinct-elements-in-a-listt/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 05:26:44 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[complex solutions]]></category>
		<category><![CDATA[Customer]]></category>
		<category><![CDATA[customer class]]></category>
		<category><![CDATA[distinct elements]]></category>
		<category><![CDATA[element]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[public string]]></category>
		<category><![CDATA[target class]]></category>
		<category><![CDATA[var]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=676</guid>
		<description><![CDATA[LINQ provides a Distinct() method, but in order to find distinct elements in a list of some target class, we must first implement the IEqualityComparer&#60;T&#62; interface in our target class. That&#8217;s what Distinct() uses in order to compute whether one element is the same as another element. Implementing IEqualityComparer&#60;T&#62;, however, is not so straightforward as [...]]]></description>
			<content:encoded><![CDATA[<p>LINQ provides a <a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx">Distinct()</a> method, but in order to find distinct elements in a list of some target class, we must first implement the <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms132151.aspx">IEqualityComparer&lt;T&gt;</a> interface in our target class.  That&#8217;s what <a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx">Distinct()</a> uses in order to compute whether one element is the same as another element.  Implementing <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms132151.aspx">IEqualityComparer&lt;T&gt;</a>, however, is not so straightforward as it requires us to override <a href="http://www.google.com/url?sa=t&#038;source=web&#038;cd=2&#038;ved=0CB0QFjAB&#038;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2F336aedhh(v%3Dvs.71).aspx&#038;ei=rio6Tp61MYGctweksqHcAg&#038;usg=AFQjCNH13w_CEJs7V6csZi9f2VJ4rugHNw&#038;sig2=JR4gaX00_PrNLXBGLXMSBQ" target="_blank">Equals()</a> and <a href="http://www.google.com/url?sa=t&#038;source=web&#038;cd=1&#038;sqi=2&#038;ved=0CBUQFjAA&#038;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.object.gethashcode.aspx&#038;ei=4Co6Tp3nFNK4tgfcvvXNDg&#038;usg=AFQjCNFXwuFFRvlbt2CLiNF7wqUWphx10g&#038;sig2=KGBDmnWpJpP512ndK2zAIQ" target="_blank">GetHashCode()</a> methods.  Most of the times if all we need to do is just to find non-duplicated elements in a given list, IEqualityComparer&lt;T&gt; route seems like an overkill.<span id="more-676"></span></p>
<p>Other developers have come up with some slick ways to solve this problem like <a target="_blank" href="http://www.codeproject.com/KB/linq/GenericPropertyComparer.aspx">creating a generic EqualityComparer&lt;T&gt;</a> and <a target="_blank" href="http://johnnycoder.com/blog/2009/12/22/c-hashsett/">using HashSet&lt;T&gt;</a>, but I find both of those methods to be more complex solutions to a simpler problem.</p>
<p>I usually use a much simpler way to accomplish the same task which I will share with you below.  Consider the following rather simple Customer class:</p>
<pre class="brush: csharp">
public class Customer
{   
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
</pre>
<p>We initialize this class with some customers as follows:</p>
<pre class="brush: csharp">
List&lt; Customer &gt; customers = new List&lt; Customer &gt;;
{
    new Customer {FirstName = &quot;John&quot;, LastName = &quot;Doe&quot;},
    new Customer {FirstName = &quot;Jane&quot;, LastName = &quot;Doe&quot;},
    new Customer {FirstName = &quot;John&quot;, LastName = &quot;Doe&quot;},
    new Customer {FirstName = &quot;Jay&quot;, LastName = null},
    new Customer {FirstName = &quot;Jay&quot;, LastName = &quot;Doe&quot;}
};
</pre>
<p>So far so good!  Now all we need is a list of distinct elements by, let&#8217;s say, FirstName (find all elements that have different first names).  We can do that by first grouping our list by FirstName, and then selecting only the first element out of each group as follows:</p>
<pre class="brush: csharp">
var distinctCustomers = customers.GroupBy(s =&gt; s.FirstName)
    .Select(s =&gt; s.First());

// Will print John Doe, Jane Doe, and Jay
foreach(var customer in distinctCustomers)
    Console.WriteLine(&quot;{0} {1}&quot;, customer.FirstName, customer.LastName);
</pre>
<p>Easy enough?  Now let&#8217;s say we need to find out whether or not the list actually contains any duplicates to begin with.  We can also do that easily by again grouping and then computing whether or not any of the groups contain more than 1 elements as follows:</p>
<pre class="brush: csharp">
var hasDusplicates = customers.GroupBy(s =&gt; s.FirstName)
    .Any(s =&gt; s.Count() &gt; 1);

Console.WriteLine(hasDusplicates);
</pre>
<p>I hope that this simplifies your code.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=ggOs6J--Bj8:Cs9KfeiC9uA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=ggOs6J--Bj8:Cs9KfeiC9uA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=ggOs6J--Bj8:Cs9KfeiC9uA:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=ggOs6J--Bj8:Cs9KfeiC9uA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=ggOs6J--Bj8:Cs9KfeiC9uA:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/ggOs6J--Bj8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2011/08/finding-distinct-elements-in-a-listt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2011/08/finding-distinct-elements-in-a-listt/</feedburner:origLink></item>
		<item>
		<title>Texticize – String.Format on Steroids!</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/8zA2iE-rcGo/</link>
		<comments>http://www.softwarerockstar.com/2011/07/texticize-string-format-on-steroids/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 16:42:35 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[Cool Tools]]></category>
		<category><![CDATA[Format]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[string format]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template engine]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[text template]]></category>
		<category><![CDATA[text templates]]></category>
		<category><![CDATA[unstructured text]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=671</guid>
		<description><![CDATA[I just released a new version of open source Texticize. Texticize is an extensible and intuitive object-to-text template engine for .NET. You can use Texticize to quickly create dynamic e-mails, letters, source code, or any other structured/unstructured text documents using predefined text templates substituting placeholders with properties of CLR objects at run-time. Check it out [...]]]></description>
			<content:encoded><![CDATA[<p>I just released a new version of open source Texticize. Texticize is an extensible and intuitive object-to-text template engine for .NET. You can use Texticize to quickly create dynamic e-mails, letters, source code, or any other structured/unstructured text documents using predefined text templates substituting placeholders with properties of CLR objects at run-time.</p>
<p>Check it out on CodePlex at <a href="http://texticize.codeplex.com/">Texticize</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=8zA2iE-rcGo:FQe9bF5rYRA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=8zA2iE-rcGo:FQe9bF5rYRA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=8zA2iE-rcGo:FQe9bF5rYRA:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=8zA2iE-rcGo:FQe9bF5rYRA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=8zA2iE-rcGo:FQe9bF5rYRA:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/8zA2iE-rcGo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2011/07/texticize-string-format-on-steroids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2011/07/texticize-string-format-on-steroids/</feedburner:origLink></item>
		<item>
		<title>Complex Object Mapping Using AutoMapper</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/RdaSjIAuY7o/</link>
		<comments>http://www.softwarerockstar.com/2011/05/complex-object-mapping-using-automapper/#comments</comments>
		<pubDate>Tue, 31 May 2011 03:39:23 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[Cool Tools]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[open source library]]></category>
		<category><![CDATA[property]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[target class]]></category>
		<category><![CDATA[target customer]]></category>
		<category><![CDATA[target object]]></category>
		<category><![CDATA[target objects]]></category>
		<category><![CDATA[type]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=654</guid>
		<description><![CDATA[AutoMapper is an awesome open source library that allows automatic object-to-object mapping.  There are various getting-started blog posts and articles on the web (e.g. Getting Started Guide and AutoMapper on CodeProject) that will get you started with AutoMapper.  I was recently challenged with a particular situation using AutoMapper that wasn&#8217;t immediately clear and took me a little [...]]]></description>
			<content:encoded><![CDATA[<p><a title="AutoMapper on CodePlex" href="http://automapper.codeplex.com/" target="_blank">AutoMapper</a> is an awesome open source library that allows automatic object-to-object mapping.  There are various getting-started blog posts and articles on the web (e.g. <a title="AutoMapper Getting Started Guide" href="http://automapper.codeplex.com/wikipage?title=Getting%20Started" target="_blank">Getting Started Guide</a> and <a title="AutoMapper article on CodeProject" href="http://www.codeproject.com/KB/library/AutoMapper.aspx" target="_blank">AutoMapper on CodeProject</a>) that will get you started with AutoMapper.  I was recently challenged with a particular situation using AutoMapper that wasn&#8217;t immediately clear and took me a little investigating and some trial and error work, so I will share that with you.</p>
<p>My source object exposed some simple properties as well as a generic List property of another type.  My target object had same simple properties, but the generic List property was of an interface type.  Following is a simplified illustration of my source and target objects:</p>
<p><strong>Source Object</strong><br />
<img src="http://www.softwarerockstar.com/wp-content/uploads/2011/05/AutoMapper05302011-1.png" alt="" title="Source Object (AutoMapper Test)" width="434" height="165" class="aligncenter size-medium wp-image-666" /></p>
<p><strong>Target Object</strong><br />
<img src="http://www.softwarerockstar.com/wp-content/uploads/2011/05/AutoMapper05302011-2.png" alt="" title="Target Object (AutoMapper Test)" width="409" height="172" class="aligncenter size-medium wp-image-666" /></p>
<p>A cool thing about AutoMapper is that when it encounters a target object of an interface, it automatically generates a dynamic proxy class using <a title="Castle Project: Dynamic Proxy" href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;sqi=2&amp;ved=0CBkQFjAA&amp;url=http%3A%2F%2Fwww.castleproject.org%2Fdynamicproxy%2Findex.html&amp;ei=kVzkTcyxDJObtweUm4iABw&amp;usg=AFQjCNHpoXQLSB90Ql7zaGC1ARzW9PQWwA" target="_blank">Castle Dynamic Proxy</a>.  This is usually the desired behavior when mapping to a non-existent target class.  In my case, however, the target class already existed, and dynamic proxy was just an overhead.  I tinkered with AutoMapper mappings to actually map to my existing target rather than generating a proxy:</p>
<p><img src="http://www.softwarerockstar.com/wp-content/uploads/2011/05/AutoMapper05302011-3.png" alt="" title="Mapping (AutoMapper Test)" width="732" height="133" class="aligncenter size-medium wp-image-667" /></p>
<p>Essentially what I am doing in my mapping is to first create a map between my source and target Orders objects.  Once that&#8217;s done, I create a map between my source and target Customer objects but provide a custom map for the Orders property.  In my custom map I simply use AutoMapper to map my source Orders property to target Orders property using the map definition I provided earlier (lines 1-2 above).  This works out great and I get my expected target object without incurring the overhead of expensive proxies.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=RdaSjIAuY7o:EU7PWH6nWp4:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=RdaSjIAuY7o:EU7PWH6nWp4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=RdaSjIAuY7o:EU7PWH6nWp4:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=RdaSjIAuY7o:EU7PWH6nWp4:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=RdaSjIAuY7o:EU7PWH6nWp4:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/RdaSjIAuY7o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2011/05/complex-object-mapping-using-automapper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2011/05/complex-object-mapping-using-automapper/</feedburner:origLink></item>
		<item>
		<title>5-minute No-nonsense Intro to Windows Azure</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/hCAHq3DNFWo/</link>
		<comments>http://www.softwarerockstar.com/2011/02/why-windows-azure/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 03:30:05 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[Cloud]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=639</guid>
		<description><![CDATA[I just came across this excellent introduction to Windows Azure from a developer&#8217;s perspective.  I hope you will enjoy.]]></description>
			<content:encoded><![CDATA[<p>I just came across this excellent introduction to Windows Azure from a developer&#8217;s perspective.  I hope you will enjoy.</p>
<p><object type="application/x-silverlight-2" data="data:application/x-silverlight-2," width="640" height="360" ><param name="source" value="http://www.microsoft.com/showcase/silverlight/player/1/player-en.xap" /><param name="initParams" value="Culture=en-US,Uuid=e8313318-004b-4255-8c70-daf54fce2836,Autoplay=False,MarketingOverlayText=Visit this video's website,ShowMarketingOverlay=true,MiscControls=FullScreen;Detached,ShowMenu=True,Tabs=Share,ShowCaption=false,VideoUrl=http://www.microsoft.com/showcase/en/us/details/e8313318-004b-4255-8c70-daf54fce2836,Mode=Player" /><param name="enableHtmlAccess" value="true" /><param name="allowHtmlPopupwindow" value="true" /><param name="background" value="#FF000000" /><param name="minRuntimeVersion" value="3.0.40624.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156" style="text-decoration: none;" onmousedown="javascript:new Image().src = 'http://m.webtrends.com/dcsygm2gb10000kf9xm7kfvub_9p1t/dcs.gif?dcsdat=' + new Date().getTime() + '&#038;dcssip=www.microsoft.com&#038;dcsuri=' + window.location.href + '&#038;WT.tz=-8&#038;WT.bh=16&#038;WT.ul=en-US&#038;WT.cd=32&#038;WT.jo=Yes&#038;WT.ti=&#038;WT.js=Yes&#038;WT.jv=1.5&#038;WT.fi=Yes&#038;WT.fv=10.0&#038;WT.sli=Not%20Installed&#038;WT.slv=Version%20Unavailable&#038;WT.dl=1&#038;WT.seg_1=Not%20Logged%20In&#038;WT.vt_f_a=2&#038;WT.vt_f=2&#038;WT.vt_nvr1=2&#038;WT.vt_nvr2=2&#038;WT.vt_nvr3=2&#038;WT.vt_nvr4=2&#038;vp_site=Embedded&#038;wtEvtSrc=' + window.location.href + '&#038;vp_sli=Embedded'"><img src="http://img.microsoft.com/showcase/Content/img/resx/en-US/installSL.gif" alt="Get Microsoft Silverlight" style="border-style: none"/></a><noscript>
<div><img alt="DCSIMG" id="DCSIMG" width="1" height="1" src="http://m.webtrends.com/dcsygm2gb10000kf9xm7kfvub_9p1t/njs.gif?dcsuri=/nojavascript&amp;WT.js=No"/></div>
<p></noscript></object></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=hCAHq3DNFWo:0yltAmoWFSY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=hCAHq3DNFWo:0yltAmoWFSY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=hCAHq3DNFWo:0yltAmoWFSY:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=hCAHq3DNFWo:0yltAmoWFSY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=hCAHq3DNFWo:0yltAmoWFSY:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/hCAHq3DNFWo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2011/02/why-windows-azure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2011/02/why-windows-azure/</feedburner:origLink></item>
		<item>
		<title>POCO in ADO.NET Entity Framework 4</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/9Vl0ixr0XR0/</link>
		<comments>http://www.softwarerockstar.com/2011/01/poco-in-ado-net-entity-framework-4/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 22:45:42 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Change]]></category>
		<category><![CDATA[data access technologies]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[entity models]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[object relational mapping]]></category>
		<category><![CDATA[OOA/OOD]]></category>
		<category><![CDATA[OR/M]]></category>
		<category><![CDATA[oriented domain]]></category>
		<category><![CDATA[POCO]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[trivial data]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=625</guid>
		<description><![CDATA[ADO.NET Entity Framework means many things to many people.  At it’s core, however, it allows for Object Relational Mapping (ORM) by providing a layer of abstraction between relational databases and Object Oriented domain objects. The first version of Entity Framework was introduced with .NET Framework 3.5 SP1 and Visual Studio 2008 SP1.  While the intention [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/aa697427(v=vs.80).aspx">ADO.NET Entity Framework</a> means many things to many people.  At it’s core, however, it allows for Object Relational Mapping (ORM) by providing a layer of abstraction between relational databases and Object Oriented domain objects.</p>
<p>The first version of Entity Framework was introduced with .NET Framework 3.5 SP1 and Visual Studio 2008 SP1.  While the intention was great, most of everyone that I know, including my own self who have ever tried using EFv1 for anything except trivial data access have developed a love-and-hate relationship with it.  The first version looked great at first, but turned out to be an immature product at best with severe restrictions as well as quite a few bugs.</p>
<p>With .NET 4 Microsoft released a new version of Entity Framework that has addressed many of the issues and made it a much more stable technology.  Amongst other things, Microsoft heard developer community at large and provided POCO support in EFv4.</p>
<p><strong>POCO Support &#8211; At Last!</strong></p>
<p>The biggest problem with EFv1 was that the domain classes had to be tightly integrated with EF.  Essentially these classes were generated from entity models and developers had no control over code generation.  This limitation didn’t fly very well with most developers, since we like to keep our domain classes generic enough to where we can easily migrate them to any underlying data access technology.  This is especially true since the data access technologies tend to change every so often.</p>
<p>In EFv4 Microsoft has gone to great lengths to support Plain Old CLR Objects (POCO), which is just a fancy name for simple .NET classes with properties that can be used as Data Transfer Objects (DTO) or simply Domain Objects.</p>
<p><a href="http://en.wikipedia.org/wiki/Plain_Old_CLR_Object">POCO</a> classes can be handwritten and easily hooked in to EFv4.  Better yet, EFv4 allows us to generate these classes based on our entity models.  Also we have complete control over code generation since EFv4 uses <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx">T4 templates</a>.  Speaking of <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx">T4 templates</a>, they can also be used to generate <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a> classes for our entity models.</p>
<p><strong>Lazy Loading, Explicit Loading, and Eager Loading</strong></p>
<p>In EFv1 the only way to automatically load related entities was using eager loading.  Essentially eager loading is a <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CCgQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Fnetframework%2Faa904594&amp;ei=JcY0Tb32HsqWhQfy69S9Cw&amp;usg=AFQjCNFXPCT1aJ1K461wVDeLDLlRwCB7Vw&amp;sig2=yZcOmHzhHUottOaC-SApQA">LINQ</a> technique that allows loading related entities in one shot using the Include method on the LINQ query.</p>
<p>EFv4 allows for lazy loading on POCO’s.  The only requirement for this added functionality is that navigation properties on POCO’s be marked as virtual and <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontextoptions.lazyloadingenabled.aspx">LazyLoadingEnabled</a> property of <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontextoptions.aspx">ObjectContextOptions</a> be set to true.  The way EFv4 does this is very cool.  Essentially it generates dynamic proxies overriding the virtual properties on POCO’s and uses those proxies instead of the POCO classes at runtime.</p>
<p>In addition to eager and lazy loading, EFv4 allows explicit loading of related entities.  Explicit loading allows us to explicitly load related entities on demand whenever the need arises.  Using explicit loading is as easy as calling the <a href="http://msdn.microsoft.com/en-us/library/dd382880.aspx">LoadProperty</a> method of the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a> using  the parent entity as well as the navigation property name as parameters.</p>
<p><strong>Change Tracking</strong></p>
<p>EFv4 supports two different types of change tracking which are as follows:</p>
<p><strong>Snapshot Change Tracking</strong>.  Using this kind of change tracking is more efficient in most cases but it might require a few additional lines of code.  When using snapshot change tracking (default behavior in EFv4), the developer must make explicit call to <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx">ObjectContext</a>’s <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.detectchanges.aspx">DetectChanges</a> method in order for the EF change tracker to notice any data that has been changed on the entity since it was first retrieved.  Also calling the <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;sqi=2&amp;ved=0CBMQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fbb336792.aspx&amp;ei=Kcc0TcoZg8yEB7fe0M8L&amp;usg=AFQjCNE7rfcpXzJatP4e27jk4gjOGewMGg&amp;sig2=xpf8b9OofvMUSm3GB8arTQ">SaveChanges</a> method of the context automatically detects any changes to entities involved.</p>
<p><strong>Real-time Change Tracking</strong>.  Using real-time change tracking the tracker keeps track of all data changes to entities in real-time.  For real-time change tracking to work for POCO’s, all properties of POCO’s should be marked as virtual.  Also any navigation properties should have return types of <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBYQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2F92t2ye13.aspx&amp;ei=vcc0TeGPL4uwhQfRl728Cw&amp;usg=AFQjCNH9Rx-AertMm5Z70BoXDKVKjEf7zg&amp;sig2=qZlGJ7KMoxVlnZnGtwxeLw">ICollection&lt;T&gt;</a>.  Once these two conditions are met, EFv4 automatically generates dynamic proxies and uses them instead of the POCO’s and tracks changes made to data contained within those entities in real time.</p>
<p><strong>Design Techniques</strong></p>
<p>EFv4 allows a couple of different design techniques that ought to satisfy most situations.</p>
<p><strong>Model First</strong>.  For new projects where that database design is not already set in stone or is non-existent, model-first approach is more natural to most developers and makes a lot of sense.  What this entails is that the developer creates an entity model using the <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBoQFjAA&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fcc716685.aspx&amp;ei=K8g0TYjSF4GGhQebm-CYCw&amp;usg=AFQjCNEisyxXXpKQ55JXj6LiDlN9upsjXg&amp;sig2=jiah8MV9pRlpkhyEbdcjLA">Entity Model Designer</a> for the domain objects involved.  This model can be easily changed during the design phase or the early development stages.  Once the architect/developer is happy with the entity design, he/she can use EFv4 to generate the data model using EFv4’s Data Definition Language (DDL) generation capabilities.  Not only that this is a more natural way of designing data access and domain model for a given solution, it’s more agile in that the model can easily be modified using a GUI tool.  Also as long as the underlying database is supported by EFv4, the developer need not worry about the exact syntax of the DDL as it can automatically be generated.  Once again the generation of DDL can be influenced using <a href="http://msdn.microsoft.com/en-us/library/bb126445.aspx">T4 templates</a>.</p>
<p><strong>Database First</strong>.  This technique is more suitable for when a project involves an existing database.  In such cases EF can easily generate domain models based on database schema.  This design technique is not new to EFv4 as it has been available since the first version of EF and this is probably what most of us are most familiar with.</p>
<p><strong>Code Only</strong>.  EFv4 introduced another design technique which requires no models to be created.  Essentially there are only code classes and EF infers the model from them.  Entire database including all required tables can be generated by EF at run-time.</p>
<p>There’s a lot more to EFv4.  What I have covered in this article only addresses a bit of POCO support in EFv4 since that has been the number 1 request of the development community since EFv1 was released.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=9Vl0ixr0XR0:BXrSCiZd3-Q:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=9Vl0ixr0XR0:BXrSCiZd3-Q:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=9Vl0ixr0XR0:BXrSCiZd3-Q:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=9Vl0ixr0XR0:BXrSCiZd3-Q:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=9Vl0ixr0XR0:BXrSCiZd3-Q:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/9Vl0ixr0XR0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2011/01/poco-in-ado-net-entity-framework-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2011/01/poco-in-ado-net-entity-framework-4/</feedburner:origLink></item>
		<item>
		<title>Simplify N-tier Development with WCF RIA Services</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/xoH38oMS5mU/</link>
		<comments>http://www.softwarerockstar.com/2011/01/simplify-n-tier-development-with-wcf-ria-services/#comments</comments>
		<pubDate>Fri, 14 Jan 2011 18:59:36 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[automatic batch]]></category>
		<category><![CDATA[batch updates]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[data validation]]></category>
		<category><![CDATA[developmentor]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[RIA]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Studio]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[tier]]></category>
		<category><![CDATA[tier development]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=619</guid>
		<description><![CDATA[Last night I was at Dallas .NET User Group meeting where Tony Sneed of DevelopMentor made a presentation on WCF RIA Services called &#8220;Turbocharge Silverlight Development with WCF RIA Services&#8220;. I had read about this new technology before, but never had the time to play with it. The presentation turned out to be pretty cool. [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I was at Dallas .NET User Group meeting where <a title="Tony Sneed's Blog" href="http://blog.tonysneed.com/" target="_blank">Tony Sneed</a> of <a title="DevlopMentor Home Page" href="http://www.develop.com/" target="_blank">DevelopMentor</a> made a presentation on <a title="WCF RIA Services Home Page" href="http://msdn.microsoft.com/en-us/library/ee707344(v=vs.91).aspx" target="_blank">WCF RIA Services</a> called &#8220;<a href="http://blog.tonysneed.com/2011/01/06/wcf-ria-services-talk/" target="_blank">Turbocharge Silverlight Development with WCF RIA Services</a>&#8220;.  I had read about this new technology before, but never had the time to play with it.</p>
<p>The presentation turned out to be pretty cool.  I think WCF RIA Services provides a very neat way to design n-tier apps without much hassle as it takes care of much of the heavy lifting for you providing very easy data validation, authentication, and role-based security amongst other awesomeness.  Also through code projection it automatically allows shared code between UI and the service tier, which makes it easy to write code once and use it cross-tier.  While WCF RIA Services can be used with any front-end, e.g. WinForms and ASP.NET, it really shines when combined with a Silverlight front-end.  The best part of the technology is automatic batch updates, which is really neat.</p>
<p><strong>Download and Install</strong></p>
<ol>
<li>Install WCF RIA Services for Silverlight 4
<ul>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=177508">Install WCF RIA Services</a> for Silverlight 4 and Visual Studio 2010</li>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=185121">Install the WCF RIA Services Toolkit</a></li>
<li>If you already have the Silverlight 4 Tools and Visual Studio 2010 installed you can <a href="http://go.microsoft.com/fwlink/?LinkID=169231">download WCF RIA Services V1.0 directly</a></li>
</ul>
</li>
<li>Install WCF RIA Services SP1 Beta for Silverlight 4
<ul>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=205085">Install WCF RIA Services</a> SP1 Beta for Silverlight 4 and Visual Studio 2010</li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=205088">Install the WCF RIA Services December 2010 Toolkit</a> for WCF RIA Services SP1 Beta</li>
</ul>
</li>
</ol>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=xoH38oMS5mU:V_D_QIHd3JM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=xoH38oMS5mU:V_D_QIHd3JM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=xoH38oMS5mU:V_D_QIHd3JM:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=xoH38oMS5mU:V_D_QIHd3JM:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=xoH38oMS5mU:V_D_QIHd3JM:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/xoH38oMS5mU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2011/01/simplify-n-tier-development-with-wcf-ria-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2011/01/simplify-n-tier-development-with-wcf-ria-services/</feedburner:origLink></item>
		<item>
		<title>An Elegant Way to Implement ReadOnlyDictionary(TKey, TValue) in C#</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/aTQPqN6e1tw/</link>
		<comments>http://www.softwarerockstar.com/2010/10/readonlydictionary-tkey-tvalue/#comments</comments>
		<pubDate>Sat, 30 Oct 2010 05:23:24 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[application framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[dictionary object]]></category>
		<category><![CDATA[fcl]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[personal project]]></category>
		<category><![CDATA[ReadOnlyDictionary]]></category>
		<category><![CDATA[return]]></category>
		<category><![CDATA[TKey]]></category>
		<category><![CDATA[tvalue]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=569</guid>
		<description><![CDATA[While working on creating an application framework for a personal project, I needed a way to expose a Dictionary&#60;TKey, TValue&#62; object from one of my methods. The only caveat being, I wanted the Dictionary object to be read-only. I knew that there exists a ReadOnlyCollection&#60;T&#62; in the FCL, but to my dismay there is no [...]]]></description>
			<content:encoded><![CDATA[<p>While working on creating an application framework for a personal project, I needed a way to expose a <a href="http://www.google.com/url?url=http://msdn.microsoft.com/en-us/library/xfhwa508.aspx&amp;rct=j&amp;sa=U&amp;ei=lpjLTNWXBIO78gaO5824AQ&amp;ved=0CBgQFjAA&amp;sig2=N2tUmql3Zzu8no1euPoCgA&amp;q=Dictionary%3CT%3E&amp;usg=AFQjCNFBme9m1NfGLRApO9FAi33ApxpW_A">Dictionary&lt;TKey, TValue&gt;</a> object from one of my methods.  The only caveat being, I wanted the Dictionary object to be read-only.<span id="more-569"></span></p>
<p>I knew that there exists a <a href="http://www.google.com/url?url=http://msdn.microsoft.com/en-us/library/ms132474.aspx&amp;rct=j&amp;sa=U&amp;ei=-JfLTLjyGIet8Abq2vn1AQ&amp;ved=0CBUQFjAA&amp;sig2=4R0hZntUVwFGdm26-vcAJg&amp;q=ReadOnlyCollection%3CT%3E&amp;usg=AFQjCNHc9Ha2z8inMldOBz1W6frCmo0ntQ">ReadOnlyCollection&lt;T&gt;</a> in the <a href="http://www.google.com/url?url=http://msdn.microsoft.com/en-us/library/d11h6832(VS.71).aspx&amp;rct=j&amp;sa=U&amp;ei=OZjLTN-DOsT48AatsrmnAQ&amp;sqi=2&amp;ved=0CCgQFjAD&amp;sig2=vLbi9KV068qpaqf05eJ6fw&amp;q=.net+fcl&amp;usg=AFQjCNHQ0uT8EIK5n2BqUQ6c50vvRLpMsQ">FCL</a>, but to my dismay there is no built-n way of creating a read-only Dictionary in .NET.  I searched the web to see how other people are solving this problem, but didn&#8217;t find a suitable implementation that I was happy with.  Hence I created a nice little ReadOnlyDictionary&lt;TKey, TValue&gt; that essentially allows developers to expose an immutable Dictionary object of specified key and value types.</p>
<pre class="brush: csharp">
public sealed class ReadOnlyDictionary &lt; TKey, TValue &gt; : ReadOnlyCollection &lt; KeyValuePair &lt; TKey, TValue &gt; &gt;
{
    public ReadOnlyDictionary(IDictionary&lt;tkey , TValue&gt; items)
        : base(items.ToList()) { }

    public TValue this[TKey key]
    {
        get
        {
            var valueQuery = GetQuery(key);
            if (valueQuery.Count() == 0)
                throw new NullReferenceException(&quot;No value found for given key&quot;);
            
            return valueQuery.First().Value;
        }
    }

    public bool ContainsKey(TKey key)
    {
        return (GetQuery(key).Count() &gt; 0);
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
        var toReturn = ContainsKey(key);
        value = toReturn ? this[key] : default(TValue);
        return toReturn;
    }

    private IEnumerable &lt; KeyValuePair &lt; TKey, TValue &gt; &gt; GetQuery(TKey key)
    {
        return (from t in base.Items where t.Key.Equals(key) select t);
    }
}
</pre>
<p>As you can see the class itself is very concise.  This is because it cleverly derives from <a href="http://msdn.microsoft.com/en-us/library/ms132474.aspx">ReadOnlyCollection</a>&lt;<a href="http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx">KeyValuePair&lt;TKey, TValue&gt;</a>&gt;, and only adds two methods and an indexer.  Also it uses <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx">LINQ to Objects</a> to select Dictionary items, and the LINQ query is neatly tucked in a private method where it&#8217;s re-used from other public methods.</tkey></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=aTQPqN6e1tw:cISdDE4DFYw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=aTQPqN6e1tw:cISdDE4DFYw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=aTQPqN6e1tw:cISdDE4DFYw:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=aTQPqN6e1tw:cISdDE4DFYw:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=aTQPqN6e1tw:cISdDE4DFYw:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/aTQPqN6e1tw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2010/10/readonlydictionary-tkey-tvalue/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2010/10/readonlydictionary-tkey-tvalue/</feedburner:origLink></item>
		<item>
		<title>Microsoft Patents The Sudo Command</title>
		<link>http://feedproxy.google.com/~r/SoftwareRockstarArticles/~3/Jx6gqeDxKBQ/</link>
		<comments>http://www.softwarerockstar.com/2009/11/microsoft-patents-the-sudo-command/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 17:53:22 +0000</pubDate>
		<dc:creator>SoftwareRockstar</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Patents]]></category>
		<category><![CDATA[Rants & Raves]]></category>

		<guid isPermaLink="false">http://www.softwarerockstar.com/?p=558</guid>
		<description><![CDATA[U.S. Patent and Trademark Office (USPTO) recently granted Microsoft a patent on a &#8220;personalized version&#8221; of the sudo command with GUI. Sudo is a command that has been in use since 1980&#8242;s or even before by Unix and other operating systems to allow running other commands with elevated privileges. For one, why would Microsoft try [...]]]></description>
			<content:encoded><![CDATA[<div class="imageleft"><img src="http://softwarerockstar.com/wp-content/uploads/2009/11/patent-trademark-office-plaque20m1-277x300.jpg" alt="USPTO Logo" title="USPTO Logo" width="200"/></div>
<p>U.S. Patent and Trademark Office (USPTO) recently granted Microsoft a patent on a &#8220;personalized version&#8221; of the sudo command with GUI.  Sudo is a command that has been in use since 1980&#8242;s or even before by Unix and other operating systems to allow running other commands with elevated privileges.  For one, why would Microsoft try to patent something like this?  Secondly, does USPTO grant patents on anything you ask them for?<span id="more-558"></span></p>
<p>We are witnessing a time in the history of technology when there is more free (open source or otherwise) software available than anyone ever cares to know, and it&#8217;s increasing at a higher rate than anyone can even keep track of.  For any application that one can conceivably imagine, chances are that there is a free alternative out there.  Why in the world then a company such as Microsoft would patent tiny things such as sudo commands, especially when they &#8220;borrowed&#8221; the original idea from elsewhere anyway?  On one hand we see companies like Google<a href="http://www.softwarerockstar.com/2009/11/google-go-a-new-programming-language/"> releasing entire programming languages into open source</a>, and on the other hand companies like Microsoft acquiring patents on simple sudo commands&#8230; we live in truly amazing times don&#8217;t we!</p>
<p><a href="http://www.groklaw.net/article.php?story=20091111094923390">Groklaw</a> has much more detailed information on this story.  You can also find the original Microsoft patent at the USPTO <a href="http://patft1.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&#038;Sect2=HITOFF&#038;d=PALL&#038;p=1&#038;u=/netahtml/PTO/srchnum.htm&#038;r=1&#038;f=G&#038;l=50&#038;s1=7,617,530.PN.&#038;OS=PN/7,617,530&#038;RS=PN/7,617,530">here</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=Jx6gqeDxKBQ:V4FDIIP6ecA:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=Jx6gqeDxKBQ:V4FDIIP6ecA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:KwTdNBX3Jqk"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=Jx6gqeDxKBQ:V4FDIIP6ecA:KwTdNBX3Jqk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?i=Jx6gqeDxKBQ:V4FDIIP6ecA:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?a=Jx6gqeDxKBQ:V4FDIIP6ecA:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/SoftwareRockstarArticles?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/SoftwareRockstarArticles/~4/Jx6gqeDxKBQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.softwarerockstar.com/2009/11/microsoft-patents-the-sudo-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.softwarerockstar.com/2009/11/microsoft-patents-the-sudo-command/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 2.021 seconds. --><!-- Cached page generated by WP-Super-Cache on 2013-04-22 11:07:35 -->
