This page illustrates how to use sensibility points in the ODataFormatter and plugin custom OData serializers/deserializers, gives a sample extends the ODataFormatter to add support of OData instance annotations.
Let’s see this sample.
CLR Model
First of all, we create the following CLR classes as our model:
If I search for documents by sending the search query, in the result, I’d like have a score for the match for each document, as the score is dependent on the in coming query, it cannot be modeled as a property on response’s document, it should be modeled as an annotation on the document. Let’s do that.
Build Edm Model
Now, we can build a pretty simple Edm Model as:
Customize OData Formatter
A custom entity serializer
This entity serializer is to add the score annotation (org.northwind.search.score) to document entries.
A custom serializer provider
This serializer provider is to inject the AnnotationEntitySerializer.
Setup configuration with customized ODataFormatter
Create the formatters with the custom serializer provider and use them in the configuration.
Controller Samples
You should add a score to result documents.
Request Samples
Add prefer header and send request.
Response Samples
Annotation is supported in newest night build, 5.6.0-beta1.
Conclusion
The sample has a custom entity type serializer, AnnotatingEntitySerializer, that adds the instance annotation to ODataEntry by overriding the CreateEntry method. It defines a custom ODataSerializerProvider to provide AnnotatingEntitySerializer instead of ODataEntityTypeSerializer. Then creates the OData formatters using this serializer provider and uses those formatters in the configuration.