Jivejdon is a full DDD application with Event Soucing/CQRS and clean architecture/Hexagonalarchitecture, powered by jdonframework ,running for over ten years in: https://www.jdon.com/forum
com.jdon.jivejdon.model.ForumMessage is the aggregate root entity of post bounded context, it is a rich model not anemic model, no "public" setter method, all setter methods are "private":
Aggregate root entity builder pattern:
Domain Model principles:
- High level of encapsulation
All members setter method are private
by default, then internal
. need heavy builder pattern to create aggregate root!
- High level of PI (Persistence Ignorance)
No dependencies to infrastructure, databases, other stuff. All classes are POJO.
The customer/supply model from jdonframework can seperate domain model from Persistence/Repository.
All datas out of domain is packed in a DTO anemic model(AnemicMessageDTO), so business rules in the aggregate root entity will not leak outside domain.
these DTO anemic models can alseo be packed in Command and Domain Events,so they be managed in DDD ubiquitous business language.
- Rich in behavior
All business logic is located in Domain Model. No leaks to application layer or other places.
- Low level of primitive obssesion
Primitive attributes of Entites grouped together using ValueObjects. MessageVO is a valueObject of aggregate root entityForumMessage, inlcude message content: subject and body, it will be processed with complex business filter logic, these filters have many implements,such as: TEXT to HTML.
- Business language
All classes, methods and other members named in business language used in the Bounded Context.
Posting a message is a event, modifying the last replies status for one thread.
How to get the the last replies status for one thread?we must iterate all posted events collection.
JiveMessage is a posted events collection database table, with a SQL select we can get the last message posted event:
SELECT messageID from jiveMessage WHERE threadID = ? ORDER BY modifiedDate DESC
This sql can quickly find the last replies, No need to play back all posted events.
In jiveThread table there is no special field for last replyies state , all states are from posted events projection. (projection can use SQL!)
When a user post a new ForumMessage, a ReplyMessageCreatedEvent event will be saved to event store: JiveMessage, simultaneously refresh the snapshot of event: ForumThreadState.
JiveJdon is developed with JdonFramework that supports Customer/Supply or pub-sub model, this model can seperate domain logic from infrastructure, databases, other stuff.
JiveJdon and Hexagonal_architecture:
models.xml is a adapter, it is XML configure acting as a controller.
<model key="messageId" class="com.jdon.jivejdon.model.message.AnemicMessageDTO">
<actionForm name="messageForm"/>
<handler>
<service ref="forumMessageService">
<createMethod name="createReplyMessage"/>
</service>
</handler>
</model>
When post a replies message, a POST command will action createReplyMessage method of forumMessageService :
public interface ForumMessageService {
Long createReplyMessage(EventModel em) throws Exception;
....
}
Domain sevice forumMessageService will delegate responsibility to business logic object:the aggregate root entity ForumMessage, that has two types: topic post and rely post, topic is a root message and has many replies messages, all messages compose one Thread(ForumThread)
Business/domain logic is in the addChild message method of ForumMessage
@OnCommand("postReplyMessageCommand") annotation is a command handler in pub-sub model from jdonframework, it can make this method executed in a single-writer pattern - no blocked, no lock, high concurrent. only one thread/process invoking this update method.
"eventSourcing.addReplyMessage" will send a "ReplyMessageCreatedEvent" domain Event to infrastructure layer such as Repository. with the pub-sub model of jdonframework, make domain no dependencies to infrastructure, databases, other stuff.
Domain event "ReplyMessageCreatedEvent" occurring in the domain will be saved in event store "jiveMessage" that is a posted events table to project the last replies state of a thread.(Event Sourcing)
CQRS addresses separates reads and writes into separate models, using commands to update data, and queries to read data.
In jivejdon ForumThread and ForumMessage are saved in cache, cache is a snapshot of even logs, if a update command activate one of these models, they will send domain events to clear the cache datas, the cache is similar as the database for query/read model, the consistency between with cache and the database for commmand model is maintained by the domain events such as "ReplyMessageCreatedEvent".
The domain event "ReplyMessageCreatedEvent" do three things:
- add a new post message to "jiveMessage" (events log)
- clear the query cache (CQRS)
- update/project the last replies state of a thread (event project to state)
Docker:
- mvn package
- docker build -t jivejdondb -f Dockerfile.db .
- docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 jivejdondb
- docker build -t jivejdonweb -f Dockerfile.web .
- docker run -p 8080:8080 jivejdonweb
browser : http://$DOCKER_HOST_IP:8080