Code First Azure Digital Twins — a first look

Telstra, where I am employed, is involved in a lot of Internet of Things projects, including IoT digital twins.

They recently took some core bits out of project they are working on with code first Azure Digital twins and have released it as an open source library, so I thought I would have an initial look at the project.

The library can be found at

The library is available on Github, available under an Apache 2.0 licence. The library provides a number of attributes that you can apply to a class that the library can use to serialise to Digital Twins Definition Language (DTDL).

Given a class like the following:

[DigitalTwin(Version = 1, DisplayName = "Digital Factory - Interface Model")]
public class Factory : TwinBase
    [TwinProperty] public string? FactoryId { get; set; }

The library can generate the corresponding DTDL for you, automatically generating the ID, types, names, and inferred schema:

    "@id": "dtmi:factoryexample:models:factory;1",
    "@type": "Interface",
    "@context": "dtmi:dtdl:context;2",
    "displayName": "Digital Factory - Interface Model",
    "contents": [
            "schema": "string",
            "@type": "Property",
            "name": "factoryId"

As well as generating the serialized DTDL models, given an instance of the classes it can also serialise them to the corresponding twins instance.

    "$dtId": null,
    "$etag": null,
    "$metadata": {
        "$model": "dtmi:factoryexample:models:factory;1",
        "PropertyMetadata": {}
    "factoryId": "factory1"

Chocolate factory example

I have added an example how to use the library, based on the Chocolate Factory example from the Azure sample hands on labs.

This consists of a simple model of a factory building, floor, and production line with multiple steps.

The example has not be merged yet (a pull request is pending), but is available in my fork.

The example includes:

  • Show the serialized model DTDL.
  • Show the serialised twin instance DTDL.
  • Run the Microsoft.Azure.DigitalTwins.Parser to validate the model DTDL.

It also has:

  • PowerShell scripts to create digital twins infrastructure in Azure.
  • Use the Azure.DigitalTwins library client to create digital twins models from the Chocolate Factory classes.
  • Use the client to create digital twins instances from the Chocolate Factory classes.

You can run the example to create an Azure Digital Twins resource and the sample models and instances.

If you open the Azure Digital Twins Explorer, you can see the models created:

Azure Digital Twins Explorer showing inheritance and relationships between models

And the instances:

Azure Digital Twins Explorer showing model instances

Next steps

The example currently only shows how to create the models and instances, to show the basics of the library. An end to end solution will need to wire up sensors (e.g.simulators for a test example) and some sort of monitoring display.

The library also has some rough edges and needs a bit more work, but is a good way to convert from dotnet classes to Digital Twins Definition Language, and to get familiar with the specifications.

The introduction also mentions an Azure DevOps pipeline component that can be used to create the needed models as part of a pipeline (although I’m not sure if it’s released yet).

This is just a first look at the library, but it is something I will keep my eye on and post when I have spent some more time with it.

Leave a Reply

Your email address will not be published. Required fields are marked *