Reputation: 323
There is 'Note' Domain object as Entity.
@Entity
@Getter
@Setter
class Note {
@ManyToOne(..)
private User writer;
private String title;
private String content;
... some attributes ...
private Date createdAt;
private Date updatedAt;
private User latestModifier;
... some methods ...
}
I thought 'NoteEditor' for treating open & save note, such as below.
class NoteEditorImpl implements NoteEditor {
private NoteRepository repository;
private ApplicationPublisher publisher;
private Note note; // opened note.
private User user; // operator.
NoteEditorImpl(ApplicationContext context, User user) {
this.repository = context.getBean(NoteRepository.class);
this.publisher = context.getBean(ApplicationPublisher.class);
this.user = user;
}
NoteEditor open(Note note) {
if ( !note.canBeEditedBy(user) ) {
throw new ServiceRuntimeException('... cause message ...');
}
this.note = note;
return this;
}
NoteEditor setTitle(String title) {
...
this.note.setTitle(title);
return this;
}
...
Note save() {
this.note.setLatestModifier(this.user);
this.note.setUpdatedAt(new Date());
publisher.publish(new NoteEditedEvent(this.note));
repository.save(this.note);
}
}
As you can see, the NoteEditor is no stateless.
I'm wondering NoteEditor is Domain Service in DDD ?
Also I want to see your opinion , correction for my design grow up.
APPEND. I explain why made stateful editor.
I can assume there are two clients (WebClient and MobileClient).
WebClient will use it such as below.
NoteEditor editor = noteService.getEditor(sessionUserId);
editor.open(selectedNote).setTitle(…).setContent(…).save();
MobileClient will use it such as below.
NoteEditor editor = noteService.getEditor(sessionUserId);
editor.open(selectedNote).setTitle(..).save();
It means some client can edit a little attributes for one time.
Therefore, I’d like to check editable(authority) in the open() method, and I’d like to put modifying information in the save() method such as above code block.
Below is NoteService code.
class NoteServiceImpl implements NoteService {
...
NoteEditor getEditor(long userId) {
User user = userRepository.findOne(userId);
NoteEditor editor = new NoteEditor(context, user);
return editor;
}
List<Note> getMyNotes(...) ...
}
Upvotes: 1
Views: 1025
Reputation: 19987
Your NoteEditor has two responsibilities. One is to edit a Note object and another is to open and save a Note object. If you separate the two everything will quickly fall into place. You need a NoteRepository for opening and saving and a NoteBuilder for a fluent API. The NoteBuilder is not a domain object, but simply a helper class for building a note via fluent API.
Upvotes: 3