如何简化重复代码?

时间:2016-01-26 14:52:20

标签: c# .net delegates objectlistview

以下代码非常重复,但在访问的属性方面包含非常小的差异。它们在RM / FM / FE和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;
};

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以创建帮助方法来提取选定的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进行重组,并且可以将FEsPrimaryFEFMsPrimaryFMRMs分组。使用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);
};

答案 1 :(得分:0)

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

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



}