Create a dynamic object

Print Friendly, PDF & Email

◷ Reading Time: 4 minutes

Dynamic Object

There are some scenarios from which you may build a dynamic structure using your rules. In order to do that, you need to:

  1. Use native support for complex object hierarchy
  2. Use new function to create an instance
  3. Use Var to build up your structure

In the example below, we used Procedure logic, but the concept is the same and you can use any other type of logic when required.

Native

Using an expression for array and dictionary allows the building of complex values (objects), as shown below:

In the expression above, using { and } defines a set of key/value. And [ and ] defines collection of objects and values.

For more details, check Native complex value.

Key

FlexRule’s (version 6+) dynamic object automatically creates a nested structure, as new properties are assigned using the |key.

The sample model below creates a complex object using |key monad and assigns it to the obj parameter.

<Procedure name="DynamicObject" enabled="true">
  <Declaration>
     <Define name="obj" direction="out" />
  </Declaration>
  <Var value="obj = new() |key(
       Name: 'John',
       Age: 39,
       Father.Name: 'Sam'
)" />
</Procedure>

Line 8 creates a property named Father and a nested property called Name which has a value of Sam. This way, you don’t need to create and manage nested properties manually.

JSON

In this approach, |asJson monad is used to create a complex dynamic object using JSON representation. Let’s assume we have the JSON below:

{ 
    Name: 'John',
    Age: 39,
    Father: { Name: 'Sam' }
}

And the following model uses the |asJson monad to create a complex object and assign it to the obj parameter.

<Procedure name="DynamicObject" enabled="true">
  <Declaration>
     <Define name="obj" direction="out" />
  </Declaration>
  <Var value="obj = "{ Name: 'John',
                        Age: 39,
                        Father: {Name: 'Sam'}
                       }" 
                  |asJson()
             " />
</Procedure>

Using Hashtable

Hashtable can be used to create your hierarchical dynamic structure

<Procedure name="DynamicObject" enabled="true">
  <Declaration>
     <Using path="System.Collections.Hashtable" name="Dic" />
     <Define name="obj" direction="out" />
  </Declaration>
  <Var value="obj = new(Dic)" />
  <Var value="obj.Add('Name', 'John')"/>
  <Var value="obj.Add('Age',39)"/>
  <Var value="obj.Add('Parent',new(Dic))"/>
  <Var value="obj['Parent'].Add('Name','My Father!')"/>
</Procedure>

Using Dictionary

Dictionary can be used to create your hierarchical dynamic structure:

<Procedure name="DynamicObject" enabled="true">
  <Declaration>
     <Using path="System.Collections.Generic.Dictionary(object,object)" name="Dic" />
     <Define name="obj" direction="out" />
  </Declaration>
  <Var value="obj = new(Dic)" />
  <Var value="obj.Add('Name', 'John')"/>
  <Var value="obj.Add('Age',39)"/>
  <Var value="obj.Add('Parent',new(Dic))"/>
  <Var value="obj['Parent'].Add('Name','My Father!')"/>
</Procedure>

ExpandoObject

When you want your application to get a dynamic object rather than a dictionary, then you can use ExpandoObject

<Procedure name="DynamicObject" enabled="true">
  <Declaration>
     <Using path="System.Dynamic.ExpandoObject" name="Dynamic" 
            assembly="System.Core.dll" />
     <Define name="obj" direction="out" />
  </Declaration>
  <Var value="obj = new(Dynamic)" />
  <Var value="obj.Name = 'John'"/>
  <Var value="obj.Father = new(Dynamic)"/>
  <Var value="obj.Father.Name = 'My Father Name!'"/>
</Procedure>

Differences

The difference between these two approaches is that the ‘ExpandoObject’ expression is more intuitive, but to assign a new member in ‘Dictionary or Hashtable Object’, the Add method should be used.

Updated on July 19, 2019

Was this article helpful?

Related Articles