Braintree如何以及为何以这种方式设置API代码?

时间:2014-02-21 02:35:55

标签: braintree

Braintree如何以及为何以这种方式设置其API代码?它们以某种方式允许方法调用被链接,然后填充到最终通过Search()方法发送的变量中。我以前从未见过这样的API(Linq除外)。他们如何在幕后设置这种东西以及是什么让这更好(或更糟)?

https://www.braintreepayments.com/docs/dotnet/transactions/search

var request = new TransactionSearchRequest().
    CreditCardCardholderName.Is("Patrick Smith").
    CreditCardExpirationDate.Is("05/2012").
    CreditCardNumber.Is("5105105105105100");

ResourceCollection<Transaction> collection = gateway.Transaction.Search(request);

1 个答案:

答案 0 :(得分:1)

如果每个方法都返回对该对象的引用,则可以根据需要将它们链接在一起。

public class Foo {
//  stuff...

    public Foo Baz() {
        // do stuff
        return this; //return a reference to the object
    }

}

我认为这个想法(就像linq一样)具有关系型面向对象的代码。

它有点像在API中定义一个遵循自己的规则的迷你语言,而不是更大的语言约定。

我更喜欢接近惯例,但这种语法在ORM的背景下并不罕见