Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning child elements in ASP.NET WebAPI OData

I'm using the latest ASP.Net WebAPI Nightly builds (dated 2013-01-16).

I have a simple EF database first model at the moment that has two entities - Patients and Visits. Each patient can have many visits.

I'd like to be able to query for my list of patients and have the visits entities for each patient returned inline. I know that WebAPI's OData implementation doesn't yet support $expand. I'm hoping that just means that optional client-controlled expansion is not supported and that I can force expansion server-side.

At the moment I'm not getting any of the visits inline.

For example, my PatientController's() Get() method looks like

[Queryable(AllowedQueryOptions=AllowedQueryOptions.Supported)]
public override IQueryable<Patient> Get()
{
    var query = this.entities.Patients.Include("Visits");
    return query;  
}

I've verified that the query executing against my database does indeed include the visit information.

To use a publicly available OData service as an example, if you use the service at http://services.odata.org/OData/OData.svc/, you can get a list of Suppliers. This is http://http://services.odata.org/OData/OData.svc/Suppliers. You can also ask for a list of suppliers that includes the list of products using http://http://services.odata.org/OData/OData.svc/Suppliers?$expand=Products

Stepping through the ASP.NET code (via the symbols server) I've got to the System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer and can see that it's CreatePropertyBag method, which builds up the list of properties to be serialized, just doesn't include the navigation properties, and they don't seem to be enumerated anywhere else apart from being written out as NavigationLinks.

I'm quite new to the ASP.NET world in general and have spent a week or so getting my head around the way things work (particularly with the changes made to OData at the end of 2012 and further changes made so far in 2013).

I suspect that if the ODataEntityTypeSerializer was to be modified (I'm happy to try) to embed this extra information in the appropriate spot (within each navigation link as an nested inline feed as best I can tell) then I'd be set.

Questions:

  1. Have I overlooked something obvious and there's a flag I can set to turn on this behaviour? I can see why, if such a flag exists, it would be off by default (EF lazy loading and this flag wouldn't get on well)

  2. If #1 is no, is there some other ODataEntityTypeSerializer that I could use? If so, how do I switch to it?

  3. If #2 is no, any pointers for where I should start writing my own? Is there a place I can substitute in my own serializer or do I have to maintain my own fork of ASP.NET's Extensions project (as opposed to the Runtime project)

Thanks very much!

like image 687
Ian Yates Avatar asked Oct 06 '22 08:10

Ian Yates


2 Answers

$expand is very high on our list of things to support for OData. But as far as I know, we don't have any flag to turn it on server-side. The formatter doesn't currently allow you to substitute your own serializers either. So I'm afraid your only option in the meantime is to create a fork and add support for $expand. If you manage to get it working, please consider sending a pull request our way:

http://aspnetwebstack.codeplex.com/SourceControl/network

like image 189
Youssef Moussaoui Avatar answered Oct 10 '22 02:10

Youssef Moussaoui


You can try it already in webapi nightly builds.

Here is how to install it with nuget: http://aspnetwebstack.codeplex.com/wikipage?title=Use%20Nightly%20Builds

like image 29
Jan Blaha Avatar answered Oct 10 '22 03:10

Jan Blaha