Introduction
[Level T2] PocoHttp is a non-opinionated Open Source .NET library for seamless consumption of HTTP data services using a familiar IQueryable<T> interface. Version 0.1 of the library is now available on GitHub - NuGet package to follow soon.
This post explains the background and motivation for this library as well as its usage and possible future directions.
Motivation and background
ASP.NET Web API exposes full richness of the HTTP spec. As such, many new avenues have been opened for creating HTTP client-server applications. Having said that, with HTTP spec aimed to be Touring-Complete, there is a client burden involved in implementing clients capable of deep protocol coherency.ASP.NET Web API has made it easy to achieve protocol coherency as part of the HTTP pipeline - modelled as Russian Dolls (see Part 6 of the series).
With a lot of WCF services (that commonly were serving domain's value objects) soon to be moved to Web API, there is a requirement for abstracting HTTP-level aspects for seamless consumption of such services. PocoHttp is designed to provide an IQueryable<T> interface which is familiar to developers who have been working with ORMs such as Entity Framework, NHibernate, etc.
WCF Data Services are able to provide this seamless communication but they
- Can only generate AtomPub payloads
- As such no content negotiation or ability to generate plain XML or JSON (it does honour Accept header but returns OData specific format)
- Use OData query syntax which is deemed by many in the community not RESTful since it assumes non-HTTP syntax coherence in the form of query string parameters on the client
- Fully expose the data to the outside world
Minimal example
Before going much further into details, it might be useful to present a minimal usage of how to use it. Example below is from the PocoHttp samples (which hosts a minimal server too) available from GitHub.var pocoClient = new PocoClient() { BaseAddress = new Uri("http://localhost:12889/api/") }; var list = pocoClient.Context<Car>() // calling "/api/Cars" .Take(1).ToList(); // getting the first item Console.WriteLine(list[0]);
As can be seen above, pocoClient is initialised with a BaseAddress (Note the trailing slash which is mandatory for correct URI composition) and then a generic context of Car type is queried. PocoClient assumes (using the naming convention setup) that the Car should be exposed at BaseAddress + "Cars" so it makes an HTTP request and turns the result into Car objects.
PocoHttp's Grammar model
Basically PocoHttp translates IQueryable<T> queries into HTTP semantics (query string parameter in case of GET or a query criteria object in the body in case of POST) using a grammar and a set of naming convention settings - and then turns the result into .NET types using media type formatting provided by the ASP.NET Web API.Currently, two built-in grammars are provided: OData and Pagination. OData implementation does not implement full features of OData (see below). Grammar can be implemented to translate the queries into other providers such as MongoDB or other custom syntaxes.
Normally, a grammar modifies the request by adding query string parameters. While it is not recommended, a grammar can modify the request to turn it into an HTTP POST and send a payload containing the criteria. The reason this is not recommended is that using POST method for GETting resources is not REST-friendly and responses to POST results cannot be cached while caching results of calls to data services can be very useful.
OData implementation
Current implementation of OData grammar supports below features:- Where: current implementation supports direct property expression. Complex property expressions (such as Car.Make.StartsWith("M")) not yet supported
- AND, OR, greater than, lesser than, lesser than or equal, greater than or equal, not equal in WHERE expressions
- Take
- Skip
- OrderBy
- OrderByDescending
Exposing an HTTP service in ASP.NET Web API (so that it can be consumed in PocoHttp) is simple: you just need to return IQueryable<T> from your action and decorate the action with [Queryable] attribute.
Having said that, while this feature is currently available in ASP.NET Web API RC, the future of OData support on Web API is a bit unclear as the Queryable attribute has been removed in ASP.NET Web API source code and it might not ship with RTM. As far as we can find out from ASP.NET team, OData support will be implemented using OData libraries and might ship out of band after the RTM. So the team is committed to full OData support but the timeline is unclear. As for now, you can happily use Queryable attribute in ASP.NET Web API RC.
As for PocoHttp, I am committed to implement full OData query syntax in the future releases.
Using PocoHttp against existing AtomPub OData services
Nothing prevents us to consume classic OData services that return AtomPub by just adding AtomPub media type formatter to the formatters. This will allow you to take advantage of all the niceties of HttpClient while using your existing OData services.I will be having a post on this soon.
Disclaimer
As I initially said, this is a non-opinionated framework. You can expose your full domain model through an IQueryable<T> interface out to the client but this is not necessarily a good thing. Exposing bowels of your server and data to the outside world is an anti-pattern and could be a costly mistake. For example you can easily expose your database to the outside world allowing this query:pocoClient.Context<Car>() .Where(car => car.Make.EndsWith("L"))
turn into this SQL statement that can bring down the server by having to look into each record:
SELECT * FROM CAR WHERE MAKE LIKE '%L'
Also free form Linq queries enable the client to select data using criteria which involves columns that do not have indices upon.
Pagination grammar
Pagination grammar is a simple non-OData syntax which is REST-friendlier. An example of the syntax is /api/Cars?skip=100&count=20.Pagination vocabulary only supports 4 constructs:
- Skip: normally represented by skip
- Take: normally represented by count
- OrderBy: normally represented by order
- OrderByDescending: normally represented by orderDesc
All above representations can be changed by the client. A typical server implementation supporting these parameters will be
public IEnumerable<Car> Get(int skip, int count) { return _repository.Skip(skip).Take(count); }As it can be seen, server implementation only supports skip and count and ignores the OrderBy and OrderByDescending.
Overriding naming convention and routing
There are times that you might want to override default route:pocoClient.Context<Person>() .Where(p => p.Name == "Ali"); // calls /api/Persons?$filter=(Name eq 'Ali')while you might want it to go to /api/Employees.
You have to ways to achieve this:
- To decorate your entity with EntityUriAttribute. In this case [EntityUri("Employees")]
- Pass the full URI as below when creating context:
pocoClient.Context<Car>("http://server/api/Employees") .Where(car => car.Make.EndsWith("L"))
Conclusion
In this post, we have introduced PocoHttp which is an emerging open source client library for consuming HTTP data services.
PocoHttp can be used to consume ASP.NET Web API services where IQueryable<T> is returned, existing OData services or any other HTTP data service built in other platforms - provided the grammar is implemented for the query syntax.
PocoHttp can be used to consume ASP.NET Web API services where IQueryable<T> is returned, existing OData services or any other HTTP data service built in other platforms - provided the grammar is implemented for the query syntax.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.