Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional stream prefix per aggregate #77

Closed
sascha-wolf opened this issue Sep 14, 2017 · 1 comment · Fixed by #87
Closed

Additional stream prefix per aggregate #77

sascha-wolf opened this issue Sep 14, 2017 · 1 comment · Fixed by #87

Comments

@sascha-wolf
Copy link

sascha-wolf commented Sep 14, 2017

It would be useful to be able to define a prefix on a per-aggregate basis. This would reduce risk of collisions when using a lot of aggregates with generated IDs and ease separation of streams on a business logic level.

Example

Let's assume we have a general User aggregate which uses UUIDs as identification. For our business logic we need to relate additional data to this user, as such we add additional aggregates which also use the user ids as identification for ease of use. While being related but separate entities this would result in a single stream containing all events from all aggregates per user.

Proposal

Similar to process managers or event handlers the name of an aggregate could be defined in a use clause. This could internally define a @behaviour conformance with a callback providing the aggregate name.

To allow backwards compatibility the Aggregate module could use function_exported?/3 to check for the behaviour conformance in advance and fall back to the current logic. The old behaviour could be deprecated if wished for and removed in a later version.


What do you think of this?

@slashdotdash
Copy link
Member

slashdotdash commented Sep 14, 2017

Yes, this feature is a useful suggestion.

Right now in Commanded, the command-to-aggregate identity mapping is handled by your router module. You specify the identity option for each command:

defmodule BankRouter do
  use Commanded.Commands.Router

  dispatch OpenAccount, to: BankAccount, identity: :account_number
end

Therefore we could use this as the extension point.

Router identity prefix

  1. Include another option to specify the identity prefix:

    defmodule BankRouter do
      use Commanded.Commands.Router
    
      dispatch [OpenAccount,DepositMoney,WithdrawMoney] 
        to: BankAccount, 
        identity: :account_number,
        identity_prefix: "bank-account-"
    end
  2. Allow a function to be used as the identity option, it could then construct the identity as required from the command being dispatched:

    defmodule BankRouter do
      use Commanded.Commands.Router
    
      dispatch [RegisterUser] 
        to: User, 
        identity: fn %{uuid: uuid} -> "user-#{uuid}" end
    
      dispatch [OpenAccount,DepositMoney,WithdrawMoney] 
        to: BankAccount, 
        identity: &BankRouter.account_identity/1
    
      def account_identity(%{account_number: account_number}), do: "bank-account-#{account_number}"
    end
  3. Allow an aggregate identity to be specified for all its commands, providing an optional prefix:

    defmodule BankRouter do
      use Commanded.Commands.Router
    
      identify BankAccount, 
        by: :account_number, 
        prefix: "bank-account-"
    
      dispatch [OpenAccount,DepositMoney,WithdrawMoney] to: BankAccount
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants