Question

i have the following code which has some duplication

    private List<SelectListItem> GetDeskList(int deskId)
    {
        List<Desk> apps = Model.GetDesks();

        List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
        {
            Selected = c.Id == deskId,
            Text = c.Name,
            Value = c.Id.ToString()
        }).ToList();
        dropdown.Insert(0, new SelectListItem());
        return dropdown;
    }

    private List<SelectListItem> GetRegionList(int regionId)
    {
        List<Region> apps = Model.GetRegions();

        List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
        {
            Selected = c.Id == regionId,
            Text = c.Name,
            Value = c.Id.ToString()
        }).ToList();
        dropdown.Insert(0, new SelectListItem());
        return dropdown;
    }

and a few more like it with a similar pattern. what is the best way to refactor this to avoid duplication

Was it helpful?

Solution

If you can change your models to implement a common interface (or inherit from a common base class) then you might be able to do something like this:

var desks = GetList(123, () => Model.GetDesks());

var regions = GetList(456, () => Model.GetRegions());

// ...

private List<SelectListItem> GetList<T>(int id, Func<List<T>> getApps)
    where T : IDropdownItem
{
    List<T> apps = getApps();

    List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
        {
            Selected = c.Id == id,
            Text = c.Name,
            Value = c.Id.ToString()
        }).ToList();

    dropdown.Insert(0, new SelectListItem());
    return dropdown;
}

public interface IDropdownItem
{
    int Id { get; }
    string Name { get; }
}

public class Desk : IDropdownItem { /* ... */ }

public class Region : IDropdownItem { /* ... */ }

OTHER TIPS

Just a stab in the dark, but something like this is where you should head:

private List<SelectListItem> GetList<T>(List<T> list, int Id)
{
    List<SelectListItem> dropdown = list.ConvertAll(c => new SelectListItem
    {
        Selected = c.Id == Id,
        Text = c.Name,
        Value = c.Id.ToString()
    }).ToList();
    dropdown.Insert(0, new SelectListItem());
    return dropdown;
}

and pass in your type safe lists instead of calling the methods in the GetList method

private List<SelectListItem> GetObjectList<ObjectType>(int id, Func<List<ObjectType>> getObjects)
{
    List<ObjectType> apps = getObjects();

    List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
    {
        Selected = c.Id == id,
        Text = c.Name,
        Value = c.Id.ToString()
    }).ToList();
    dropdown.Insert(0, new SelectListItem());
    return dropdown;
}

private List<SelectListItem> GetDeskList(int deskId)
{
    return GetObjectList(deskId, (() -> Model.GetDesks()));
}

private List<SelectListItem> GetRegionList(int regionId)
{
    return GetObjectList(regionId, (() -> Model.GetRegions()));
}

Maybe templatize the function on the list item type, and pass in the list?

private List<SelectListItem, ItemType> GetRegionList(int theId, List<ItemType> apps) 
{ 
     List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem 
    { 
        Selected = c.Id == theId, 
        Text = c.Name, 
        Value = c.Id.ToString() 
    }).ToList(); 
    dropdown.Insert(0, new SelectListItem()); 
    return dropdown; 
} 

List<Desk> apps = Model.GetDesks();
GetRegionList<SelectListItem,Desk>(ID, apps);

I would also strongly suggest you have a look at some of Matthew Cochran's blogs on patterns. I found them really helpful. Here is one: Visitor pattern and just look under his posts for a few of his patterns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top