DataSmith 0.1.2
Loading...
Searching...
No Matches
Model System

DataSmith uses a code-first model system.

You define plain C# classes, and DataSmith generates runtime model containers, queries, and persistence logic.


🧠 What Is a Model?

A model describes a type of data stored and managed by DataSmith.

Models are:

  • Plain C# classes (POCOs)
  • Attribute-driven
  • Serializable
  • Engine-agnostic

DataSmith generates companion classes that provide:

  • Storage
  • Queries
  • Change tracking
  • Persistence

πŸ— Creating a Model

Mark a class with the GameModel attribute.

[GameModel(ModelValueType.List)]
public class InventoryItem
{
[PrimaryKey]
public string Id;
public string Name;
public int Value;
}
Definition GameModel.cs:8
Definition DataSmithGenerator.cs:13

Run the generator to create the runtime model classes.


πŸ“¦ Model Types

The ModelValueType determines how data is stored.

πŸ”Ή Single Model

Stores exactly one instance of the data type.

[GameModel(ModelValueType.Single)]
public class PlayerStats
{
public int Level;
public int Experience;
}

Generated model:

  • Holds a single value
  • Provides getters/setters for each field
  • Emits change events

πŸ”Ή List Model

Stores multiple items of the data type.

[GameModel(ModelValueType.List)]
public class InventoryItem
{
[PrimaryKey]
public string Id;
public string Name;
public int Value;
}

Generated model:

  • Backed by a list
  • Supports Add / Remove
  • Supports queries
  • Can enforce uniqueness via primary keys

πŸ”Ή Database Model (DB)

Uses an external database provider instead of in-memory storage.

[GameModel(ModelValueType.DB)]
public class AccountData
{
[PrimaryKey]
public int AccountId;
public string Username;
public int Coins;
}

Behavior depends on the configured database provider.


🏷 Attributes

Attributes modify how models behave.


πŸ”‘ PrimaryKey

Marks a field as the unique identifier for the model.

[PrimaryKey]
public string Id;

For list models:

  • Enforces uniqueness
  • Used for lookups
  • Required for references

πŸ”— Reference

Indicates that a field refers to another model.

Typically stores the target model’s primary key.

[Reference(typeof(InventoryItem))]
public string ItemId;

Generated code provides helper methods to retrieve the referenced object.


πŸ”„ Generated Companion Classes

For each model type, DataSmith generates:

  • <TypeName>Model
  • <TypeName>Query

Example:

InventoryItem β†’ InventoryItemModel + InventoryItemQuery

These classes handle runtime behavior and should not be edited manually.


🧩 Accessing Models

Models are retrieved through the DataContext.

var inventory = DataContext.Get<InventoryItemModel>();

⚠️ Best Practices

βœ” Use simple public fields βœ” Always define a primary key for list models βœ” Avoid heavy logic inside model classes βœ” Treat models as data containers only


πŸ”— See Also