如何清理这段代码以消除重复?

发布于 2024-09-07 13:39:14 字数 946 浏览 6 评论 0原文

我有以下代码,其中有一些重复

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

,还有一些类似的代码具有类似的模式。重构以避免重复的最佳方法是什么

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海螺姑娘 2024-09-14 13:39:14

只是在黑暗中刺探,但您应该这样做:

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

并传入类型安全列表,而不是调用 GetList 方法中的方法

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

假面具 2024-09-14 13:39:14

如果您可以更改模型以实现公共接口(或从公共基类继承),那么您可能可以执行以下操作:

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

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 { /* ... */ }
一笔一画续写前缘 2024-09-14 13:39:14
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()));
}
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()));
}
远山浅 2024-09-14 13:39:14

也许将列表项类型上的函数模板化,然后传入列表?

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

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);
忆伤 2024-09-14 13:39:14

我还强烈建议您查看 Matthew Cochran 的一些关于模式的博客。我发现它们真的很有帮助。这是一个:
访客模式,只需在他的帖子下查看一些他的模式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文