AshCommanded.Commanded.Transformers.GenerateMainRouterModule (AshCommanded v0.1.0)

View Source

Generates the main router module that delegates to domain-specific routers.

This transformer collects all command-resource mappings from the DSL state and generates:

  1. Domain-specific router modules for each domain with commands
  2. 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.before?/1.

Builds the module name for the main router.

Transforms the DSL state to generate router modules.

Functions

after?(arg1)

Specifies that this transformer should run after the domain router transformer.

after_compile?()

Callback implementation for Spark.Dsl.Transformer.after_compile?/0.

before?(_)

Callback implementation for Spark.Dsl.Transformer.before?/1.

build_main_router_module(app_prefix)

Builds the module name for the main router.

Examples

iex> build_main_router_module(MyApp)
MyApp.Router

transform(dsl_state)

Transforms the DSL state to generate router modules.

This transformer will:

  1. Collect command mappings from all resources
  2. Generate domain-specific router modules
  3. Generate the main router module

Examples

iex> transform(dsl_state)
{:ok, updated_dsl_state}