You could create your own custom section in your config file.
First define what each record looks like:
public class Department : ConfigurationSection
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("assignee", IsRequired = true)]
public string Assignee
{
get { return (string)this["assignee"]; }
set { this["assignee"] = value; }
}
}
Define the a collection so the framework can work with your section
public class DepartmentCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "Department"; }
}
protected override ConfigurationPropertyCollection Properties
{
get { return new ConfigurationPropertyCollection(); }
}
public Department this[int index]
{
get { return (Department) BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}
public Department this[string name]
{
get { return (Department) BaseGet(name); }
}
public void Add(Department item)
{
base.BaseAdd(item);
}
public void Remove(Department item)
{
BaseRemove(item);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
protected override ConfigurationElement CreateNewElement()
{
return new Department();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as Department).Name;
}
}
Define what the list looks like
public class DepartmentList : ConfigurationSection
{
private static readonly ConfigurationPropertyCollection DepartmentListProperties;
private static readonly ConfigurationProperty DepartmentProperty;
static DepartmentList()
{
DepartmentProperty = new ConfigurationProperty(
"",
typeof (DepartmentCollection),
null,
ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection
);
DepartmentListProperties = new ConfigurationPropertyCollection
{
DepartmentProperty
};
}
public DepartmentCollection Departments
{
get { return (DepartmentCollection) base[DepartmentProperty]; }
set { base[DepartmentProperty] = value; }
}
protected override ConfigurationPropertyCollection Properties
{
get { return DepartmentListProperties; }
}
}
Now, in your .config file specify the new type:
<configSections>
<section name="DepartmentAssigneeMapping" type="DatabaseCleanup.DepartmentList, DatabaseCleanup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</configSections>
and add your section
<DepartmentAssigneeMapping>
<Department name="Depart1" assignee="PersonAssignTo1" />
<Department name="Depart2" assignee="PersonAssignTo2" />
</DepartmentAssigneeMapping>
Now, in your class:
public List<string> departmentList;
public List<string> assignedtoList;
var mappings = (DepartmentList) ConfigurationManager.GetSection("DepartmentAssigneeMapping");
departmentList = (
from Department department in mappings.Departments
select department.Name).ToList();
assignedtoList = (
from Department department in mappings.Departments
select department.Assignee).ToList();
Your class is much nicer, and the config is way cleaner.
This is fully tested, and the results you are looking for are replicated.
Dictionary<string, string>instead of twoLists? – svick Sep 5 '12 at 21:57