Generated Model Objects
What code generation produces from your event definitions, how to extend it safely, and when it runs.
Table of Contents
Regeneration and Source Control
When Generation Runs
Generation is a Toolkit Generate step, not an MSBuild step. The extension contributes two commands:
| Command | Runs when | Does |
|---|---|---|
--GenerateStoryboardClasses |
modelGeneratorEnabled is true |
Writes the model objects |
--AddStoryboardSDK |
Always | Adds the Storyboard SDK and native SBIO wrapper to the project |
Only the first is gated on modelGeneratorEnabled. The SDK step always runs, because the engine
and the exported application are needed to build and run either way.
Because this is a Generate step, editing custom.evt does not regenerate on build. Re-run
Generate after changing an event, or the generated classes stay stale and your code compiles
against the old layout.
What Gets Written
One file per language, into generatorOutputFolder:
| Application type | Files |
|---|---|
Dotnet |
GeneratedStoryboardObjects.cs |
Cpp |
GeneratedStoryboardObjects.h, GeneratedStoryboardObjects.cpp |
Every launch configuration's events/custom.evt is merged first, so one file covers every screen.
That merge is why event names must be unique across projects.
The file contains three things:
- One
partial classper event - A
StoryboardMetadataclass holding the field layout for every event - A factory that turns a received message into the right model object
The Event Class
For this event:
<event format="2u1 value 1u1 increase" name="change_brightness" type="0x20"/>
generation writes:
public partial class ChangeBrightness : DemoViewModelBase
{
public const string Name = "change_brightness";
public const string Format = "2u1 value 1u1 increase";
public const StoryboardMessageDirection Direction = StoryboardMessageDirection.In;
#region Auto Generated Properties
public UInt16 Value
{
get { return OnGetValue<UInt16>(); }
set { OnSetValue(value); }
}
public Byte Increase
{
get { return OnGetValue<Byte>(); }
set { OnSetValue(value); }
}
#endregion
#region Auto Generated Methods
public override Dictionary<string, StoryboardPropertyInfo> GetMetadata()
=> StoryboardMetadata.EventMetadata.GetMetadata(Name);
public ChangeBrightness() : base(Name, Format, Direction) { }
public ChangeBrightness(StoryboardMessage message) : base(message) { }
#endregion
}
| Member | Purpose |
|---|---|
Name |
The event name. Useful for switch on a received message. |
Format |
The format string, for reference. |
Direction |
Whether this event is sent or received. |
| Properties | One per field, reading and writing the message buffer |
| Parameterless constructor | Allocates a buffer sized for the event — use this to send |
| Message constructor | Wraps a received message — used by the factory |
Note the class is partial. That is the extension point.
The Metadata Table
Alongside the classes, generation emits the field layout for every event:
metadata.Add(ViewNetwork.Name, new Dictionary<string, StoryboardPropertyInfo>()
{
{nameof(ViewNetwork.SignalLevel), new(1, SBFieldType.SignedInt32_4s, 0, 4, 1, nameof(ViewNetwork.SignalLevel), 2224516004)},
{nameof(ViewNetwork.SavedProfile), new(2, SBFieldType.Int8_1u, 4, 1, 1, nameof(ViewNetwork.SavedProfile), 82972086)},
{nameof(ViewNetwork.Ssid), new(3, SBFieldType.String_1s, 5, 1, 0, nameof(ViewNetwork.Ssid), 1635682825)},
});
Each entry carries the field's ordinal, type, start byte, element size, count, name, and a stable unique id. The property getters and setters do their work through this table rather than through reflection, which keeps the generated code AOT- and trimmer-safe.
The unique id is also the field's system parameter id when a model publishes itself for telemetry — see View Models & Property Binding.
The same file defines the factory:
public StoryboardViewModelBase CreateObject(StoryboardMessage data)
{
switch (data.Name)
{
case BrightnessUpdate.Name: return new BrightnessUpdate(data);
case ChangeBrightness.Name: return new ChangeBrightness(data);
// ...
}
}
This is what message.GetObject<T>() calls. Pass StoryboardMetadata.EventMetadata to the
StoryboardClient constructor to make it available.
Note
An event with no fields still generates a class and an empty metadata entry. ToggleAc in the
thermostat demo is one — the event's arrival is the information.
The Base Class
Every generated class derives from generatorModelBaseClass. That class is yours, and it must
supply the two constructors the generated code calls:
public abstract class DemoViewModelBase : StoryboardViewModelBase
{
public static StoryboardClient StorybardClient { get; set; }
public DemoViewModelBase(string name, string format, StoryboardMessageDirection direction)
: base(name, format, direction) { }
public DemoViewModelBase(StoryboardMessage message) : base(message) { }
public void UpdateStoryboard()
{
OnUpdateStoryboard();
StorybardClient?.SendEvent(Message);
}
public virtual void OnUpdateStoryboard() { }
public virtual void OnUpdateModel(string target) { }
}
This is where cross-cutting model behavior belongs — a shared client reference, a send helper, and the hooks each event overrides. Every generated class inherits it, so adding a method here adds it everywhere without touching generated code.
Extending a Generated Class
Declare your own partial half in a separate file. The demos keep these in a
StoryboardViewModels/ folder:
// StoryboardViewModels/ChangeBrightness.cs
public partial class ChangeBrightness
{
public override void OnUpdateModel(string target)
{
var newBrightness = Increase == 1
? Math.Min(Model.Percentage + (short)Value, 100)
: Math.Max(Model.Percentage - (short)Value, 5);
Model.SetBrightness(newBrightness);
}
}
The most important use of this is routing on a multi-display application. The generated constructor
leaves ChannelId at zero, so stamp it in an OnInit override:
public partial class DashboardUpdate
{
protected override void OnInit()
{
Message.ChannelId = MultiScreenChannels.DashboardChannel;
}
}
OnInit runs once the message buffer exists, for both constructors, which makes it the right place
for anything that must hold whether the object was created to send or built from a received
message.
Warning
Never edit GeneratedStoryboardObjects.cs. The next Generate overwrites it without warning.
Regeneration and Source Control
The generated file is build output derived from custom.evt. Whether to commit it is a project
decision:
| Commit it | Ignore it | |
|---|---|---|
| For | Builds without running Generate; diffs show event changes explicitly | No merge conflicts on a regenerated file |
| Against | Regeneration churn in every pull request | Every clone must run Generate before it compiles |
The demos in this repository commit it, so that a demo extracted from the package builds immediately. A larger application with several developers editing events usually does better ignoring it.
Either way, treat it as generated: no edits, and regenerate after every change to an event definition.
Next: Client Lifecycle.