gpt4 book ai didi

c# - 在流畅的界面中实现条件

转载 作者:太空狗 更新时间:2023-10-29 23:35:39 25 4
gpt4 key购买 nike

我一直在尝试为我的系统中的一组规则实现流畅的界面。我想要完成的是这个

TicketRules
.RequireValidation()
.When(quartType => quartType == QuartType.Before).TotalMilageIs(64)
.When(quartType => quartType == QuartType.After).TotalMilageIs(128);

但是,我在实现 When 条件时遇到了问题。目前,我需要像这段代码一样调用 When() 两次:

rules.When(param => param.Remarque == "Test").TotalMilageIs(100);
rules.When(param => param.Remarque == "Other").TotalMilageIs(50);

var params1 = new AddTicketParameters() { Remarque = "Test" };
var params2 = new AddTicketParameters() { Remarque = "Other" };

rules.ExecuteWith(params1);

Assert.That(ticket.TotalMilage, Is.EqualTo(100));

rules.ExecuteWith(params2);

Assert.That(ticket.TotalMilage, Is.EqualTo(50));

我的 TicketRules 类看起来是这样的:

[EditorBrowsable(EditorBrowsableState.Never)]
public class TicketRules : ITicketRule, IHideObjectMembers
{
private Ticket theTicket;

public Ticket Ticket
{
set
{
theTicket = value;
}
}

private List<ITicketRule> allRules = new List<ITicketRule>();

public TicketRules()
{
}

public TicketRules(Ticket ticket)
{
theTicket = ticket;
}

public void Execute()
{
ExecuteWith(null, null);
}

public void ExecuteWith(AddTicketParameters param)
{
ExecuteWith(param, null);
}

public virtual void ExecuteWith(AddTicketParameters param, Ticket outsideTicket)
{
foreach (ITicketRule rule in allRules)
{
rule.ExecuteWith(param, theTicket ?? outsideTicket);
}
}

public TicketRules RequireValidation()
{
CreateModifierRule(ticket => ticket.NeedValidation = true);
return this;
}

public TicketRules TotalMilageIs(int milage)
{
CreateModifierRule(ticket => ticket.TotalMilage = milage);
return this;
}

private void CreateModifierRule(Action<Ticket> function)
{
AddRule(new ModifierTicketRule(function));
}

internal void AddRule(ITicketRule rule)
{
allRules.Add(rule);
}

public WhenClauseTicketRule When(Predicate<AddTicketParameters> predicate)
{
WhenClauseTicketRule whenClause = new WhenClauseTicketRule();
whenClause.Predicate = predicate;

AddRule(whenClause);

return whenClause;
}

public TicketRules UseStandardFormulaForTotalMilageAndTime()
{
AddRule(new StandardFormulaTicketRule());
return this;
}

public TicketRules EnsureMinimumMilageIs(int milage)
{
AddRule(new EnsureMinimumMilageTicketRule(milage));
return this;
}
}

ITicketRules

internal interface ITicketRule : IHideObjectMembers
{
void ExecuteWith(AddTicketParameters param, Ticket ticket);
}

我还需要在 When 子句中支持 AddTicketParameters 的子类(虽然我可能在该部分使用泛型)。我在这里发帖是因为我对我的设计和 Martin Fowler articles 很困惑更让我困惑。

最佳答案

method chaining 时,这被称为整理问题。试试这个

TicketRules
.RequireValidation()
.When(quartType => quartType == QuartType.Before,
rule => rule.TotalMilageIs(64))
.When(quartType => quartType == QuartType.After,
rule => rule.TotalMilageIs(128));

起初看起来有点奇怪,但它将您的条件包装到不同的范围内,因此您可以有条件地执行它们。把它想象成创建你自己的 if block 。通过关闭它,您知道何时可以“完成”子语句。

关于c# - 在流畅的界面中实现条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1189979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com