jLinq - Extending A Method

It wasn't until late into the jLinq project that I had the idea that you should be able to extend your own methods onto the main library. jLinq ended up having quite a rewrite in order to make it fit that model.

jLinq is actually entirely composed to extension methods which I suppose is a form of dog-fooding your own product. If I couldn't do it with an extension method, then my framework didn't expose enough to a future developer.

jLinq allows you to extend jLinq in three separate ways.

  • Create an entirely new function in the jLinq library
  • Create a new function in a separate namespace in the jLinq library
  • Create your own library (with a copy of the existing jLinq library)

Each of these methods allow you to add one of four types of functions. This may change as time goes on to allow more methods, but for now these are what is available.

Source Commands

A source command is the first command you call when performing a jLinq query. The command .from() is a good example of a source command. Source commands return a jLinq object and any extension methods that have been provided. A source command should also set the data that is being queried

Query Commands

A query command is what sets the comparisons that are used when making a selection. Query commands are not run immediately so you are able to use operators like or() and not() to set how the query is evaluated. The criteria is not evaluated until you call a selection command which is explained more in a moment.

Action Commands

An action command affects a query or data, but doesn't actually change what is selected. A good example is the .orderBy() command. This command resorts the information for the query, but doesn't change what will ultimately be selected.

Selection Commands

A selection command can return anything, including the query being created. Normally, a selection command returns the results of the query as an array. Sometimes a selection command will return something different, like with commands like .count() or .isEmpty(), which return numbers and booleans.

More About Extensions

Namespaces

jLinq doesn't allow you to overwrite a method in the core. Instead, if you want to create a command that is the same name as an existing command, you must provide a namespace to put it into. This way, naming conflicts are minimized. You access a command the same way you normally would, except you include the namespace first jLinq.from(...).custom.startsWith(...).

Custom Libraries

Another thing you can do is create a separate instance of the jLinq library under a different name (technically, you can override jLinq at that point, but that wouldn't be very nice). If you're working with a custom library then you can override anything (granted you don't lock the library too, which will be explained in another post)

More Control

You can see that jLinq gives you complete control over how you use the library. Hopefully people begin to develop add-ins for jLinq

May 3, 2009

jLinq - Extending A Method

Post titled "jLinq - Extending A Method"