Managing Configuration Manager Part 2 – Group and Group Collection

As I mentioned in my last post in this series, the ModuleCollection will be contained in a Group. Since I can’t have multiple Groups in one app.config file (largely for the same reason as I can’t have multiple Modules in a Group) I’m going to need to keep the Groups in a Collection.

    public class Group : ConfigurationElement
    {
        private static ConfigurationProperty sGroupName;
        private static ConfigurationProperty oModuleCollection;

        private static ConfigurationPropertyCollection oProperties;

        static Group()
        {
            sGroupName = new ConfigurationProperty(
                "name",
                typeof(string),
                null,
                ConfigurationPropertyOptions.IsRequired
                );
            oModuleCollection = new ConfigurationProperty(
                "modules",
                typeof(ModuleCollection),
                null,
                ConfigurationPropertyOptions.IsRequired
                );
            oProperties = new ConfigurationPropertyCollection();

            oProperties.Add(sGroupName);
            oProperties.Add(oModuleCollection);
        }

        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)base[sGroupName]; }
        }

        [ConfigurationProperty("modules", IsRequired = true)]
        [ConfigurationCollection(typeof(Module), AddItemName = "module")]
        public ModuleCollection Modules
        {
            get { return (ModuleCollection)base[oModuleCollection]; }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get { return oProperties; }
        }
    }

As you can see, all a Group is is just a name and a collection of Modules. But wait, why is the collection named “modules” here (hint: plural). Well, with this code, a Group can now be defined like this:

  <group name="Group 1">
    <modules>
      <module order="1" name="SomeModule" path="Project1" />
      <module order="2" name="OtherModule" path="Project2" />
    </modules>
  </group>
  <group name="Group 2">
    <modules>
      <module order="1" name="YetAnotherModule" path="Project3" />
    </modules>
  </group>

So you see, a Group has one string property, its name, and a ModuleCollection property, named “modules”, which contain an array of Modules. Alright, it’s a bit different from the original thought, but let’s keep going!

    public class GroupCollection : ConfigurationElementCollection
    {
        static GroupCollection()
        {
        }

        public GroupCollection()
        {
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return "group"; }
        }

        public Group this[int index]
        {
            get { return (Group)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }

        public new Group this[string name]
        {
            get { return (Group)base.BaseGet(name); }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new Group();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as Group).Name;
        }
    }

Incredibly similar to ModuleCollection right? It should be. It’s the same BasicMap. An AddItemName configured to be simple to use. The same array index and key accessors. So far still so simple. All that we have so far are still Elements and ElementCollections. Next, we’ll go into Section and SectionGroup.

Leave a Reply