1

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);
4

1 回答 1

1

如果每个方法都返回对对象的引用,您可以将任意数量的对象链接在一起。

public class Foo {
//  stuff...

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

}

我认为这个想法是(如 linq),拥有关系型的面向对象的代码。

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

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

于 2014-02-21T02:46:30.243 回答