Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top