leora
leora

Reputation: 196439

How can this code be cleaned up to remove duplication?

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

Upvotes: 2

Views: 383

Answers (5)

Ryk
Ryk

Reputation: 3102

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.

Upvotes: 0

LukeH
LukeH

Reputation: 269278

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 { /* ... */ }

Upvotes: 2

AShelly
AShelly

Reputation: 35520

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);

Upvotes: 0

Mau
Mau

Reputation: 14468

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()));
}

Upvotes: 1

user177800
user177800

Reputation:

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

Upvotes: 4

Related Questions