[英]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:
创建一个帮助方法:
membership.premium
) becomes a method argument. membership.premium
)成为方法参数。 return false;
return false;
becomes a return type of bool
. bool
的回归类型。 PremiumMemberShip
, ...) must expose a common API surface to the helper. PremiumMemberShip
,...)必须向帮助程序公开一个公共API表面。 This can be done in various ways: Id
and Remarks
members. Id
和Remarks
成员的接口。 dynamic
. dynamic
。 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.