Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit ffb720c

Browse files
committed
Extract the use case business logic to its own Application Service class and execute it from the main entry point
* Create the `AggregateRoot` abstract class in order to deal with recorded domain events being able to pull them out afterwards * Refactor the `EventBus#publish` contract in order to receive a `List` of `DomainEvent`s instead of a single one. I've tried with the varargs approach in order to be able to publish one single event or a list of them, but it wouldn't match the actual contract of the `AggregateRoot#pullDomainEvents(): List<DomainEvent>` return type. So taking into account we would have to convert it to an `Array` (cost O(n)) just because of that, I think it make sense to just use `List` on the `EventBus` side and simplify the signatures at least for now 🤟
1 parent 23ef6ef commit ffb720c

File tree

7 files changed

+76
-14
lines changed

7 files changed

+76
-14
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tv.codely;
22

33
import tv.codely.context.notification.module.push.application.create.SendPushToSubscribersOnVideoPublished;
4-
import tv.codely.context.video.module.video.domain.VideoPublished;
4+
import tv.codely.context.video.module.video.application.create.VideoPublisher;
55
import tv.codely.shared.application.DomainEventSubscriber;
66
import tv.codely.shared.domain.EventBus;
77
import tv.codely.shared.infrastructure.bus.ReactorEventBus;
@@ -13,14 +13,12 @@ public static void main(String[] args) {
1313
final Set<DomainEventSubscriber> subscribers = Set.of(
1414
new SendPushToSubscribersOnVideoPublished()
1515
);
16-
1716
final EventBus eventBus = new ReactorEventBus(subscribers);
17+
final var videoPublisher = new VideoPublisher(eventBus);
1818

19-
final var videoPublished = new VideoPublished(
20-
"\uD83C\uDF89 New youtube.com/CodelyTV video title",
21-
"This should be the video description \uD83D\uDE42"
22-
);
19+
final var videoTitle = "\uD83C\uDF89 New YouTube.com/CodelyTV video title";
20+
final var videoDescription = "This should be the video description \uD83D\uDE42";
2321

24-
eventBus.publish(videoPublished);
22+
videoPublisher.publish(videoTitle, videoDescription);
2523
}
2624
}

src/main/java/tv/codely/context/video/module/video/application/create/VideoCreator.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tv.codely.context.video.module.video.application.create;
2+
3+
import tv.codely.context.video.module.video.domain.Video;
4+
import tv.codely.shared.domain.EventBus;
5+
6+
public final class VideoPublisher {
7+
private final EventBus eventBus;
8+
9+
public VideoPublisher(EventBus eventBus) {
10+
this.eventBus = eventBus;
11+
}
12+
13+
public void publish(String title, String description) {
14+
final var video = Video.publish(title, description);
15+
16+
eventBus.publish(video.pullDomainEvents());
17+
}
18+
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package tv.codely.context.video.module.video.domain;
2+
3+
import tv.codely.shared.domain.AggregateRoot;
4+
5+
public final class Video extends AggregateRoot {
6+
private final String title;
7+
private final String description;
8+
9+
private Video(String title, String description) {
10+
this.title = title;
11+
this.description = description;
12+
}
13+
14+
public static Video publish(String title, String description) {
15+
var video = new Video(title, description);
16+
17+
var videoCreated = new VideoPublished(title, description);
18+
19+
video.record(videoCreated);
20+
21+
return video;
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tv.codely.shared.domain;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
public abstract class AggregateRoot {
7+
private List<DomainEvent> recordedDomainEvents = new LinkedList<>();
8+
9+
final public List<DomainEvent> pullDomainEvents() {
10+
final var recordedDomainEvents = this.recordedDomainEvents;
11+
this.recordedDomainEvents = new LinkedList<>();
12+
13+
return recordedDomainEvents;
14+
}
15+
16+
final protected void record(DomainEvent event) {
17+
recordedDomainEvents.add(event);
18+
}
19+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tv.codely.shared.domain;
22

3+
import java.util.List;
4+
35
public interface EventBus {
4-
void publish(DomainEvent event);
6+
void publish(final List<DomainEvent> events);
57
}

src/main/java/tv/codely/shared/infrastructure/bus/ReactorEventBus.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import tv.codely.shared.application.DomainEventSubscriber;
88
import tv.codely.shared.domain.DomainEvent;
99

10+
import java.util.Arrays;
11+
import java.util.List;
1012
import java.util.Set;
1113

1214
import static reactor.bus.selector.Selectors.$;
@@ -21,7 +23,11 @@ public ReactorEventBus(final Set<DomainEventSubscriber> subscribers) {
2123
}
2224

2325
@Override
24-
public void publish(final DomainEvent event) {
26+
public void publish(final List<DomainEvent> events) {
27+
events.forEach(this::publish);
28+
}
29+
30+
private void publish(final DomainEvent event) {
2531
Class<? extends DomainEvent> eventIdentifier = event.getClass();
2632
Event<DomainEvent> wrappedEvent = Event.wrap(event);
2733

0 commit comments

Comments
 (0)