Using the OData Service Broker (including Serialization and Deserialization)
This tutorial focuses on using the generic OData Service Broker, which is used to expose OData services as SmartObjects. In this tutorial, you will register a service instance against an OData service object, and you will learn how data can be serialized and deserialized. The following aspects are covered:
- Registering a new Service Instance against an OData data source, with the OData Service Broker
- Creating a SmartObject from the Product OData entity
- Testing the SmartObject and returning an array of objects
- Testing the SmartObject and returning serialized objects
- Passing the serialized content through to the Deserialize method
Step 1 Tasks
- Register a new OData Service Instance. For the OData Service Url, enter
http://k2learningodata.azurewebsites.net/K2LearningService.svc - Leave the Authentication Mode as Impersonate, because for this service the credentials don’t matter.
- Change the Display Name to
ODataSample - After the new service instance has been created, briefly review the Entities that are returned.
K2learningservice2
- Launch the SmartObject Services Tester if it is not already open. Expand the ServiceObject Explorer node and locate the OData Service Object. Right-click the OData Service Object and select Register ServiceInstance.
- Copy and paste the following URL into the OData Service Url field:
http://k2learningodata.azurewebsites.net/K2LearningService.svc
then click Next. (The other settings are fine as is. You can leave the Authentication Mode as Impersonate, because for this service the credentials don’t matter.) - Change the Display Name to
ODataSample
then click Add. Click OK when you see the success dialogue. - Expand the Entities node and review the Service Objects that were discovered for the sample service. In the next step, you will generate a SmartObject from the Product entity.
K2learningservice2
Step 1 Review
In this step, you registered a new instance against an OData service. There were several entities discovered and exposed as Service Objects. In the next step, you will work with the Product entity.
In this step, you will create a new SmartObject from the Product entity.
Step 2 Tasks
- Using the Service Object Tester utility,. create a new SmartObject from the Product Entity. Name the new SmartObject
OData Product
and choose the OData category as the location to save it. - Check that the name is unique, then Publish the SmartObject.
- Expand the ODataSample Service Instance, then the Entities node if it is not already. Right-click the Product entity and select Create SmartObject.
- Name the new SmartObject
OData Product
then select the OData category for the SmartObject location. Click Check Name to confirm the name is unique and meets the K2 SmartObject naming convention. Click OK for the confirmation dialogue (that the name is unique), then click Publish SmartObject. Click OK to the dialogue indicating the SmartObject was successfully published. Exit the Publish SmartObject screen.
Step 2 Review
In this step, you created a new SmartObject from the Product entity returned by the sample OData service. (We just generated a SmartObject using the Service Tester utility in this step to save time. In the real world, you would probably create the SmartObject in one of the K2 design tools like K2 Studio or K2 Designer). In the next two steps, you will test the new SmartObject and learn how K2 can serialize and deserialize data.
The first test you will run will execute the Product List method, and return an array of Products as a list of SmartObject records.
Step 3 Tasks
- Navigate to the OData Product SmartObject, then Execute the List method. Review the results that are returned.
- Expand the SmartObject Explorer, then expand the OData category. Because you selected the OData category when you created the new SmartObject, this is where K2 saved the SmartObject. Right-click the OData Product SmartObject and select Execute SmartObject.
- Change the Method to Execute to List, then click Execute. Review the records that are returned as part of the list. (Collapse the Input Properties pane if you need more room.)
Step 3 Review
In this step, you executed the list method for the OData Product SmartObject, showing that the OData broker was able to "convert" the Product entity to a straightforward SmartObject.
Now imagine having to pass all of the classes that make up a complex object. For example, perhaps our Product entity had within it more complex entities such as Product Specifications or Manufacturing Specifications. Since these are complex types nested within a complex type, K2 may not be able to create a straightforward SmartObject representation of the Product entity, because K2 does not know how to expose a Product Specificationscomplex type as a property of a SmartObject. One way of handling this is to use Serialization to format complex objects into a single string that can later be Deserialized.
In this step, you will execute the List to Serialized Item method to see how Serialization works.
Step 4 Tasks
- Still working with the OData Product SmartObject, Execute the List to Serialized Item method. Review the results returned.
- Copy one of the strings.
- Change the Method to Execute to List to Serialized Item and Execute the method. Notice now that each record is comprised of a string that contains all of the properties making up the complex object.
- Click to highlight one of the string values for one of the records, and then press <CTRL-C> to copy the string.
- Paste the copied text data for one of the records into something like Notepad. (We will be pasting this value into the Deserialize method in the next step)
We will be using just the data value, not the heading, so you can delete the Product (Product) text, as shown below:
Step 4 Review
In this step, you executed the List to Serialized Item method. When data has been serialized, it is essentially reformatted into a string that can be persisted (stored), handled or transported across multiple data connections and consumers. In the next step, you will deserialize the data to return it to a complex object, to illustrate how data is serialized into text format and deserialized into object format.
The opposite of serialization is deserialization. When you deserialize a text value, the data is "converted" back into a complex object.
Step 5 Tasks
- Execute the Deserialize method. Use the string you copied in the previous step as the Serialized Item (String) Input Property. Review the results returned.
- In the SmartObject tester utility, change the Method to Execute to Deserialize. Click <CTRL-V> to paste the string you copied to Notepad in the preceding step, into the Serialized Item (String) Input Properties field. (If you collapsed the Input Properties pane, you will need to expand the pane once again to see the input field.)
- Execute the Deserialize method. Review the object that is returned, and notice how K2 "converted" the text string into a SmartObject with Properties.
Step 5 Review
In this step, you executed the Deserialize method to convert a serialized string back to a complex object.