UserRegisteredEvent vs RegisteredUserEvent naming

Viewed 94

We are starting to use Domain-Driven Design in my company and I'm wondering how to name events.

I'm hesitating between:

  • UserRegisteredEvent: because it is the past tense of "User registers"
  • RegisteredUserEvent: because adjectives are supposed to come first in English

UserRegisteredEvent seems to be the right one from what I can find of the internet.

But I'm wondering in case the subject is not the one doing the action like with a TextMessage:

  • a TextMessage was sent => SentTextMessageEvent
  • users send TextMessages => UserSentTextMessageEvent (a bit verbose, and we don't really care it was done by a user)
  • a TextMessage does not send itself => TextMessageSentEvent

=> What are the best practices for this? => Should I distinguish UserRegisteredEvent (the User registered himself) from RegisteredUserEvent (someone registered a new user)?

Thank you!

2 Answers

When working closely with the business stakeholders - which is one of the main pillars of leveraging domain-driven-design - the naming part is most of the time quite obvious. Business people do already know how to name what happened to what part of the system.

The what part of the system will give you the name of the involved entity (or object) while the what happened will give you a verb in the past tense.

When naming the domain events in source code we provide some readable summary of the event which makes it quickly recognizeable as the corresponding event from the business perspective.

You looked at two different naming schemas in your question:

  • Schema A: Past tense verb at the beginning
    • <past-tense-verb-describing-what-happened><name-of-the-entity-of-interest>
    • RegisteredUser
    • SentTextMessage
    • ShippedOrder
    • DeliveredParcel
  • Schema B: Past tense verb at the end
    • <name-of-the-entity-of-interest><past-tense-verb-describing-what-happened>
    • UserRegistered
    • TextMessageSent
    • OrderShipped
    • ParcelDelivered

Appending the ...Event postfix (e.g. UserRegisteredEvent) is of course valid and appending the postfix or not was almost always decided based on the preferences of the teams I've been working with.

When talking about domain events you will also have to consider domain commands. For domain commands you can also add the ...Command postfix. The command represents an order telling you what to do which involves some entity (or object) and some describing verb in it's imperative form.

For instance:

  • ShipOrder
  • RegisterUser
  • SendTextMessage

So let's look again at the following event names from your question as an example using the schema with the past tense verb at the beginning (schema A):

  • RegisteredUserEvent: because adjectives are supposed to come first in English

  • a TextMessage was sent => SentTextMessageEvent

In all cases when I was applying domain-driven design we agreed on using the other schema where the past tense verb is at the end (schema B) and here is why:

The name of the event shall be quickly recognizeable and - which is also very important to me - differentiable from commands even without using ...Event and ...Command postfixes.

I as a reader am having a hard time when I look at the following examples of commands and events using schema A:

  • SentTextMessage (event) vs. SendTextMessage (command)
  • RegisteredUser (event) vs. RegisterUser (command)

But when I use the other form of event naming (schema B) it is getting a lot easier for all involved people be it in drawings, written architectural documentation, source code, etc., e.g.:

  • TextMessageSent (event) vs. SendTextMessage (command)
  • UserRegistered (event) vs. RegisterUser (command)

An event is sonething of interest that occurred in the past, so you name it with a sentence in past tense. In your examples:

UserWasRegistered

TextMessageWasSent

To abbreviate the names you can omit "was". But "registered" is not an adjective.

Related