Adding Support for POST, PUT and DELETE to Linq2Rest

Tags: Linq2Rest HTTP LINQ

In version 2.5.0.0 Linq2Rest includes support for all the HTTP verbs (well not all of them, since HEAD and CONNECT are not expected to return a response body). This has been a frequent user request. To be honest I was surprised when it was first requested because I had seen Linq2Rest as a query library, as in Language Integrated Query for REST. Even though the RestContext is constructed with an IRestClient which can fetch results, there was nothing to prevent the user from posting to the server using other means. But when you start to think about it, then it does make some sense to support more than just the GET verb.

If one considers the query to define a resource, e.g. all the orders from this year worth over 1000 money, then it should be possible to invoke actions on that resource as with any other resource. It is similar to creating a ForEach(Action) extention on an IEnumerable, which also divides opinions.

The tricky thing is that PUT, POST and DELETE are not safe actions, and therefore performing the query can only be done by invoking side effects. This is not a good thing when performing queries. For idempotent actions this may not be such a big issue in practice, but when it comes to POST actions there is a problem, since POST requests are not required to be idempotent. This makes it effectively impossible to repeat the query in a reliable way. But then again, if you are aware of the consequences, then go ahead and use it.

So how does one use it? The library includes new extension methods for PUT, POST and DELETE. GET is handled as a query in the same way as always. From the RestContext create the query, and include one of the extension methods to change the type of request being sent to the REST service. Like so:

C#

var result = _provider.Query
.Where(x => x.Value <= 3)
.Post(new SimpleDto { ID="1" })
.Count(x=> x.ID != 0);

Notice the use of the Post extension method on the queryable. For PUT and DELETE operations, you would use the Put and Delete extension methods, respectively. The extension method makes an internal change to the query provider to ensure that the passed argument (for POST and PUT requests) is posted to the service in order to generate the response.

When it comes to POST operations, then they are generally expected to return a response body. The same is generally true for PUT operations, but it may not be the case for DELETE operations. However the API is designed to expect a response stream. I am not sure how this plays out in real life scenarios and how big a problem it is. So please do send your feedback so that the API can be changed appropriately.

Currently the library is in alpha version, so the above may change, depending on the feedback I get.

You can install Linq2Rest from Nuget.org or get the source code from BitBucket.

2 Comments

  • Yannic Methot said

    Do you have a sample for the 'Delete' operation? When I call the 'Delete' I was able to have a break point in my controller, but the parameter is always null.

    My call looks like that :

    restContext.Query.Where(x => x.id == 1).Delete().Count();

    It fails on the 'Count' because the results set is null.

    Any help would be appreciated

    thanks

  • jjrdk said

    Sorry about the late reply. In order to count, there needs to be a response. Can you confirm that there is a response that can be counted?

Comments have been disabled for this content.

Latest Tweets