PouryaKit
Get started

Add a Module

Grow a solution past its first Module, from the monolith or in the modular monolith.

What it is

A Module is a business boundary inside a Host: its own project, data, rules and public Contracts. Its Composition Root declares everything it needs and offers, and the Host adds it once. Anything outside the Module reaches it through its Gateway.

The monolith has no Modules yet

The monolith registers Catalog on the Host's own services, without PouryaKit.Modularity. A second Module needs the modular monolith's composition, and growing into it leaves the business code as it is.

When to use it

When a part of your business has data and rules of its own that other parts should reach only through its Contracts. Calls between Modules of one Host stay in-process: Integration Events and Snapshots through the Host's Outbox, and a question through the owning Module's Contracts, never HTTP.

Add it

Start from the modular monolith

Generate it, or grow the monolith into it: Growing the monolith into a modular monolith lists the files that change.

Terminal
dotnet new pouryakit-modular-monolith -n Acme.Shop

Create the Module's projects

Give the Module a folder under src/Modules/ with its project, and a Contracts project when other Modules use what it publishes, as Catalog has Catalog.Contracts. Its project references PouryaKit.Modularity and the Kit packages its Features use; Asking another Module a question says what a Contracts project may reference. Add the project to the solution and reference it from the Host.

Write its Composition Root

One public sealed class that implements IModuleDefinition, whose Configure(ModuleBuilder module) declares the Module's data, Features, handlers, publications, subscriptions and endpoints. It never names its own Module or assembly: the Kit infers both. The Composition Root shows Catalog's.

Add it to the Host

In AppHost.cs, add it beside the others with modules.Add<…>() in AddPouryaKitModules, and give it its own section in appsettings.json when it has settings.

Give it a migration and migrate

Each Module keeps its own schema and its migrations in its own folder; Migrations has the dotnet ef migrations add command. Then migrate it as Your first Host migrates Catalog, naming its project and its DbContext.

What holds for every Module

RuleStated in
A Module references no other Module's implementationStructure tests
A declaration belongs to the Module whose builder it was made onThe Composition Root

Failure messages

Next steps

On this page