Upload
michael-schnell
View
269
Download
1
Embed Size (px)
DESCRIPTION
Describes how to create DDD objects using https://github.com/fuinorg/ddd-4-java
Citation preview
ddd-4-java - Domain-Driven Design (DDD) for Java
Base classes for Domain Driven Design (DDD) with Java
AggregateRoot / Entity / Repository / Event / …
Small (Only ~50 Interfaces / Classes)
Event Store Interface
Event Store Implementation for a relational database
Open Source https://github.com/fuinorg/ddd-4-java LGPL License
ddd-4-java – Vendor Example
Vendor [AggregateRoot]• VendorRef ref // VendorId + VendorKey + VendorName• Person person
Person [Entity]• PersonId id• PersonName name
CreateVendor
ChangeVendorName
AddPerson
ChangePersonNameChangeName
RemovePerson
ddd-4-java – CreateVendor: Aggregate
public class Vendor extends AbstractAggregateRoot<VendorId> {
private VendorRef ref;
:
}
Extend AbstractAggregateRoot
No public getters
ddd-4-java – CreateVendor: Constructor
public Vendor(@NotNull VendorId id, @NotNull VendorKey key, @NotNull VendorName name, @NotNull ConstructorService service) throws DuplicateVendorKeyException { // CHECK PRECONDITIONS Contract.requireArgNotNull("id", id); Contract.requireArgNotNull("key", key); Contract.requireArgNotNull("name", name); Contract.requireArgNotNull("service", service);
// VERIFY BUSINESS RULES - Normally a validator (not a service) service.addVendorKey(key);
// HANDLE EVENT apply(new VendorCreatedEvent(new VendorRef(id, key, name)));}
No state change in a constructor or method(Only allowed in a @EventHandler method)
Calls the “handle(..)” method
ddd-4-java – CreateVendor: Constructor
public class Vendor extends AbstractAggregateRoot<VendorId> {
private VendorRef ref; :
@EventHandler private void handle(VendorCreatedEvent event) { this.ref = event.getVendorRef(); } :
}No exceptions in the “handle(..)” method
Invoked from AbstractAggregateRoot
ddd-4-java – CreateVendor: Unit Test
@Testpublic void testConstructor() throws DuplicateVendorKeyException { // PREPARE VendorId id = new VendorId(UUID.randomUUID()); VendorKey key = new VendorKey("V00001"); VendorName name = new VendorName("Peter Parker Inc."); ConstructorService service = new ConstructorService() { public void addVendorKey(VendorKey key) throws DuplicateVendorKeyException { // Do nothing } }; // TEST Vendor vendor = new Vendor(id, key, name, service); // VERIFY List<DomainEvent<?>> events = vendor.getUncommittedChanges(); assertThat(events).hasSize(1); assertThat(events.get(0)).isInstanceOf(VendorCreatedEvent.class); :}
ddd-4-java – To be continued…
TO BE CONTINUED…