GhaylStackDocumentationv1.0.2
العربيةWebsite
Build and extend

Build and extend

Add a business module

Connect contracts, persistence, host registration and migrations without leaving a module half-wired.

Define a real boundary first

Create a module when a capability has its own business model and lifecycle. Add a feature to an existing module when that module already owns the data. Use Catalog as the reference implementation: it has Contracts, domain entities, a context, initializer, permissions, endpoints and integration tests.

For a hypothetical Assets capability, decide what it owns and what it obtains through Identity, Organizations, Files or other contracts. The names below describe a new module you create; they are not a claim that an Assets module already ships in GhaylStack.

Create the two projects and references

Create src/Modules/Assets/Modules.Assets.Contracts and src/Modules/Assets/Modules.Assets. Follow existing project files for central framework and package configuration. Set their root namespaces and assembly names to the GhaylStack.Modules.Assets naming pattern, with .Contracts for contracts.

Add both projects to src/GhaylStack.slnx. Contracts can reference required shared abstractions and Mediator abstractions; runtime references its own Contracts and the building blocks it uses. Reference another module's Contracts when needed, never its runtime as a shortcut.

Add the runtime reference to the API, DbMigrator and appropriate test projects. A persistent module also needs a reference from GhaylStack.Migrations.PostgreSQL so generated migrations can reference its context. Architecture tests discover referenced GhaylStack.Modules.* assemblies in their output directory, so include the new module reference there.

Implement one usable operation

Add a contract, validator, domain behavior, handler and versioned endpoint before expanding into many screens. Follow adding a feature. Define a module context based on BaseDbContext, apply entity configurations before its base model builder, and register an IDbInitializer for migrations and intentional seed behavior.

The module entry class implements IModule and carries an assembly-level FshModule registration with a deliberately chosen order. Its ConfigureServices registers permissions, context, initializer, module services, health check and any integration-event handlers. Its MapEndpoints creates the versioned route group and maps every feature's endpoint. Avoid adding custom middleware unless the capability actually needs it.

Wire both composition roots

There are two independent composition roots to update:

Location Required wiring
API Program.cs Add runtime assembly to moduleAssemblies; add contract and runtime markers to Mediator's assemblies
DbMigrator Program.cs Add the same module to its module list and its Mediator composition
Module entry class Register services and map the new endpoint
Migration project Reference the runtime and contain the context's migrations
Architecture test project Reference the new module so discovery includes it

Only adding a project reference is insufficient. Missing API registration can produce a 404; missing Mediator registration can fail dispatch; missing migrator registration can leave tables absent even while other modules migrate successfully.

Keep framework outbox registration at the host level. Register your event consumers, not another framework outbox owner.

Generate and review the database change

Follow migrations with the new context and the existing PostgreSQL migration project. Review schema, tenant columns, keys, indexes and relationships. Apply it to a fresh development database and a representative upgrade database.

Implement seeding deliberately. Catalog's initializer has no automatic tenant sample data; demonstration content is handled separately. Do not populate production tenants with tutorial records by placing them in unconditional startup code.

Verify the new module as a consumer would

Build the complete solution and run architecture tests. Run the migrator, start the API, then call the new operation with an authorized development user. Read the saved result back. Repeat with invalid input, missing permission and another tenant. Include organizational scope if the operation uses it.

Add an integration test to the existing fixture and verify the new module's database health check. If the change belongs to the starter kit, also run the template generation checks so the generated application includes the new projects and files. See testing, CI and adding a permission.

Implementation references

View MarkdownBack to top

Search documentation

Use ↑ ↓ to move, Enter to open and Esc to close.