HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Creating a list of pairs after splitting the string containing all of the sets

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
aftercontainingtheallcreatingsplittinglistsetsstringpairs

Problem

I am working on asp.net/MVC and I need to read a value from my web.config file. Splitting the list into multiple keys is not an option.



I handled the problem by doing the following (which I am sure that there is a better approach)

public List departmentList = new List();
public List assignedtoList = new List();
public string[] pair = ConfigurationManager.AppSettings["DepartAssign"].Split(';');
foreach (string s in pair)
{
     departmentList.Add(s.Split(':').First()); //add the department to the list
     assignedtoList.Add(s.Split(':').Last()); //add the assignedto to the list
 }


and of course, I would be able to match the department with the assigned person by using the same index. Not really the cleanest approach but it works for now.

any way to make this code better?

Solution

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:


    


and add your section


    
    


Now, in your class:

public List departmentList;
public List 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.

Code Snippets

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; }
    }
}
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;
    }
}
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; }
    }
}
<configSections>
    <section name="DepartmentAssigneeMapping" type="DatabaseCleanup.DepartmentList, DatabaseCleanup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</configSections>
<DepartmentAssigneeMapping>
    <Department name="Depart1" assignee="PersonAssignTo1" />
    <Department name="Depart2" assignee="PersonAssignTo2" />
</DepartmentAssigneeMapping>

Context

StackExchange Code Review Q#15365, answer score: 2

Revisions (0)

No revisions yet.