CQRS How to avoid repeating fields between a command and an event?

I am implementing a project using CQRS and Event Sourcing. I realized that my teams and my events are almost always the same.

Say I have a command CreatePost:

public class CreatePost implements Command {
    private final String title;
    private final String content;
}

The event fired from this command is the same:

public class PostCreated implements Event {
    private final String title;
    private final String content;
}

How do you deal with this in your applications?

EDIT . Of course, I know the basic OOP technique. I could create an abstraction that has common fields, but this question needs to be taken in the context of CQRS / ES.

+4
source share
6 answers

How to avoid repeating fields between a team and an event?

I would not, until I can stand it.

, , - , . , .

, , . : .

- , ; API / .

. , , , .

, - .

, , , .

CreatePost 
    { Post 
        { Title
        , Contents
        }
    }

PostCreated
    { Post 
        { Title
        , Contents
        }
    }
+6

Post, ..

public class PostModel {
    private String title;
    private String content;

    // Add get/set methods
}

, .

+1

?

. + , . , , , , , .. .

+1

, , , .

PHP traits . , (, ) ; , , -, Java, , .

P.S. , , .

0

, .

,

, , , .

- . - IPost IEvent - .

: , , , / .

?

, , , . .

, , , , . , - - - BC.

0

, , , . / getters/equals/hashCode/toString . , interface Action

interface Command<T extends Action> {
  T getAction();
  // other properties common to commands of all action types...
}

class AbstractCommand<T extends Action> implements Command<T> {
  public T getAction() { ... }
  // other properties...
}

interface Event<T extends Action> {
  T getAction();
  // other properties common to events of all action types...
}

class AbstractEvent<T extends Action> implements Event<T> {
  public T getAction() { ... }
  // other properties...
}

.

class ConcreteAction implements Action {
  // properties COMMON to the command and event(s)...
}

class ConcreteCommand extends AbstractCommand<ConcreteAction> { ... }

class ConcreteEvent extends AbstractEvent<ConcreteAction> { ... }

- , ConcreteCommand ConcreteEvent.

The inheritance model here is very simple. You may only need to do more in rare cases than just extend abstract classes without using anything but the usual Action. And in the case when there are no properties necessary for Action, just define class EmptyAction implements Actionfor use in these types of commands and events.

0
source

Source: https://habr.com/ru/post/1682411/


All Articles