BasicDigitalTwin Class

Definition

An optional, helper class for deserializing a digital twin.

public class BasicDigitalTwin
type BasicDigitalTwin = class
Public Class BasicDigitalTwin
Inheritance
System.Object
BasicDigitalTwin

Examples

Here's an example of how to use the BasicDigitalTwin helper class to serialize and create a digital twin.

// Create digital twin with component payload using the BasicDigitalTwin serialization helper

var basicTwin = new BasicDigitalTwin
{
    Id = basicDtId,
    // model Id of digital twin
    Metadata = { ModelId = modelId },
    Contents =
    {
        // digital twin properties
        { "Prop1", "Value1" },
        { "Prop2", 987 },
        // component
        {
            "Component1",
            new BasicDigitalTwinComponent
            {
                // component properties
                Contents =
                {
                    { "ComponentProp1", "Component value 1" },
                    { "ComponentProp2", 123 },
                },
            }
        },
    },
};

Response<BasicDigitalTwin> createDigitalTwinResponse = await client.CreateOrReplaceDigitalTwinAsync(basicDtId, basicTwin);
Console.WriteLine($"Created digital twin '{createDigitalTwinResponse.Value.Id}'.");

Here's an example of how to use the BasicDigitalTwin helper class to get and deserialize a digital twin.

Response<BasicDigitalTwin> getBasicDtResponse = await client.GetDigitalTwinAsync<BasicDigitalTwin>(basicDtId);
BasicDigitalTwin basicDt = getBasicDtResponse.Value;

// Must cast Component1 as a JsonElement and get its raw text in order to deserialize it as a dictionary
string component1RawText = ((JsonElement)basicDt.Contents["Component1"]).GetRawText();
var component1 = JsonSerializer.Deserialize<BasicDigitalTwinComponent>(component1RawText);

Console.WriteLine($"Retrieved and deserialized digital twin {basicDt.Id}:\n\t" +
    $"ETag: {basicDt.ETag}\n\t" +
    $"ModelId: {basicDt.Metadata.ModelId}\n\t" +
    $"Prop1: {basicDt.Contents["Prop1"]} and last updated on {basicDt.Metadata.PropertyMetadata["Prop1"].LastUpdatedOn}\n\t" +
    $"Prop2: {basicDt.Contents["Prop2"]} and last updated on {basicDt.Metadata.PropertyMetadata["Prop2"].LastUpdatedOn}\n\t" +
    $"Component1.Prop1: {component1.Contents["ComponentProp1"]} and  last updated on: {component1.Metadata["ComponentProp1"].LastUpdatedOn}\n\t" +
    $"Component1.Prop2: {component1.Contents["ComponentProp2"]} and last updated on: {component1.Metadata["ComponentProp2"].LastUpdatedOn}");

Remarks

This helper class will only work with System.Text.Json. When used with the ObjectSerializer, parameter to DigitalTwinsClientOptions it will only work with the default (JsonObjectSerializer).

For more samples, see our repo samples.

Constructors

BasicDigitalTwin()

Properties

Contents

This field will contain properties and components as defined in the contents section of the DTDL definition of the twin.

ETag

A string representing a weak ETag for the entity that this request performs an operation against, as per RFC7232.

Id

The unique Id of the digital twin in a digital twins instance. This field is present on every digital twin.

Metadata

Information about the model a digital twin conforms to. This field is present on every digital twin.

Applies to