AshCommanded.Commanded.Transformers.GenerateMainRouterModule (AshCommanded v0.1.0)
View SourceGenerates the main router module that delegates to domain-specific routers.
This transformer collects all command-resource mappings from the DSL state and generates:
- Domain-specific router modules for each domain with commands
- A main router module that delegates to these domain routers
This transformer should run after the domain router transformer has processed all resources.
Example
# Domain-specific routers
defmodule MyApp.Accounts.Router do
use Commanded.Commands.Router
dispatch [MyApp.Accounts.Commands.RegisterUser],
to: MyApp.Accounts.UserAggregate,
identity: :id
end
defmodule MyApp.Billing.Router do
use Commanded.Commands.Router
dispatch [MyApp.Billing.Commands.CreateInvoice],
to: MyApp.Billing.InvoiceAggregate,
identity: :id
end
# Main router
defmodule MyApp.Router do
@moduledoc "Main router for all commands in the application"
use Commanded.Commands.Router
# Forward commands to domain-specific routers
identify MyApp.Accounts.Commands.RegisterUser, as: :accounts
forward :accounts, to: MyApp.Accounts.Router
identify MyApp.Billing.Commands.CreateInvoice, as: :billing
forward :billing, to: MyApp.Billing.Router
# Helper function to dispatch any command
def dispatch_command(command) do
__MODULE__.dispatch(command)
end
end
For simpler applications with a single domain, the main router may directly handle commands:
defmodule MyApp.Router do
use Commanded.Commands.Router
dispatch [MyApp.Commands.RegisterUser, MyApp.Commands.CreateInvoice],
to: MyApp.UserAggregate,
identity: :id
end
Summary
Functions
Specifies that this transformer should run after the domain router transformer.
Callback implementation for Spark.Dsl.Transformer.after_compile?/0
.
Callback implementation for Spark.Dsl.Transformer.before?/1
.
Builds the module name for the main router.
Transforms the DSL state to generate router modules.
Functions
Specifies that this transformer should run after the domain router transformer.
Callback implementation for Spark.Dsl.Transformer.after_compile?/0
.
Callback implementation for Spark.Dsl.Transformer.before?/1
.
Builds the module name for the main router.
Examples
iex> build_main_router_module(MyApp)
MyApp.Router
Transforms the DSL state to generate router modules.
This transformer will:
- Collect command mappings from all resources
- Generate domain-specific router modules
- Generate the main router module
Examples
iex> transform(dsl_state)
{:ok, updated_dsl_state}