https://github.com/linux-china/ddd-base
DDD(Domain Driven Design) base package for java
https://github.com/linux-china/ddd-base
ddd
Last synced: 9 months ago
JSON representation
DDD(Domain Driven Design) base package for java
- Host: GitHub
- URL: https://github.com/linux-china/ddd-base
- Owner: linux-china
- Created: 2014-11-17T10:28:19.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2023-03-31T15:14:31.000Z (almost 3 years ago)
- Last Synced: 2025-04-02T02:51:08.861Z (9 months ago)
- Topics: ddd
- Language: Java
- Homepage:
- Size: 235 KB
- Stars: 261
- Watchers: 25
- Forks: 81
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-java - DDD Base
README
DDD Base
========

Domain Driven Design base package for Java.
# How to use it in Java?
Please refer https://jitpack.io/#linux-china/ddd-base/1.1.1
# Features
* Annotations
* Base classes for entity, domain event etc
* Domain event: follow CloudEvents specification and CloudEvent convert support
* DDD Reactive: https://www.reactive-streams.org/
# Components
* Data: Entity, VO and Aggregate
* Behaviour: Repository, Service, Factory and Specification
* Event: Follow CloudEvents spec
* Infrastructure
* CQRS interfaces for command & query
# Reactive
DDD + Reactive(RSocket) to make context map easy.
# Code Structure
Please visit [src/test/java](https://github.com/linux-china/ddd-base/tree/master/src/test/java/org/mvnsearch/demo/domain) for code structure
If you use Kotlin to develop application, the structure will be different, please add entity, vo and repository in the same kt file.
# Events
Please extend DomainEvent or DomainEventBuilder, then use ApplicationEventPublisher to publish the event. please
refer https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2
Attention: Spring framework 5.2 will add reactive support: https://github.com/spring-projects/spring-framework/issues/21831
Event extensions(JavaScript Object):
* Distributed Tracing extension(traceparent, tracestate): embeds context from Distributed Tracing so that distributed systems can include traces that span an event-driven system.
* Dataref(dataref): reference another location where this information is stored
* Partitioning(partitionkey): This extension defines an attribute for use by message brokers and their clients that support partitioning of events, typically for the purpose of
scaling.
* Sequence(sequence): describe the position of an event in the ordered sequence of events produced by a unique event source
* Sampling(sampledrate): Sampling
* Multi-tenant(tenantId): Multi-tenant system support
CloudEvents JSONSchema: https://github.com/cloudevents/spec/blob/v0.3/spec.json
### How to create event class
* Extend CloudEvent class:
```java
public class LoginEvent extends CloudEvent {
public LoginEvent(String email, String ip) {
setData(email);
setContentType("text/plain");
setExtension("ip", ip);
}
}
```
* Create an event directly
```
CloudEvent loginEvent = new CloudEvent("text/plain", "linux_china@hotmail.com");
```
* Event Builder or reactive processor
```
CloudEvent loginEvent = CloudEventBuilder.newInstance().contentType("text/plain").data("linux_china@hotmail.com").build();
```
* Non-blocking Event Listener: https://github.com/spring-projects/spring-framework/issues/21831
```java
@Component
public class MyListener {
@EventListener
public Mono handleContextRefresh(ContextRefreshedEvent event) {
//...
}
@EventListener
public Flux handleBusinessEvent(MyBusinessEvent event) {
//...
}
@EventListener
public CompletableFuture handleBusinessEvent(MyOtherBusinessEvent event) {
//...
}
}
```
### ObjectMapper
* ObjectMapper creation
```
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
```
* write as String
```
objectMapper.writeValueAsString(loginEvent);
```
* read from json text
```
objectMapper.readValue(jsonText, new TypeReference>() {});
```
### References
* CloudEvents Specification: https://github.com/cloudevents/spec/blob/master/spec.md
* Reactive Streams: http://www.reactive-streams.org/
* DDD Reference: https://domainlanguage.com/wp-content/uploads/2016/05/DDD_Reference_2015-03.pdf
* ddd-4-java: Base classes for Domain Driven Design with Java https://github.com/fuinorg/ddd-4-java
* jMolecules – Architectural abstractions for Java with DDD concepts https://github.com/xmolecules/jmolecules
* Domain-Driven Design Crew: https://github.com/ddd-crew
* What is Event Sourcing? https://www.eventstore.com/blog/what-is-event-sourcing
* Event Sourcing and CQRS: https://www.eventstore.com/blog/event-sourcing-and-cqrs
* CQRS Document: https://cqrs.files.wordpress.com/2010/11/cqrs_documents.pdf
* remesh: a DDD framework for large and complex TypeScript/JavaScript applications - https://github.com/remesh-js/remesh