Modeling the domain

At the heart of any domain-driven application is the model that represents the system’s core knowledge and business rules. Modeling the domain well means translating real-world concepts into expressive, cohesive, and consistent software structures.

Entities

An entity is an object defined primarily by its identity and not just by its attributes. Even if the attributes change over time, the identity of an entity remains the same.

Main characteristics:

  • Has a unique identity (usually an Id).
  • What matters is who the entity is, not just what it contains.
  • Its attributes may change over time.

Creating an entity with Lino

To create a new entity using Lino, run:

lino entity new

The CLI wizard will ask for:

  • Service – The service where the entity will be created.
  • Module – The module where the entity will be created (only in modular services).
  • Entity name – The name used in the domain and in the database table.

Then, you will define the fields that make up the entity, configuring each one.

Available field types

Type Description Range / Notes
short16-bit integer-32,768 β†’ 32,767
int32-bit integer-2,147,483,648 β†’ 2,147,483,647
long64-bit integer-9,223,372,036,854,775,808 β†’ 9,223,372,036,854,775,807
stringTextUp to ~2 billion characters
boolBoolean valuetrue or false
GuidGlobally unique identifierDistributed uniqueness
decimalHigh-precision decimal numberIdeal for monetary values
floatFloating point (32-bit)β‰ˆ 6–9 digits of precision
doubleFloating point (64-bit)β‰ˆ 15–17 digits of precision
DateTimeDate and timeIncludes time zone
DateOnlyDate only (C# 10+)–
TimeOnlyTime only (C# 10+)–
EntityReference to another entity1:1 or 1:N
Value ObjectImmutable Value Objecte.g., Address, CPF
EnumEnumerationFixed set of values
List<Entity>List of entities1:N
ManyToManyMany-to-manyRequires a join table

Example

Creating the entity Person:

β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ PK β”‚ FK β”‚ Property name β”‚ Type   β”‚ Length β”‚ Required  β”‚ Auto-increment β”‚
β”œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ x  β”‚    β”‚ Id            β”‚ int    β”‚        β”‚     x     β”‚       x        β”‚
β”œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    β”‚    β”‚ Name          β”‚ string β”‚  100   β”‚     x     β”‚                β”‚
β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Structure generated by Lino:

<ProjectName>/
└── src/
    └── Services/
        └── <ServiceName>/
            └── Domain/
                β”œβ”€β”€ <ProjectName>.<ServiceName>.Domain.csproj
                └── Aggregates/
                    └── People/
                        β”œβ”€β”€ Person.cs
                        β”œβ”€β”€ Errors/
                        β”‚   └── PersonErrors.cs
                        β”œβ”€β”€ Repositories/
                        β”‚   └── IPersonRepository.cs
                        └── Resources/
                            └── Person/
                                β”œβ”€β”€ PersonResources.resx
                                β”œβ”€β”€ PersonResources.en.resx
                                └── PersonResources.pt-BR.resx

After defining your entities, use Lino itself to manage the Migrations and keep the database synchronized. We will cover this process in detail in the Persistence Layer section.

Current evolution workflow

Besides the interactive lino entity new flow, the CLI also lets you make intent explicit when service, module, and name are already known:

lino entity new --name <EntityName> --service <ServiceName> --module <ModuleName>
lino entity edit --service <ServiceName> --module <ModuleName> --entity <EntityName>
lino entity list --service <ServiceName> --module <ModuleName>

Use entity list before creating new concepts in large modules. During edits, review identifier, properties, requiredness, length, relationships, indexes, ownership, tenant, and migration impact.

Strongly Typed IDs, ownership, and invariants

When Strongly Typed IDs are enabled, the identifier becomes a dedicated type, such as ProductId, reducing accidental swaps between IDs from different entities. Relationships should reflect real domain ownership: not every reference needs direct navigation; often an identifier, shadow entity, integration event, or explicit query protects module boundaries better.

Put invariants in the domain, not only in the UI, database, or validators. After changing entities, run a build, review the diff, and generate or update migrations.

Value Objects

A Value Object represents a domain concept defined only by its attributes – it does not have its own identity. Two Value Objects are considered equal if all their values are equal.

Main characteristics:

  • Immutable after creation.
  • Do not have an Id.

Creating a Value Object with Lino

Run:

lino value-object new

The CLI will ask for:

  • Service – Service in which the object will be created.
  • Module – Module in which the object will be created (only in modular services).
  • Location – Domain root or specific aggregate.
  • Value object name.

Then, define the fields that make up the object.

Available field types

TypeDescriptionNotes
short16-bit integer-32,768 β†’ 32,767
int32-bit integer-2,147,483,648 β†’ 2,147,483,647
long64-bit integer-9,223,372,036,854,775,808 β†’ 9,223,372,036,854,775,807
stringTextUp to ~2 billion characters
boolBooleantrue/false
decimalPrecise decimalMonetary values
floatFloating point (32-bit)β‰ˆ 6–9 digits
doubleFloating point (64-bit)β‰ˆ 15–17 digits
DateTimeDate/timeIncludes timezone
DateOnlyDate onlyC# 10+
TimeOnlyTime onlyC# 10+

Example

Value object Address:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Property name β”‚ Type   β”‚ Length β”‚ Required  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Street        β”‚ string β”‚  100   β”‚     x     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Number        β”‚ string β”‚   10   β”‚     x     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Neighborhood  β”‚ string β”‚   50   β”‚           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ City          β”‚ string β”‚  100   β”‚     x     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ State         β”‚ string β”‚   2    β”‚     x     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ PostalCode    β”‚ string β”‚   20   β”‚     x     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Country       β”‚ string β”‚  100   β”‚     x     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Generated file structure (aggregate Person):

<ProjectName>/
└── src/
    └── Services/
        └── <ServiceName>/
            └── Domain/
                β”œβ”€β”€ <ProjectName>.<ServiceName>.Domain.csproj
                └── Aggregates/
                    └── People/
                        β”œβ”€β”€ Person.cs
                        β”œβ”€β”€ ValueObjects/
                        β”‚   └── Address.cs
                        β”œβ”€β”€ Errors/
                        β”‚   β”œβ”€β”€ AddressErrors.cs
                        β”‚   └── PersonErrors.cs
                        β”œβ”€β”€ Repositories/
                        β”‚   └── IPersonRepository.cs
                        └── Resources/
                            β”œβ”€β”€ Address/
                            β”‚   β”œβ”€β”€ AddressResources.resx
                            β”‚   β”œβ”€β”€ AddressResources.en.resx
                            β”‚   └── AddressResources.pt-BR.resx
                            └── Person/
                                β”œβ”€β”€ PersonResources.resx
                                β”œβ”€β”€ PersonResources.en.resx
                                └── PersonResources.pt-BR.resx

As with entities, Migrations can be managed by Lino to keep the data model in sync.

Current evolution workflow

Besides the interactive command, use parameters when you already know where the concept should be created:

lino value-object new --name <ValueObjectName> --service <ServiceName> --module <ModuleName>
lino value-object edit --service <ServiceName> --module <ModuleName> --value-object <ValueObjectName>
lino value-object list --service <ServiceName> --module <ModuleName>

When an entity property is declared as ValueObject, Lino can reuse an existing Value Object or create a new one during entity modeling. Prefer this option when the fields represent one concept, such as money, address, dimensions, period, or document.

Persistence, UI, and localization

Lino maps Value Object properties through the module persistence layer and carries display metadata into resources. This enables localized labels and messages for nested values. The Value Object belongs to the module where it was generated; it does not become a shared contract between modules without an explicit integration.

Protect invariants in the type itself: negative values, invalid currency, inverted periods, or malformed documents should not be valid.

Enumerations

In DDD, enumerations can go beyond the traditional C# enum. They can be rich objects that represent fixed states, containing validations, helper methods, and even behavior.

Motivation:

  • C# enums are limited to an integer or string value.
  • Modeling an Enumeration as a class offers greater flexibility and expressiveness.

Main characteristics:

  • They are classes inheriting from a common base, encapsulating Id and Name.
  • Allow adding validations, helper methods, and behavior.

Creating an enumeration with Lino

Run:

lino enumeration new

The assistant will ask for:

  • Service.
  • Module (if applicable).
  • Location – Root of the domain or aggregate.
  • Enumeration name.
  • Type – Traditional enum or Smart Enum (class).
  • Storage – int or string in the database.

Example

Enumeration PersonStatus:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Value β”‚ Name      β”‚ Display Name β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1     β”‚ Active    β”‚ Active       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 2     β”‚ Inactive  β”‚ Inactive     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 3     β”‚ Suspended β”‚ Suspended    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 4     β”‚ Deleted   β”‚ Deleted      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Generated structure:

<ProjectName>/
└── src/
    └── Services/
        └── <ServiceName>/
            └── Domain/
                β”œβ”€β”€ <ProjectName>.<ServiceName>.Domain.csproj
                └── Aggregates/
                    └── People/
                        β”œβ”€β”€ Person.cs
                        β”œβ”€β”€ Enums/
                        β”‚   └── PersonStatus.cs
                        β”œβ”€β”€ ValueObjects/
                        β”‚   └── Address.cs
                        β”œβ”€β”€ Errors/
                        β”‚   β”œβ”€β”€ AddressErrors.cs
                        β”‚   └── PersonErrors.cs
                        β”œβ”€β”€ Repositories/
                        β”‚   └── IPersonRepository.cs
                        └── Resources/
                            β”œβ”€β”€ Address/
                            β”‚   β”œβ”€β”€ AddressResources.resx
                            β”‚   β”œβ”€β”€ AddressResources.en.resx
                            β”‚   └── AddressResources.pt-BR.resx
                            β”œβ”€β”€ Person/
                            β”‚   β”œβ”€β”€ PersonResources.resx
                            β”‚   β”œβ”€β”€ PersonResources.en.resx
                            β”‚   └── PersonResources.pt-BR.resx
                            └── PersonStatus/
                                β”œβ”€β”€ PersonStatusResources.resx
                                β”œβ”€β”€ PersonStatusResources.en.resx
                                └── PersonStatusResources.pt-BR.resx

Storing an enumeration value as a string is valid and can improve readability, but tends to be less efficient in terms of performance and storage. Therefore, we recommend storing the value as an int, and to maintain referential integrity and ease maintenance, create an auxiliary entity (table) where the primary key corresponds to the enumeration value.

After defining your enumerations, use Lino to generate and apply Migrations, ensuring the database reflects the domain model. See details in the Persistence Layer section.

Current evolution workflow

Besides the interactive command, use parameters when location and name are already defined:

lino enumeration new --name <EnumerationName> --service <ServiceName> --module <ModuleName>
lino enumeration edit --service <ServiceName> --module <ModuleName> --enumeration <EnumerationName>
lino enumeration list --service <ServiceName> --module <ModuleName>

Enumeration, entity, or configuration

Use enumerationUse entity or configuration
Order status, publication state, technical integration typeUser-managed category, tenant-configurable reason, supplier managed in backoffice
Values versioned with codeValues changed at runtime or controlled by permissions
Simple rule or small behavior per valueLifecycle, audit, dynamic translation, or own relationship

If the enumeration exposes a display name, Lino can generate resources for localized labels. If it is persisted or used as seed data, review migrations after adding, removing, or renaming values. Consistency between code, database, and UI is part of the domain change.

An unhandled error has occurred. Reload πŸ—™