<?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>Alvin Ashcraft&#039;s Morning Dew &#187; how-to</title>
	<atom:link href="http://www.alvinashcraft.com/category/development/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alvinashcraft.com</link>
	<description>.NET Development Resources from a Progressive.NET Perspective</description>
	<lastBuildDate>Thu, 29 Jul 2010 12:03:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Extend a Generic List to Provide Dictionary-Like Lookups</title>
		<link>http://www.alvinashcraft.com/2008/09/22/extend-a-generic-list-to-provide-dictionary-like-lookups/</link>
		<comments>http://www.alvinashcraft.com/2008/09/22/extend-a-generic-list-to-provide-dictionary-like-lookups/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 01:17:11 +0000</pubDate>
		<dc:creator>Alvin Ashcraft</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">http://www.alvinashcraft.com/2008/09/22/extend-a-generic-list-to-provide-dictionary-like-lookups/</guid>
		<description><![CDATA[&#160; Welcome to a quick and dirty example in which I’ll make use of interfaces, inheritance and generics. I was working with some code that did a lot of iterating over collections to find objects with a particular ID. This is an abstracted example of what I did to simplify and centralize that code in [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>Welcome to a quick and dirty example in which I’ll make use of interfaces, inheritance and generics. I was working with some code that did a lot of iterating over collections to find objects with a particular ID. This is an abstracted example of what I did to simplify and centralize that code in a reusable manner.</p>
<p><em>If you are using Visual Studio 2008, you could achieve the same kind of reuse with an extension method on your generic list. The project I was working on was in .NET 2.0 and Visual Studio 2005.</em></p>
<p>This method returns a student’s course based on its ID. The student object here is a private class member. The CurrentCourses property is a List&lt;Course&gt; collection.</p>
<blockquote><pre class="code"><span style="color: blue">private </span><span style="color: #2b91af">Course </span>FindCourseOld(<span style="color: blue">int </span>id)
{
    <span style="color: blue">foreach </span>(<span style="color: blue">var </span>course <span style="color: blue">in </span>_student.CurrentCourses)
    {
        <span style="color: blue">if </span>(course.ID == id)
        {
            <span style="color: blue">return </span>course;
        }
    }

    <span style="color: blue">return null</span>;
}</pre>
</blockquote>
<pre class="code">&nbsp;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>It’s pretty simple, but suppose you had a dozen or more types with an ID property that were all queries like this? It smells like an opportunity to do some refactoring. We need to keep all the functionality of the List&lt;T&gt; generic collection, including sorting, index operations, and enumerating, but we want to provide a dictionary-type of lookup on the ID property. Let’s inherit from List&lt;T&gt; and add a method to perform our lookup.</p>
<p>Before an object can be stored in this new collection, our child of List&lt;T&gt;, that we are going to create, we need to ensure it has an ID property on which we can operate. To that end, I present to you the IHasIdentifier interface. I know it’s not grammatically correct, but I think it gets the point across.</p>
<blockquote><pre class="code"><span style="color: blue">public interface </span><span style="color: #2b91af">IHasIdentifier
</span>{
<span style="color: gray">    </span><span style="color: blue">int </span>ID { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
}</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>&nbsp;</p>
<p>The new interface has a single property named ‘ID’, which many of the types you might consider using in our collection may already have. Next up is the IdentityList&lt;T&gt; itself. The implementation I have here is very simple. There is just a single method, GetItemByID, added to the base class, List&lt;T&gt;.</p>
<blockquote><pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">IdentityList</span>&lt;T&gt; : <span style="color: #2b91af">List</span>&lt;T&gt; <span style="color: blue">where </span>T : <span style="color: #2b91af">IHasIdentifier
</span>{
<span style="color: gray">    </span><span style="color: blue">public </span>T GetItemByID(<span style="color: blue">int </span>id)
    {
        <span style="color: blue">foreach </span>(<span style="color: blue">var </span>obj <span style="color: blue">in this</span>)
        {
            <span style="color: blue">if </span>(obj.ID == id)
            {
                <span style="color: blue">return </span>obj;
            }
        }

        <span style="color: blue">return default</span>(T);
    }
}</pre>
</blockquote>
<p>&nbsp;</p>
<p>We’re inheriting from List&lt;T&gt; and specifying that T must implement IHasIdentifier. Now, in the GetItemByID method, we can do our iteration to find the object with the given ID and return it. Finally, let’s take a look at the FindCourseNew method which uses our IdentityList&lt;T&gt; collection.</p>
<pre class="code"><span style="color: blue">private </span><span style="color: #2b91af">Course </span>FindCourseNew(<span style="color: blue">int </span>id)
{
    <span style="color: blue">return </span>_student.CurrentCourses.GetItemByID(id);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>&nbsp;</p>
<p>The code here is much cleaner, and there could be more opportunity for code reuse with the IdentityList&lt;T&gt;. Many of the dictionary type of methods could be adapted and used within this collection. You can download the sample code <a target="_blank" href="http://www.alvinashcraft.com/code/IdentityListExample.zip">here</a>. As always, any and all feedback is welcome!</p>
<p>&nbsp;</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C16BAC14-9A3D-4c50-9394-FBFEF7A93539:c6dd5048-588f-436d-b6e0-946b0bdaa220" class="wlWriterEditableSmartContent"><a href="http://www.dotnetkicks.com/kick/?url=http://www.alvinashcraft.com/2008/09/22/extend-a-generic-list-to-provide-dictionary-like-lookups/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.alvinashcraft.com/2008/09/22/extend-a-generic-list-to-provide-dictionary-like-lookups/" border="0" alt="kick it on DotNetKicks.com" /></a></div>
<p>&nbsp;</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e2b15131-3d1b-4ff9-b6ef-6322e312684f" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/.net+development" rel="tag">.net development</a>,<a href="http://technorati.com/tags/coding+tips" rel="tag">coding tips</a>,<a href="http://technorati.com/tags/c%23" rel="tag">c#</a>,<a href="http://technorati.com/tags/generics" rel="tag">generics</a>,<a href="http://technorati.com/tags/List+collection" rel="tag">List collection</a></div>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Extend+a+Generic+List+to+Provide+Dictionary-Like+Lookups+http://m8nrr.th8.us" title="Post to Twitter"><img class="nothumb" src="http://www.alvinashcraft.com/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Extend+a+Generic+List+to+Provide+Dictionary-Like+Lookups+http://m8nrr.th8.us" title="Post to Twitter">Tweet This Post</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.alvinashcraft.com/2008/09/22/extend-a-generic-list-to-provide-dictionary-like-lookups/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Roland Weigelt&#8217;s GhostDoc for Visual Studio</title>
		<link>http://www.alvinashcraft.com/2008/08/23/roland-weigelts-ghostdoc-for-visual-studio/</link>
		<comments>http://www.alvinashcraft.com/2008/08/23/roland-weigelts-ghostdoc-for-visual-studio/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 18:29:06 +0000</pubDate>
		<dc:creator>Alvin Ashcraft</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[how-to]]></category>

		<guid isPermaLink="false">http://www.alvinashcraft.com/2008/08/23/roland-weigelts-ghostdoc-for-visual-studio/</guid>
		<description><![CDATA[&#160; I have been using Roland WeigeIt’s GhostDoc 2.1.3 for both Visual Studio 2005 and 2008 for about six months, and I am hooked. For those of you who have not heard of GhostDoc, here is the summary from Roland’s site: GhostDoc is a free add-in for Visual Studio that automatically generates XML documentation comments [...]]]></description>
			<content:encoded><![CDATA[</p>
<p>&#160;</p>
<p>I have been using <a href="http://www.roland-weigelt.de/ghostdoc/" target="_blank">Roland WeigeIt’s GhostDoc</a> 2.1.3 for both Visual Studio 2005 and 2008 for about six months, and I am hooked. For those of you who have not heard of GhostDoc, here is the summary from Roland’s site:</p>
<blockquote><p>GhostDoc is a free add-in for Visual Studio that automatically generates XML documentation comments for C#. Either by using existing documentation inherited from base classes or implemented interfaces, or by deducing comments from name and type of e.g. methods, properties or parameters.</p>
</blockquote>
</p>
</p>
<p>It saves me so much time writing XML documentation in my code that there is no longer any excuse for any docs being missing. Documenting a piece of code is as simple as right-clicking on it and selcting ‘Document This’ (there is also a keyboard shortcut – CTRL-D is the default). The built-in rules for creating the comments are probably sufficient for most developers, but more advanced users can edit existing rules or add their own.</p>
<p>I have attached a small sample project containing some domain classes representing part of a pharmacy system. Here are a few examples of the XML documentation that is generated using GhostDoc’s default configuration. Some of the code is a bit contrived so that I could demonstrate different types of comments.</p>
</p>
</p>
</p>
<h5>Documenting a Constructor</h5>
<blockquote><pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Initializes a new instance of the </span><span style="color: gray">&lt;see cref=&quot;Drug&quot;/&gt; </span><span style="color: green">class.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;param name=&quot;id&quot;&gt;</span><span style="color: green">The id.</span><span style="color: gray">&lt;/param&gt;
</span><span style="color: blue">public </span>Drug(<span style="color: blue">int </span>id)
{
    LoadDrug(id);
}</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h5>Documenting a Dispose Method</h5>
<blockquote>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public void </span>Dispose()
{
    <span style="color: green">// TODO: clean up resources
</span>}</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>&#160;</p>
<h5>Documenting Properties</h5>
<blockquote>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Gets or sets the therapeutic categories.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;value&gt;</span><span style="color: green">The therapeutic categories.</span><span style="color: gray">&lt;/value&gt;
</span><span style="color: blue">public </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">DrugCategory</span>&gt; TherapeuticCategories { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

<span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Gets or sets the NDC.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;value&gt;</span><span style="color: green">The NDC.</span><span style="color: gray">&lt;/value&gt;
</span><span style="color: blue">public string </span>NDC { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

<span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Gets or sets a value indicating whether this instance is generic.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;value&gt;
///     &lt;c&gt;</span><span style="color: green">true</span><span style="color: gray">&lt;/c&gt; </span><span style="color: green">if this instance is generic; otherwise, </span><span style="color: gray">&lt;c&gt;</span><span style="color: green">false</span><span style="color: gray">&lt;/c&gt;</span><span style="color: green">.
</span><span style="color: gray">/// &lt;/value&gt;
</span><span style="color: blue">public bool </span>IsGeneric { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>&#160;</p>
<h5>Documenting a Method</h5>
<blockquote>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Loads the drug.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;param name=&quot;id&quot;&gt;</span><span style="color: green">The id.</span><span style="color: gray">&lt;/param&gt;
</span><span style="color: blue">private void </span>LoadDrug(<span style="color: blue">int </span>id)
{
    <span style="color: green">// do nothing... just an example
</span>}</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>&#160;</p>
<h5>Documenting an Inherited/Implemented Method</h5>
<blockquote>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Creates a new object that is a copy of the current instance.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;returns&gt;
/// </span><span style="color: green">A new object that is a copy of this instance.
</span><span style="color: gray">/// &lt;/returns&gt;
</span><span style="color: blue">public object </span>Clone()
{
    <span style="color: blue">return </span>MemberwiseClone();
}</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>&#160;</p>
<h5>Documenting an Overridden Method</h5>
<blockquote>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Returns a </span><span style="color: gray">&lt;see cref=&quot;T:System.String&quot;/&gt; </span><span style="color: green">that represents the current </span><span style="color: gray">&lt;see cref=&quot;T:System.Object&quot;/&gt;</span><span style="color: green">.
</span><span style="color: gray">/// &lt;/summary&gt;
/// &lt;returns&gt;
/// </span><span style="color: green">A </span><span style="color: gray">&lt;see cref=&quot;T:System.String&quot;/&gt; </span><span style="color: green">that represents the current </span><span style="color: gray">&lt;see cref=&quot;T:System.Object&quot;/&gt;</span><span style="color: green">.
</span><span style="color: gray">/// &lt;/returns&gt;
</span><span style="color: blue">public override string </span>ToString()
{
    <span style="color: blue">return </span><span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">&quot;{0} {1}&quot;</span>, Name, DoseRoute);
}</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>&#160;</p>
<h5>Documenting an Event</h5>
</p>
</p>
</p>
</p>
<blockquote>
<pre class="code"><span style="color: gray">/// &lt;summary&gt;
/// </span><span style="color: green">Occurs when [allergy detected].
</span><span style="color: gray">/// &lt;/summary&gt;
</span><span style="color: blue">public event </span><span style="color: #2b91af">EventHandler </span>AllergyDetected;</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>As you can see, the default configuration of GhostDoc is pretty intelligent. Here are some shots of the rules configuration options provided by default:</p>
<p>&#160;</p>
<p><a href="http://www.alvinashcraft.com/wp-content/uploads/ghostdocconfig1.png"><img title="GhostDoc Rules Config" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="484" alt="GhostDoc Rules Config" src="http://www.alvinashcraft.com/wp-content/uploads/ghostdocconfig1-thumb.png" width="598" border="0" /></a> </p>
<p>&#160;</p>
<p><a href="http://www.alvinashcraft.com/wp-content/uploads/ghostdocconfig1a.png"><img title="GhostDoc Rules Config 2" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="484" alt="GhostDoc Rules Config 2" src="http://www.alvinashcraft.com/wp-content/uploads/ghostdocconfig1a-thumb.png" width="598" border="0" /></a> </p>
<p><em>(I added the shading in the second shot to mask options that were visible in the previous one.)</em></p>
<p>&#160;</p>
<p>Users have the ability to change any of these existing rules or add their own. This post is just a brief overview of the product, so I won’t go into details on how to configure a rule at this time. Let me know if you would be interested in more posts that dive into more details on the configuration of GhostDoc.</p>
<p>If you don’t already use it, I encourage you to download it and give it a try. The learning curve is not steep and the benefits are great. Plus, it’s free! One more quick note, VB support is experimental at this time, and it is turned off by default. To enable it, go to the Options tab on the config screen.</p>
</p>
</p>
</p>
<p>GhostDoc download links are <a href="http://www.roland-weigelt.de/ghostdoc/" target="_blank">here</a>.</p>
<p>My sample project (VS2008) can be downloaded <a href="http://www.alvinashcraft.com/code/GhostDocSample.zip" target="_blank">here</a>.</p>
<p>&#160;</p>
<p><div class="wlWriterSmartContent" id="scid:C16BAC14-9A3D-4c50-9394-FBFEF7A93539:e62d18fd-5122-4db3-a428-a7e167c01b8a" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><a href="http://www.dotnetkicks.com/kick/?url=http://www.alvinashcraft.com/2008/08/23/roland-weigelts-ghostdoc-for-visual-studio/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.alvinashcraft.com/2008/08/23/roland-weigelts-ghostdoc-for-visual-studio/" border="0" alt="kick it on DotNetKicks.com" /></a></div>
</p>
<div class="wlWriterSmartContent" id="scid:d7bf807d-7bb0-458a-811f-90c51817d5c2:1d523341-a33f-434d-9a12-8619e9fc9a8f" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<p><span class="TagSite">Technorati:</span> <a href="http://technorati.com/tag/visual+studio" rel="tag" class="tag">visual studio</a>, <a href="http://technorati.com/tag/add-ins" rel="tag" class="tag">add-ins</a>, <a href="http://technorati.com/tag/ghostdoc" rel="tag" class="tag">ghostdoc</a>, <a href="http://technorati.com/tag/xml+documentation" rel="tag" class="tag">xml documentation</a><br /><!-- StartInsertedTags: visual studio, add-ins, ghostdoc, xml documentation :EndInsertedTags --></p>
</div>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Roland+Weigelt%E2%80%99s+GhostDoc+for+Visual+Studio+http://7fg98.th8.us" title="Post to Twitter"><img class="nothumb" src="http://www.alvinashcraft.com/wp-content/plugins/tweet-this/icons/tt-twitter.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/home/?status=Roland+Weigelt%E2%80%99s+GhostDoc+for+Visual+Studio+http://7fg98.th8.us" title="Post to Twitter">Tweet This Post</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.alvinashcraft.com/2008/08/23/roland-weigelts-ghostdoc-for-visual-studio/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
