<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>crunchlife: Removing Duplicate Items from an Abstract Generic List</title>
    <link>http://crunchlife.com/articles/2008/07/31/removing-duplicate-items-from-an-abstract-generic-list</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Removing Duplicate Items from an Abstract Generic List</title>
      <description>&lt;p&gt;&lt;img src="/files/rocking_chair.jpg" class="right" /&gt;I&amp;#8217;ve got some explaining to do. I
was hesitant in posting this code for fear that it might be too niche
to benefit anyone. It may be, but the underlying problem affects many
programmers working in the IT industry. What do you do when the company
legacy system&amp;#8217;s data model doesn&amp;#8217;t work with your fancy &lt;a href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank"&gt;ORM (Object-relational mapping)&lt;/a&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&amp;#8220;Any problem in computer science can be solved with another layer of indirection. But that usually will create another problem.&amp;#8221; - &lt;a href="http://en.wikipedia.org/wiki/David_Wheeler_(computer_scientist)" target="_blank"&gt;David Wheeler&lt;/a&gt;
&lt;/blockquote&gt;

&lt;p&gt;At my last job, the &lt;a href="http://en.wikipedia.org/wiki/Enterprise_resource_planning" target="_blank"&gt;ERP (Enterprise resource planning)&lt;/a&gt; system was something of mystery and voodoo. Only a few had sufficient knowledge to work with it and because of its arcane nature it was deemed untouchable. Within my first months as an employee I wrote a rudimentary ORM to serve as a layer between our client applications and the ERP system. Its performance was terrible. I wrote it off as &amp;#8220;experience&amp;#8221; and the department ultimately decided to ignore interfacing directly with the ERP system.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m using the &lt;a href="http://subsonicproject.com/" target="_blank"&gt;SubSonic&lt;/a&gt; ORM on a few ASP.NET projects with my current employer. SubSonic has worked great, but a
few of its database requirements have left me in the dust with yet another ERP system. The ERP&amp;#8217;s tables have no primary keys, constraints, or relationships, but rather than write a complete ORM I decided to roll my own data layer.&lt;/p&gt;

&lt;p&gt;All of the strongly-typed collections in the ERP&amp;#8217;s data layer implement the AbstractList type. AbstractList implements List&lt;ItemType&gt; where ItemType implements IUniqueIdentifier. That is a mouthful, but the key (pun intended) to removing duplicate items is to make sure they&amp;#8217;re unique.&lt;/p&gt;

&lt;p&gt;The IUniqueIdentifier interface contains only one property, UniqueIdentifier.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public interface IUniqueIdentifier
{
    string UniqueIdentifier
    {
        get;
        set;
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 

&lt;p&gt;If the ERP system contained a table called Customers a Customer type implementing IUniqueIdentifier would be created. &lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public class Customer : IUniqueIdentifier
{
    private string uniqueIdentifier;

    // etc...

    public UniqueIdentifier
    {
        get { return this.uniqueIdentifier; }
        set { this.uniqueIdentifier = value; }
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#8217;d also have a CustomerCollection class:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public class CustomerCollection : AbstractList&amp;lt;Customer, CustomerCollection&amp;gt;
{
    // etc...
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After such a long-winded introduction I can feel better about dumping the following code on anyone that has happened to read this far (kudos to you).&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public abstract class AbstractList&amp;lt;ItemType, ListType&amp;gt; :
List&amp;lt;ItemType&amp;gt; where ItemType:IUniqueIdentifier where
ListType:AbstractList&amp;lt;ItemType, ListType&amp;gt;, new()
{
    public ListType RemoveDuplicates()
    {
        Dictionary&amp;lt;string, int&amp;gt; uniqueStore = new Dictionary&amp;lt;string, int&amp;gt;();
        ListType list = new ListType();

        foreach (ItemType item in this)
        {
            if (!uniqueStore.ContainsKey(item.UniqueIdentifier))
            {
                uniqueStore.Add(item.UniqueIdentifier, 0);
                list.Add(item);
            }
        }

        return list;
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#8217;d love to hear how others have worked around legacy systems and still kept their code clean. &lt;/p&gt;</description>
      <pubDate>Thu, 31 Jul 2008 07:40:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:f418da7f-eb68-46f7-9192-6c8254e39b4f</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/07/31/removing-duplicate-items-from-an-abstract-generic-list</link>
      <category>Code Snippets</category>
      <category>Expect the Unexpected</category>
      <category>CSharp</category>
      <category>dotNET</category>
      <category>ORM</category>
      <enclosure type="image/jpeg" length="61497" url="http://crunchlife.com/files/rocking_chair.jpg"/>
      <trackback:ping>http://crunchlife.com/articles/trackback/75</trackback:ping>
    </item>
  </channel>
</rss>
