Create your own Dynamic Object in C#

Chia Li Yun
Javarevisited
Published in
2 min readNov 1, 2021
Photo by Gabriel Crismariu on Unsplash

If you are using a NoSQL database, you might want to create a class that can hold extensible properties in addition to the standard properties to leverage on the dynamic schema capability in NoSQL.

You may rely on Json Serialiser, particularly, the System.Text.Json.

What is System.Text.Json?

System.Text.Json is a default Json serialiser by Microsoft. It is about x2 faster than Newtonsoft.

In .NET 5, Microsoft has made significant improvement in terms of performance as compared to its initial version in .NET Core 3.1.

The Approach

We will be relying on JsonExtensionData attribute from System.Text.Json.Serialization namespace, that will be assigned to a Dictionary of string to object/JsonElement. This dictionary will be used to hold the additional properties that are not declared explicitly as a property of the class. Just like any other Json properties, this property requires a public modifier.

If you do not need to instantiate an expandable object manually yourself, you do not need instantiate the dictionary object in the constructor. In other words, if you are only using Json serialiser to construct the expandable objects in your entire application, the instantiation of dictionary object would be handled during the serialisation/deserialisation operation.

An indexer method is declared to allow easy access of the additional properties stored within the dictionary.

Base ExpandableObject class

This expandable object class would only handle the additional properties and has no knowledge of other existing properties declared in the class. This will eliminate the need to use reflection to access existing properties.

Usage

For example, you want to have a dynamic animal class.

Example of derived class that can have expandable properties

It will be able to accept the following JSON payload and parse the values to respective properties accordingly.

// Duck type{
"Name": "Mallory",
"Gender": 1, // 1 is male, 0 is female
"DateOfBirth": "2018-09-19T12:00",
"IsMammal": true,
"BeakColor": "Red" // additional property can be found in dictionary
}
// Dog type{
"Name": "Groot",
"Gender": 1, // 1 is male, 0 is female
"DateOfBirth": "2018-09-19T12:00",
"IsMammal": true,
"EyeColor": "Brown" // additional property can be found in dictionary
}

Should I use Object or JsonElement as the type of the value of expandable dictionary?

Object is generally recommended as object is the base class type and can be instantiated in your codebase. This gives you the capability to add more key/value pair to the expandable dictionary easily.

JsonElement is an object that can only be instantiated within the System.Text.Json package. Hence, it will be more troublesome if you need to add more values to the dictionary.

Apart from that, JsonElement has GetRawText method that gives you the orginal string value that was parsed.

Hope you find this article helpful. Thank you for reading and stay tuned for more C# related articles!

--

--

Chia Li Yun
Javarevisited

Recent graduate from university. Always excited about the new technologies and love to share with the tech community here!