0

What intrinsic properties of jQuery and the requirements it satisfies makes it such a good candidate for the builder pattern and method chaining?

Asked another way, could the C#/VB .NET core libraries be rewritten with much more method chaining similar to jQuery or are there some inherit, limiting factors?

Does a lot of it come down to JavaScript being dynamic vs. typed like VB/C# or something with jQuery mostly being interested in DOM manipulation?

Chad
  • 3,159
  • 4
  • 33
  • 43
  • -1 for question based on false premise. – John Saunders Jun 08 '12 at 06:01
  • @JohnSaunders: I read the question the same way you did at first. Turns out (based on his comment to my answer, and carefully rereading the question) he's asking if the core .NET libraries *could* be rewritten to make more use of method chaining, or if there is some inherent technological factor that would *theoretically* prevent that. – Eric J. Jun 08 '12 at 16:32

2 Answers2

2

You can chain methods in C# just fine. For example, that is done with Linq all of the time.

To understand method chaining implementation in C#, see

Method-Chaining in C#

This is also sometimes referred to as a fluent interface.

The fundamental idea is that each method that participates in a chain returns this, so that other methods of the class can be called by referencing the return value of the previous call.

There is nothing technical preventing the core libraries from being rewritten to make more extensive use of method chaining. Practically, though, rewriting the core libraries would break all existing .NET applications.

Certainly one could write a library that provides a fluent interface to things in the core library. One thing that springs to mind is the handling of Streams and Readers in System.IO.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Yes yes, I know it's possible. My question is why wasn't it done with the core libraries whereas it was done with jQuery? – Chad Jun 08 '12 at 15:36
  • You would probably have to ask the people that wrote the core libraries for a definitive answer. My guess is that the pattern wasn't broadly used in the industry when .NET 1.0 was being worked on in the 2000-2001 time frame, so the team did not think to implement it. Even .NET 1.0 has everything needed to implement method chaining, so it certainly wasn't any sort of technical limitation. – Eric J. Jun 08 '12 at 16:29
1

No there are several C# libraries that are written with similar "chaining". It's typically called Fluent APIs. One example is Fluent NHibernate, but there are many more as it's just a way of structuring your code. It's not a limitation of dynamic vs static languages

TGH
  • 38,769
  • 12
  • 102
  • 135