如何消除重复的代码?

时间:2010-07-15 06:33:16

标签: c#

我有以下代码,我想以最少重复代码的方式编写代码。

if (Categories != null)
{
    bool flag=false;
    foreach (dynamic usableCat in Category.LoadForProject(project.ID))
    {
        foreach (dynamic catRow in Categories)
        {
            if (usableCat.ID == catRow.ID)
                flag = true;                            
        }
        if (!flag)
        {
            int id = usableCat.ID;
            Category resolution = Category.Load(id);
            resolution.Delete(Services.UserServices.User);
        }
    }
}
if (Priorities != null)
{
    bool flag = false;
    foreach (dynamic usableCat in Priority.LoadForProject(project.ID))
    {
        foreach (dynamic catRow in Priorities)
        {
            if (usableCat.ID == catRow.ID)
                flag = true;
        }
        if (!flag)
        {
            int id = usableCat.ID;
            Priority resolution = Priority.Load(id);
            resolution.Delete(Services.UserServices.User);
        }
    }
}

请注意,类别和优先级没有包含ID的公共基本类型或接口。

3 个答案:

答案 0 :(得分:7)

void DeleteUsable<Ttype>(IEnumerable<Ttype> usables, IEnumerable<Ttype> collection, Func<int, Ttype> load)
{
     bool flag = false;
     foreach (dynamic usableCat in usables)
                {
                    foreach (dynamic catRow in collection)
                    {
                        if (usableCat.ID == catRow.ID)
                            flag = true;
                    }
                    if (!flag)
                    {
                        int id = usableCat.ID;
                        Ttype resolution = load(id);
                        resolution.Delete(Services.UserServices.User);
                    }
                }
}

编辑: 叫它:

if (Categories != null) 
   DeleteUsable(Category.LoadForProject(project.ID), Categories, Categoriy.Load); 
if (Priorities != null) 
   DeleteUsables(Priority.LoadForProject(project.ID), Priorities, Priority.Load);

答案 1 :(得分:3)

让我建议一种替代方法:使用LINQ删除标志循环的 need ,而不是将flag分解出来:

if (Categories != null)
{
    foreach (var usableCat in Category.LoadForProject(project.ID))
    {
       if (!Categories.Any(row => usableCat.ID == row.ID))
            Category.Load(usableCat.ID).Delete(Services.UserServices.User);
    }
}
if (Priorities != null)
{
    foreach (var usablePri in Priority.LoadForProject(project.ID))
    {
        if (!Priorities.Any(row => usablePri.ID == row.ID))
            Priority.Load(usablePri.ID).Delete(Services.UserServices.User);
    }
}

答案 2 :(得分:2)

我建议使用这样的方法(因为您可以访问动态类型):

void DeleteUsables(dynamic usablesResource, dynamic usablesCatalog)
{
    bool flag = false;
    foreach (dynamic usableCat in usablesCatalog.LoadForProject(project.ID))
    {
        foreach (dynamic catRow in usablesResource)
        {
            if (usableCat.ID == catRow.ID)
                flag = true;                            
        }
        if (!flag)
        {
            int id = usableCat.ID;
            dynamic resolution = usablesCatalog.Load(id);
            resolution.Delete(Services.UserServices.User);
        }
    }

}

然后你会这样打电话:

if (Categories != null)
{
    DeleteUsables(Categories, Category)
}
if (Priorities != null)
{
    DeleteUsables(Priorities, Priority)
}