Abstract structure - enables dynamic data in a statically typed environment

The purpose of this sample is to illustrate the functionality of the class AbstractStructure. It is used throughout the .NET SDK to allow users to handle dynamic data in a statically typed environment. Communication with the Qlik associative engine is performed through JSON-RPC calls. The .NET SDK provides C# wrappers that allows users to operate on the underlying JSON data through C# classes. The class AbstractStructure provides the mechanism that makes it possible to benefit from the dynamic nature of the underlying data, while still providing the advantages of working with a typed language such as C#.

We recommend that you to download and run the appropriate example before you continuing reading. All the sample code for the connection examples are available on Github.

Note: The purpose of the sample code provided by Qlik is to demonstrate capabilities and use cases of the API. They are provided on an as-is basis, without any guarantees that they will work in all system configurations and future software releases. These code samples are released under: Wikipedia: MIT license.

Accessing dynamic properties in AbstractStructure instances

The class AbstractStructure provides the two methods Get and Set that can be used to access dynamic properties. The C# classes generated for dynamic structures of the Qlik Engine JSON API will typically contain properties that use these methods as part of the getters and setters of the properties.

Example: Illustrates a class that is a wrapper for AbstractStructure, that provides a property named N with a value of type int.

       // A class containing a number.
	class A : Qlik.Engine.AbstractStructure
	{
		// Wrapper for a dynamic property named "n".
		public int N
		{
			get { return Get<int>("n"); }
			set { Set("n", value); }
		}
	}

Reinterpretation of an AbstractStructure instance

The class AbstractStructure provides functionality that makes it possible to perform type casts that would normally be considered invalid by the standard type cast mechanism of C#. An instance of AbstractStructure can be cast to any class that extends AbstractStructure without consideration of the C# class relationship of the original type and the type it is being cast to. For example, to cast GenericObjectProperties to the Properties class of a particular client visualization object. Or to cast between a MasterObjectProperties instance and the GenericObjectProperties class it represents.

Example: Reinterpret the properties of a master object to a specific visualization class.

	
	var properties = masterObject.Properties;

	// Check if the master object represents a visualization (i.e. has a property called "visualization").
	if (properties.Get<string>("visualization") != null)
	{
		Console.WriteLine("Master object seems to represent a Qlik Sense client visualization.");

		// Interpret properties as being of type VisualizationBaseProperties.
		var visualizationBaseProperties = properties.As<VisualizationBaseProperties>();

		// Check if the visualization represents a bar chart and print information accordingly.
		if (visualizationBaseProperties.Visualization == "barchart")
		{
			Console.WriteLine("Master object with id {0} is a barchart.", properties.Info.Id);
			var barchartProperties = visualizationBaseProperties.As<BarchartProperties>();
			var barGrouping = barchartProperties.BarGrouping.Grouping;
			Console.WriteLine("  BarGrouping is set to: " + barGrouping);
		}
		else
			Console.WriteLine("Master object with id {0} is not a bar chart. It is a {1}.", properties.Info.Id,
			visualizationBaseProperties.Visualization);

		// Print the title of the master object.
		Console.WriteLine("  Title is: \"{0}\"", visualizationBaseProperties.Title);
	}

Comparing the methods As and CloneAs

The method CloneAs creates a new object that is independent of the original object. The method As, on the other hand, makes it possible to reinterpret an object as a new type, but changes to any of the objects will be reflected in both interpretations.

Example: Dynamic properties in multiple interpretations of a properties instance.

    var masterObjectProperties = masterObject.Properties;
    var barchartProperties = masterObjectProperties.As<StringExpressionContainer>();
    barchartProperties.Set("myProperty", new StringExpressionContainer("=Sum(Sales)"));
    var myProperty = masterObjectProperties.Get<StringExpressionContainer>("("myProperty");
    Console.WriteLine("Expression used for sum of sales is: " + myProperty);

Example: Use of As to leverage suspended layout for master object.

	using (masterObject.SuspendedLayout)
	{
		var barchartProperties = masterObject.Properties.As<BarchartProperties>();
		barchartProperties.Legend.Dock = LegendDock.Bottom;
	}

Did this information help you?

Thanks for letting us know. Is there anything you'd like to tell us about this topic?

Can you tell us why it did not help you and how we can improve it?