Fluent interfaces and leaky abstractions

前端 未结 8 2255
攒了一身酷
攒了一身酷 2021-02-13 14:30

What is a fluent interface? I can\'t find a good definition of this, but all I get are long code examples in a language I am not very familiar with (e.g. C++).

Also, wha

8条回答
  •  无人及你
    2021-02-13 15:06

    In a fluent interface, a object's methods will return a reference to the object, so that you can chain the method calls together.

    For example, in NValidate, I did this to simplify parameter validation:

    public City GetCity(string zipCode)
    {
       zipCode.Assert("zipCode").IsNotNullOrEmpty().HasLength(5,10).Matches("\\d[5]-\\d[4]");
       // Continue processing
    }
    

    I can't speak to leaky abstractions, though.

提交回复
热议问题