views:

96

answers:

5

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

+2  A: 

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 { /* ... */ }
LukeH
@LukeH - In your example, i can't compile your solution as i am getting errors where it can't resolve c.Id or c.Name. any idea why ?
ooo
@ooo: I just made an edit. For this to work your models would need to implement a common interface (or inherit from a common base class) with those properties.
LukeH
+4  A: 

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

fuzzy lollipop
+1  A: 
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()));
}
Mau
A: 

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);
AShelly
A: 

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.

Ryk