Bojan Vukasovic
Bojan Vukasovic

Reputation: 2268

Domain Events - Inheritance or use of enums

So guess that I have this kind of domain events:

class BookChangedName...
class BookType1ChangedName extends BookChangedName...
class BookType2ChangedName extends BookChangedName...

Is that better or:

class BookChangedName{
    enum bookType = BOOK_TYPE_1;
}

Since they say use inheritance only if there is a different behavior between classes, I assume here I would go with enum sample (case #2) - since Domain Events are just plain simple DTOs.

But again, in my domain this different types of events have different meaning (different processing paths). So in case I use example #1 i would end up with a lot of:

if(event instanceof BookType1ChangedName){
   //do smth in domain
}
else if(event instanceof BookType2ChangedName){
   //do smth in domain
}

and also I could not be as explicit as:

when(BookType1ChangedName event){...

I would have to do some kind of pre-processing like:

@EventHandler(matcher_pattern = event->event.bookType==BOOK_TYPE_1)
when(BookChangedNameevent){...

Upvotes: 0

Views: 791

Answers (3)

Chris Simon
Chris Simon

Reputation: 6505

Do your domain experts use the phrase "BookType1"? You said that there are different logic paths for each book type - I'd suggest interrogate the rational for that, and perhaps there is a more domain-oriented name for each event? Try and listen for the language used by the domain experts to describe the two situations.

If you can find more descriptive language, I would go with separate events based on inheritence. With some domain event mechanisms, you can subscribe to handle either the abstract super class or either of the specialisations, which may be helpful if you want that flexibility.

On the other hand, if it really is just booktype1 and booktype2 and in the future maybe booktype3 and booktype4, I'd consider either an enum or even just an integer and consider implementing the different logic paths in different strategies and have your domain event handler use a factory to return the appropriate strategy for the given book type, then delegate to the strategy to execute the logic.

Upvotes: 2

alltej
alltej

Reputation: 7285

You should just have a single BookChangedName event and have a BookType property for that event. The handler/subscriber of the event should take care of the logic.

Upvotes: 0

Constantin Galbenu
Constantin Galbenu

Reputation: 17683

Event names are an important part in DDD (in fact any "name"is important) so I suggest to use different event names for different events as those names carry a lot of information. Also, if you use a type code (with enum) you increase the CRAP index because of the additional ifs.

Upvotes: 0

Related Questions