Using Generics and Inheritance to Simplify Development
page 7 of 9
by Brian Mains
Feedback
Average Rating: 
Views (Total / Last 10 Days): 35605/ 63

Example: Custom Configuration Collections

I first read the MSDN example on custom configuration collections and saw what was involved in creating a custom collection from the example.  Maybe there is an easier way, but that is where generics and inheritance come into play.  Instead of implementing that approach every time, I created this element class:

Listing 4

namespace Nucleo.Configuration
{
  public abstract class ConfigurationElementBase:
    System.Configuration.ConfigurationElement
  {
    protected internal abstract string Key
    {
      get;
    }
  }
}

The key property is something that must be overridden and returns a unique value that identifies an element in a collection.  The key is important because the base collection for configuration collections uses a unique value.  The key property ensures this.  Because the collection knows of the Key property in the element base interface, the collection can automatically use the element without having to create a custom collection on your own.  The following class is the base collection class, which defines a generic property that references the element base class above.

Listing 5

namespace Nucleo.Configuration
{
  public abstract class ConfigurationCollectionBase < T > :
    ConfigurationElementCollection where T: ConfigurationElementBase
  {
    public override ConfigurationElementCollectionType CollectionType
    {
      get
      {
        return ConfigurationElementCollectionType.AddRemoveClearMap;
      }
    }
 
    public T this[int index]
    {
      get
      {
        return (T)this.BaseGet(index);
      }
      set
      {
        if (this.BaseGet(index) != null)
          this.BaseRemoveAt(index);
        this.BaseAdd(index, value);
      }
    }
 
    new public T this[string key]
    {
      get
      {
        return (T)this.BaseGet(key);
      }
    }
 
    public void Add(T element)
    {
      this.BaseAdd(element);
    }
 
    public void Clear()
    {
      this.BaseClear();
    }
 
    public bool Contains(string key)
    {
      return (this.BaseGet(key) != null);
    }
 
    protected override ConfigurationElement CreateNewElement()
    {
      return Activator.CreateInstance < T > ();
    }
 
    protected override object GetElementKey(ConfigurationElement element)
    {
      return ((T)element).Key;
    }
 
    public void Remove(T element)
    {
      this.Remove(element.Key);
    }
 
    public void Remove(string key)
    {
      this.BaseRemove(key);
    }
 
    public void RemoveAt(int index)
    {
      this.BaseRemoveAt(index);
    }
  }
}

Now, because the work is done in the base collections class, inheritance from this is a snap and reduces the amount of code needed.  For instance, below is a derived implementation of the element.

Listing 6

namespace Nucleo.Web.Configuration
{
  public class IndividualPageElement: ConfigurationElementBase
  {
    protected override string Key
    {
      get
      {
        return this.PageName;
      }
    }
  }
}

The collection definition is even easier.

Listing 7

public class IndividualPageElementCollection :  
ConfigurationCollectionBase<IndividualPageElement> 
{ 
}

That is all it takes to implement a collection with all that functionality and with strong typing.  This is a more useful example because it really can cut down on the total amount of coding that is required for a collection.  However, this usage is not limited to collections, but can be used in many scenarios.


View Entire Article

User Comments

Title: Using Generics and Inheritance to Simplify Development   
Name: Meena
Date: 2007-10-26 6:32:53 PM
Comment:
This article help me a lot to learn generics.

Thanks
Title: Great Article!   
Name: Mohammad Azam
Date: 2007-09-28 3:18:52 PM
Comment:
Hi Brain,

Great article as always! Keep up the good work.
Title: Using Generics and Inheritance to Simplify Development   
Name: Brain
Date: 2007-05-12 1:40:08 PM
Comment:
Outstanding article. It was very informative. Thanks dude.
- Uday.D






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 10:47:40 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search