JayData.org

Generate OData client environment dynamically and forget JaySvcUtil.exe

Author: Peter Aron Zentai September 17th, 2012

, , ,


Supported in: JayData Community, JayData Pro, JayStorm

The JayData client environment is built up from Entity and EntityContext classes that describe metadata and also behavior. Until now such an environment could be generated from an OData metadata endpoint using JaySvcUtil.exe. While this approach is still viable, there is a new way: instead of generating this environment at development time, you can have the metadata downloaded at runtime and the client classes created on the fly. This allows for a number of scenarios that are impossible to solve with context classes generated at development time.

  • An OData endpoint with frequent schema changes with a client code, that displays data based on its ever changing metadata: this is very much the case with a solution that uses SharePoint lists as OData data sources. SharePoint let’s users to add or remove columns from lists thus rendering any kind of pregenerated client schemas useless.
  • Sometimes the endpoint is not known at development time: such a scenario could be a generic OData client, that uses solely the metadata provided by the endpoint to define its behavior and working.
  • At development time it’s nice to have a client that is always up to date, so changing the Entity Framework metadata (EDMX) or the CodeFirst classes does it all, and you don’t need to update the client separately.

To following example shows how to use the dynamic context creation feature:

The result:

CropperCapture[51]

The key part here is the $data.service  method that takes an OData service URL as the first parameter, and a function as second that will receive the context factory method and the context type as arguments.

Use the context factory parameter to create a context instance. This context instance is equivalent to that you had created with the help of JaySvcUtil.exe – only that it exists solely in the memory.

The context type parameter provides you with the OData endpoint’s metadata transformed into JayData class definitions that you can reflect through and build 100% domain driven, automatically scaffolding data apps. While the corresponding blog post on JayData  type schema is on its way, logging the contextType parameter to the console lets you browse through it’s structure.

Type information is available through the context instance too, making it convenient to create an instance of a not yet known type. Use the elementType property of any EntitySet instance to access it’s item constructor.

The result:

CropperCapture[52]

Dynamic context creation is a cool agile approach, but also it has some limitations:

  • You will not get intellisense hints from such context (as metadata is not present in the IDE at the time you create the code)
  • Because of the $metadata being XML content you need to have same origin access to it, or CORS configured properly, as there is no JSONP transfer for XML.

, , ,