3

In this discussion (creating API that is fluent ) , a response indicates that the Builder Pattern is better than using Extension methods for creating Fluent interfaces.

What are the reasons this could be true? Extension methodology follows the "O" in the SOLID principle... I personally only use Extension methods for developing Fluent interfaces but am wondering if I need to rethink this...

Community
  • 1
  • 1
JWP
  • 6,672
  • 3
  • 50
  • 74
  • Agreed, too opinion based. – BenjaminPaul Feb 19 '15 at 16:36
  • 1
    Here's an opinion for Brian, Servy, Bryan, Mason, Nathan... You guys are preventing legitimate research. – JWP Feb 19 '15 at 17:00
  • I didn't ask for any opinion on why it's true, I asked for the truth of the matter. Apparently you guys don't like the truth. – JWP Feb 19 '15 at 17:01
  • Gravedig, but throwing you an upvote. The opinion-based flag is thrown up very inconsistently, and it really annoys me too. I've raised perfectly legitimate 'poor quality' flags that have not been actioned, and witnessed more than a few good questions get closed. – mirichan Sep 22 '15 at 08:25

2 Answers2

5

This SO Question describes disadvantages of extension methods.

Furthermore some think, that extension methods violate the Open/Closed principle rather than following it, because they introduce features in a way that may have not been intended by the original creator.

If Builder Pattern achieves the same goals without disadvantages, then why not use it instead.

Community
  • 1
  • 1
Eiver
  • 2,594
  • 2
  • 22
  • 36
  • NONE of the disadvantages mentioned above are concerning if one follows convention over configuration. Naming a method to remove ambiguity and to define exactly (and only that) what it does is as important as naming a class. Visual Studio allows us to Go To Definition anyway. – JWP Dec 17 '15 at 16:27
2

One thing that comes to my mind is that extension methods are not thought to be used for fluent APIs but just to extend the functionality of a type.

The builder pattern requires more understanding, is related to currying, monads and functional programming.

So the latter is a better conceptual base which translates to many languages, while the former is a C# lingo that may not have a counterpart in other languages.

If you obtain the same behaviour with both methods you can use one by your free choice. But then you should also consider which features you need/want: expressiveness, readability, portability, security, performance of code are all qualities that may differ between the two choices.

pid
  • 11,472
  • 6
  • 34
  • 63