Using Custom Objects in an iKnode Application

iKnode applications have supported native types as a return and as parameters since its inception. This obviously limited the information that could be received and returned from an iKnode application, and in the end also its functionality.

One way to solve this was to serialize the input and outputs of the application using a JSON Serializer (or an XML Serializer). This worked ok, but it poluted the iKnode application with unnecessary serialize/deserialize code, making the application code more difficult to maintain.

The full purpose of iKnode is to make it extremely simple to create services in the cloud, automating things like security configuration, performance configuration and also serialization of custom objects.

We are proud to announce that iKnode now supports custom objects as input and as output. The way it works is with automatic serialization of custom objects into JSON.

Let’s try an example:

Custom Objects Test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using iKnode.Applications;

namespace Applications
{
    /// <summary>
    /// Defines the Custom Objects Test Application.
    /// </summary>
    [Application]
    public class CustomObjectsTest
    {
        /// <summary>
        /// Creates a Default User.
        /// </summary>
        /// <remarks>
        /// This method tests the return of a custom object of Type <c>Client</c>.
        /// </remakrs>
        /// <returns>Client Information.</returns>
        public Client CreateDefault()
        {
            return new Client(1, new FullName("Alex", "Espinoza"));
        }

        /// <summary>
        /// Creates the select User.
        /// </summary>
        /// <remarks>
        /// This method tests the return of a custom object of Type <c>Client</c>.
        /// And also the input of a custom object of Type <c>FullName</c>
        /// </remakrs>
        /// <returns>Client Information.</returns>
        public Client CreateClient(int id, FullName name)
        {
            return new Client(id, name);
        }
    }

    /// <summary>
    /// Defines the Full Name class.
    /// </summary>
    [Serializable]
    public class FullName
    {
        public string FirstName;
        public string LastName;

        public FullName(string firstName, string lastName)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
        }
    }

    /// <summary>
    /// Defines the Client class.
    /// </summary>
    [Serializable]
    public class Client
    {
        public int Id;
        public FullName Name;

        public Client(int id, FullName name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}

We have 3 classes in our application, one that defines the Application class CustomObjectsTest and two classes used as input and output for the application Client and FullName. As you can see, both the Client class and the FullName class have the serializable attribute. Classes used as input/output for the application need to be serializable to work.

Let’s execute the method CreateDefaultUser():

As you can see, the output is already formatted as a JSON object. If you look at the result in the JSON viewer, you’ll be able to see the full client object in a nicer way.

Now let’s try the other method, to test the FullName object as a parameter. Input the following JSON in the fullName parameter and then click execute:

1
{"FirstName":"John","LastName":"Doe"}

As you can see newly created object includes the correct full name:

Now when the iKnode Application gets call, it can be easily deserialized with the Newtonsoft JSON Library:

Deserialize the Client Object
1
Client defaultClient = JsonConvert.DeserializeObject<Client>(jsonClient);

And Serialized like this:

Serialize the FullName Object
1
string output = JsonConvert.SerializeObject(fullName);

For this to work, you would need to share the Client and FullName object. This example includes the Client and FullName object inside of the iKnode App for demostration purposes, but I recommend you have those in an assembly that can be shared between your Mobile or Web App and the iKnode App.

Let us know what you think.