What fluent interfaces have you made or seen in C# that were very valuable? What was so great about them?

后端 未结 11 1187
-上瘾入骨i
-上瘾入骨i 2021-01-31 05:42

\"Fluent interfaces\" is a fairly hot topic these days. C# 3.0 has some nice features (particularly extension methods) that help you make them.

FYI, a fluent API means

11条回答
  •  伪装坚强ぢ
    2021-01-31 06:01

    I love the fluent interface in CuttingEdge.Conditions.

    From their sample:

     // Check all preconditions:
     id.Requires("id")
        .IsNotNull()          // throws ArgumentNullException on failure 
        .IsInRange(1, 999)    // ArgumentOutOfRangeException on failure 
        .IsNotEqualTo(128);   // throws ArgumentException on failure 
     

    I've found that it is a lot easier to read, and makes me much more effective at checking my preconditions (and post conditions) in methods than when I have 50 if statements to handle the same checks.

提交回复
热议问题