-
Notifications
You must be signed in to change notification settings - Fork 7
Cleanup Java #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Cleanup Java #1
Changes from 1 commit
64a3139
5284899
b74f578
db2fa14
8f6c0bb
f662d05
18d0171
9b2f7a2
5cd75f4
7ae25a2
3790e6c
15e7593
a7b76a8
f3d26ce
80d5a2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,18 @@ | ||
| package net.agilepartner.workshops.cqrs.core; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Getter | ||
| @Setter(AccessLevel.PROTECTED) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ouuuuhhh black magic
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Black magic has been removed. |
||
| public abstract class Command implements Message { | ||
| public UUID id; | ||
| public UUID aggregateId; | ||
|
|
||
| private static final long serialVersionUID = -840035726759327475L; | ||
|
|
||
| private UUID id; | ||
| private UUID aggregateId; | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| package net.agilepartner.workshops.cqrs.core; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Getter | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More black magic |
||
| @Setter | ||
| public abstract class Event implements Message { | ||
| public UUID aggregateId; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand it seems counter-intuitive to have public fields, but most likely the events will be serialized, and there is no real need for encapsulation here, since events (and command for that matter) are mainly data containers, and have no logic.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, it's counter-intuitive. And in case field are not encapsulated, the temptation is use something like lombok. |
||
| public int version; | ||
|
|
||
| private static final long serialVersionUID = 8922791526755347386L; | ||
|
|
||
| private UUID aggregateId; | ||
| private int version; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package net.agilepartner.workshops.cqrs.core.infrastructure; | ||
|
|
||
| import net.agilepartner.workshops.cqrs.core.Event; | ||
|
|
||
| public class UnsupportedEventException extends RuntimeException { | ||
|
|
||
| public UnsupportedEventException(Class< ? extends Event> eventType) { | ||
| super("Unsupported event " + eventType.getSimpleName()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,18 +63,18 @@ private void checkActivated() throws InventoryItemDeactivatedException { | |
| protected <T extends Event> void apply(T event) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this is exactly what we want to avoid by having the "ugly" reflection code in the I want to avoid the if-then-else like the plague and I'd much rather stay with nice, clean, and small I agree that reflection is not the most efficient and clean way to implement that. There are cleaner ways to implement the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this point, I'm totally agree it is not ideal. I'm just looking for a better solution. Single Responsibility Principle is respected, and there is no more ugly code. |
||
| if (event instanceof InventoryItemCreated) { | ||
| InventoryItemCreated evt = (InventoryItemCreated) event; | ||
| this.name = evt.name; | ||
| this.stock = evt.quantity; | ||
| this.name = evt.getName(); | ||
| this.stock = evt.getQuantity(); | ||
| this.active = true; | ||
| } else if (event instanceof InventoryItemRenamed) { | ||
| InventoryItemRenamed evt = (InventoryItemRenamed) event; | ||
| this.name = evt.name; | ||
| this.name = evt.getName(); | ||
| } else if (event instanceof InventoryItemCheckedIn) { | ||
| InventoryItemCheckedIn evt = (InventoryItemCheckedIn) event; | ||
| this.stock += evt.quantity; | ||
| this.stock += evt.getQuantity(); | ||
| } else if (event instanceof InventoryItemCheckedOut) { | ||
| InventoryItemCheckedOut evt = (InventoryItemCheckedOut) event; | ||
| this.stock -= evt.quantity; | ||
| this.stock -= evt.getQuantity(); | ||
| } else if (event instanceof InventoryItemDeactivated) { | ||
| this.active = false; | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this code is a workshop, and because not everyone is familiar with lombok, I wanted to use vanilla java code and not carry extra dependencies doing black magic. I admit though that lombok is a good library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used it when I started to modify the code, but it has been removed since.