简体   繁体   English

我该如何减少这些方法的重复

[英]how can i reduce the duplication in these methods

Consider the following method: 请考虑以下方法:

    private bool DescriptionValid(Membership membership, string identifier)
    {
    // search for identifier in 4 lists
    // only need to find it failing in one

    if (membership.premium.Where(ev => ev.Id == identifier).Any())
    {
        var ev = membership.premium.Where(x => x.Id == identifier).Select(m => m).SingleOrDefault();
        if (String.IsNullOrEmpty(ev.Remarks))
        {
            return false;
        }
    }

    if (membership.club.Where(ev => ev.Id == identifier).Any())
    {
        var ev = membership.club.Where(x => x.Id == identifier).Select(m => m).SingleOrDefault();
        if (String.IsNullOrEmpty(ev.Remarks))
        {
            return false;
        }
    }

    if (membership.basic.Where(ev => ev.Id == identifier).Any())
    {
        var ev = membership.basic.Where(x => x.Id == identifier).Select(m => m).SingleOrDefault();
        if (String.IsNullOrEmpty(ev.Remarks))
        {
            return false;
        }
    }

    if (membership.junior.Where(ev => ev.Id == identifier).Any())
    {
        var ev = membership.junior.Where(x => x.Id == identifier).Select(m => m).SingleOrDefault();
        if (String.IsNullOrEmpty(ev.Remarks))
        {
            return false;
        }
    }

    // no fails
    return true;
}

Membership is a property which contains four lists: 会员资格是一个包含四个列表的属性:

public IList<PremiumMemberShip> premium { get; set; } = new List<PremiumMemberShip>();

public IList<ClubMemberShip> club { get; set; } = new List<ClubMemberShip>();

public IList<BasicMemberShip> basic { get; set; } = new List<BasicMemberShip>();

public IList<JuniorMemberShip> junior { get; set; } = new List<JuniorMemberShip>();

Each membership is different, but they share similar traits. 每个成员资格都不同,但它们具有相似的特征。 Is there a way I can condense the code below? 有没有办法可以压缩下面的代码? Essentially the same property is being tested each time, it's just that the type is changing for the different if s. 基本上每次都测试相同的属性,只是类型正在改变不同的if

It feels like there should be a better way to do this. 感觉应该有更好的方法来做到这一点。

Without changing any of the structure of you code you could always just do this: 在不改变任何代码结构的情况下,您可以随时执行此操作:

private bool DescriptionValid(Membership membership, string identifier)
{
    var query =
        from ms in new []
        {
            membership.premium.Select(m => new { m.Id, m.Remarks }),
            membership.club.Select(m => new { m.Id, m.Remarks }),
            membership.basic.Select(m => new { m.Id, m.Remarks }),
            membership.junior.Select(m => new { m.Id, m.Remarks }),
        }
        let ev = ms.Where(x => x.Id == identifier).SingleOrDefault()
        where ev != null && String.IsNullOrEmpty(ev.Remarks)
        select ev;

    return !query.Any();
}

In general you can create common code by keeping the commonalities and abstracting from the differences. 通常,您可以通过保持共性和从差异中抽象来创建公共代码。 Create a helper method: 创建一个帮助方法:

  1. The source collection ( membership.premium ) becomes a method argument. 源集合( membership.premium )成为方法参数。
  2. The return false; return false; becomes a return type of bool . 成为bool的回归类型。
  3. The classes ( PremiumMemberShip , ...) must expose a common API surface to the helper. 类( PremiumMemberShip ,...)必须向帮助程序公开一个公共API表面。 This can be done in various ways: 这可以通过各种方式完成:
    1. An interface with Id and Remarks members. 具有IdRemarks成员的接口。
    2. A common base class. 一个共同的基类。
    3. dynamic . dynamic
    4. The helper method could be generic and take functions Func<T, int> getID, Func<T, string> getRemarks . 辅助方法可以是通用的,并使用函数Func<T, int> getID, Func<T, string> getRemarks

Otherwise, the helper method contains a copy of the existing code with fairly mechanical changes. 否则,辅助方法包含具有相当机械更改的现有代码的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM