如何简化这些重复的代码?

How can this repetitive code be simplified?

以下代码非常重复,但在访问哪些属性方面存在细微差别。它们在 RMs/FMs/FEs 和 PrimaryRM/PrimaryFM/PrimaryFE 之间变化,具体取决于 GroupKeyGetter 适用于哪一列。

我找不到将其转换为方法的方法,这样我就不需要拥有这么大的代码块,但必须有一种更好的方法...

allRMsColumn.GroupKeyGetter = delegate(object rowObject)
{
    var users = ((Tenant) rowObject).RMs.Trim().Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries);

    return users.FirstOrDefault(user => GlobalSettings.Users.Find(x => x.Name == user && x.Selected) != null) ?? ((Tenant)rowObject).PrimaryRM;
};

allFMsColumn.GroupKeyGetter = delegate (object rowObject)
{
    var users = ((Tenant)rowObject).FMs.Trim().Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

    return users.FirstOrDefault(user => GlobalSettings.Users.Find(x => x.Name == user && x.Selected) != null) ?? ((Tenant)rowObject).PrimaryFM;
};

allFEsColumn.GroupKeyGetter = delegate (object rowObject)
{
    var users = ((Tenant)rowObject).FEs.Trim().Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

    return users.FirstOrDefault(user => GlobalSettings.Users.Find(x => x.Name == user && x.Selected) != null) ?? ((Tenant) rowObject).PrimaryFE;
};

有什么想法吗?

您可以制作用于提取所选 User 的辅助方法,如下所示:

private string GetFirstSelectedUser(string userList) {
    var users = userList.Trim().Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
    return users.FirstOrDefault(user => GlobalSettings.Users.Find(x => x.Name == user && x.Selected) != null);
}

现在所有三个代表都可以这样编码:

allRMsColumn.GroupKeyGetter = delegate(object rowObject) {
    var renant = (Tenant) rowObject;
    return GetFirstSelectedUser(tenant.RMs) ?? tenant.PrimaryRM;
};

如果您可以随意对 Tenant 进行重组,并且可以将 FEsPrimaryFEFMsPrimaryFM 和 [=19 分组=] 使用 PrimaryRM,您可以进一步简化您的方法:

class GroupWithPrimary {
    public string Group {get;}
    public string Primary {get;}
    public GroupWithPrimary(string group, string primary) {
        Group = group;
        Primary = primary;
    }
}
private string GetFirstSelectedUser(GroupWithPrimary gp) {
    var users = gp.Group.Trim().Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
    return users.FirstOrDefault(user => GlobalSettings.Users.Find(x => x.Name == user && x.Selected) != null) ?? gp.Primary
}
allRMsColumn.GroupKeyGetter = delegate(object rowObject) {
    return GetFirstSelectedUser(((Tenant)rowObject).RM);
};
allFMsColumn.GroupKeyGetter = delegate(object rowObject) {
    return GetFirstSelectedUser(((Tenant)rowObject).FM);
};
allFEsColumn.GroupKeyGetter = delegate(object rowObject) {
    return GetFirstSelectedUser(((Tenant)rowObject).FE);
};

看起来三个变体之间的差异可以提取到函数中,如下所示:

Func<object, object> CreateGroupKeyGetter(Func<Tenant, string> func1, 
           Func<Tenant, string> func2)
{
    return rowObject=>
            {
                var users = func1(((Tenant) rowObject)).Trim().Split(new[] ", "}, StringSplitOptions.RemoveEmptyEntries);

                return users.FirstOrDefault(user => GlobalSettings.Users.Find(x => x.Name == user && x.Selected) != null) ?? func2(((Tenant)rowObject));
            }
}

allRMsColumn.GroupKeyGetter = CreateGroupKeyGetter(t=>t.Rms, t=>t.PrimaryRm);
allFMsColumn.GroupKeyGetter = CreateGroupKeyGetter(t=>t.Fms, t=>t.PrimaryFm);
allFesColumn.GroupKeyGetter = CreateGroupKeyGetter(t=>t.Fes, t=>t.PrimaryFe);



}