在流畅的界面中实现条件 [英] Implementing conditional in a fluent interface

查看:53
本文介绍了在流畅的界面中实现条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为系统中的一组规则实现流利的接口.我要完成的是这个

I've been trying to implement a fluent interface for a set of rules in my system. What I am trying to accomplish is this

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

但是,我在实现何时"条件时遇到了麻烦.目前,我需要像以下代码片段一样两次调用While():

However, I have trouble implementing the When conditional how I intended to be. Currently, I need to call When() twice like in this snippet:

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类看起来像这样:

My TicketRules class looks this:

[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;
    }
}

ITicket规则

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

我还需要在When子句中支持AddTicketParameters的子类(尽管我可能对该部分使用了泛型).我之所以在这里发布,是因为我对自己的设计感到困惑,而马丁·福勒(Martin Fowler)文章使我更加困惑.

I also need to support the subclasses of AddTicketParameters in the When clause (I've though maybe using generics for that part). I'm posting here because I'm all confused in my design and the Martin Fowler articles confuse me even more.

推荐答案

方法链接 试试这个

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

起初看起来有些奇怪,但是它将条件包装到另一个范围中,因此您可以有条件地执行它们.考虑一下它,就像创建自己的if块一样.通过关闭它,您知道何时可以完成"一个子语句.

It looks a little odd at first, but it wraps your conditionals into a different scope so you can conditionally execute them. Think about it like creating your own if block. By closing it, you know when you can "finish" a sub statement.

这篇关于在流畅的界面中实现条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆