├── doc └── images │ └── rhombic.png ├── diamond-demo ├── diamond-order │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── xyz │ │ │ └── zhangyi │ │ │ └── diamond │ │ │ └── demo │ │ │ └── ordercontext │ │ │ ├── domain │ │ │ ├── order │ │ │ │ ├── Product.java │ │ │ │ ├── OrderStatus.java │ │ │ │ ├── OrderId.java │ │ │ │ ├── OrderItem.java │ │ │ │ ├── Order.java │ │ │ │ └── OrderService.java │ │ │ ├── InventoryReview.java │ │ │ ├── exception │ │ │ │ ├── NotEnoughInventoryException.java │ │ │ │ └── OrderException.java │ │ │ └── shoppingcart │ │ │ │ ├── ShoppingCart.java │ │ │ │ └── ShoppingCartService.java │ │ │ ├── package-info.java │ │ │ ├── south │ │ │ ├── adapter │ │ │ │ ├── publishers │ │ │ │ │ └── EventPublisherAdapter.java │ │ │ │ ├── repositories │ │ │ │ │ └── OrderRepositoryAdapter.java │ │ │ │ └── clients │ │ │ │ │ ├── InventoryClientLocalAdapter.java │ │ │ │ │ └── InventoryClientRemoteAdapter.java │ │ │ ├── port │ │ │ │ ├── publishers │ │ │ │ │ └── OrderPlacedEventPublisher.java │ │ │ │ ├── clients │ │ │ │ │ └── InventoryClient.java │ │ │ │ └── repositories │ │ │ │ │ ├── ShoppingCartRepository.java │ │ │ │ │ └── OrderRepository.java │ │ │ └── message │ │ │ │ ├── CheckingInventoryRequest.java │ │ │ │ ├── LockingInventoryRequest.java │ │ │ │ └── InventoryReviewResponse.java │ │ │ └── north │ │ │ ├── message │ │ │ ├── PlacingOrderRequest.java │ │ │ └── OrderPlaced.java │ │ │ ├── remote │ │ │ └── controller │ │ │ │ └── OrderController.java │ │ │ └── local │ │ │ └── appservices │ │ │ └── OrderAppService.java │ └── pom.xml ├── diamond-foundation │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── xyz │ │ │ └── zhangyi │ │ │ └── diamond │ │ │ └── demo │ │ │ └── foundation │ │ │ ├── stereotype │ │ │ ├── Direction.java │ │ │ ├── SubDomain.java │ │ │ ├── MessageContract.java │ │ │ ├── PortType.java │ │ │ ├── RemoteType.java │ │ │ ├── Local.java │ │ │ ├── Aggregate.java │ │ │ ├── DomainService.java │ │ │ ├── Port.java │ │ │ ├── Adapter.java │ │ │ ├── Remote.java │ │ │ └── BoundedContext.java │ │ │ ├── model │ │ │ └── message │ │ │ │ ├── Event.java │ │ │ │ └── ApplicationEvent.java │ │ │ ├── exception │ │ │ ├── ApplicationException.java │ │ │ └── DomainException.java │ │ │ └── package-info.java │ └── pom.xml ├── diamond-customer │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── xyz │ │ │ └── zhangyi │ │ │ └── diamond │ │ │ └── demo │ │ │ └── customercontext │ │ │ ├── domain │ │ │ ├── Customer.java │ │ │ ├── exception │ │ │ │ └── CustomerNotFoundException.java │ │ │ └── CustomerService.java │ │ │ ├── package-info.java │ │ │ ├── north │ │ │ ├── message │ │ │ │ └── CustomerResponse.java │ │ │ ├── local │ │ │ │ └── appservice │ │ │ │ │ └── CustomerAppService.java │ │ │ └── remote │ │ │ │ └── resource │ │ │ │ └── CustomerResource.java │ │ │ └── south │ │ │ └── port │ │ │ └── repository │ │ │ └── CustomerRepository.java │ └── pom.xml ├── diamond-notification │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── xyz │ │ │ └── zhangyi │ │ │ └── diamond │ │ │ └── demo │ │ │ └── notificationcontext │ │ │ ├── domain │ │ │ ├── Notified.java │ │ │ ├── Notification.java │ │ │ ├── Customer.java │ │ │ └── NotificationService.java │ │ │ ├── package-info.java │ │ │ ├── north │ │ │ ├── local │ │ │ │ ├── handler │ │ │ │ │ ├── OrderPlacedEventHandler.java │ │ │ │ │ └── OrderCancelledEventHandler.java │ │ │ │ └── appservice │ │ │ │ │ └── NotificationAppService.java │ │ │ ├── message │ │ │ │ ├── OrderCancelled.java │ │ │ │ └── OrderPlaced.java │ │ │ └── remote │ │ │ │ └── subscriber │ │ │ │ └── OrderPlacedEventSubscriber.java │ │ │ └── south │ │ │ ├── port │ │ │ └── client │ │ │ │ ├── SmsClient.java │ │ │ │ └── CustomerClient.java │ │ │ └── message │ │ │ └── CustomerResponse.java │ └── pom.xml ├── diamond-inventory │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── xyz │ │ │ └── zhangyi │ │ │ └── diamond │ │ │ └── demo │ │ │ └── inventorycontext │ │ │ ├── package-info.java │ │ │ ├── domain │ │ │ ├── Availability.java │ │ │ ├── InventoryReview.java │ │ │ ├── Product.java │ │ │ ├── PurchasedProduct.java │ │ │ └── InventoryService.java │ │ │ ├── north │ │ │ ├── message │ │ │ │ ├── CheckingInventoryRequest.java │ │ │ │ └── InventoryReviewResponse.java │ │ │ ├── local │ │ │ │ └── appservice │ │ │ │ │ └── InventoryAppService.java │ │ │ └── remote │ │ │ │ └── resource │ │ │ │ └── InventoryResource.java │ │ │ └── south │ │ │ └── port │ │ │ └── repository │ │ │ └── ProductRepository.java │ └── pom.xml └── pom.xml ├── .gitignore ├── README.md └── LICENSE /doc/images/rhombic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/agiledon/diamond/HEAD/doc/images/rhombic.png -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/order/Product.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.order; 2 | 3 | public class Product { 4 | } 5 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/Direction.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | public enum Direction { 4 | North, 5 | South 6 | } 7 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/SubDomain.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | public enum SubDomain { 4 | Core, Generic, Supporting 5 | } -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/MessageContract.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | 4 | public @interface MessageContract { 5 | Direction value(); 6 | } 7 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/PortType.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | public enum PortType { 4 | Repository, 5 | Client, 6 | Publisher 7 | } 8 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/RemoteType.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | public enum RemoteType { 4 | Resource, 5 | Controller, 6 | Provider, 7 | Subscriber 8 | } 9 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/InventoryReview.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain; 2 | 3 | public class InventoryReview { 4 | public boolean isAvailable() { 5 | return false; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/order/OrderStatus.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.order; 2 | 3 | public enum OrderStatus { 4 | New, Registered, Granted, Shipped, Cancelled, Invoiced, Closed 5 | } 6 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.customercontext.domain; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Aggregate; 4 | 5 | @Aggregate 6 | public class Customer { 7 | } 8 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/model/message/Event.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.model.message; 2 | 3 | import java.io.Serializable; 4 | 5 | public interface Event extends Serializable { 6 | String eventId(); 7 | } 8 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/exception/NotEnoughInventoryException.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.exception; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.exception.DomainException; 4 | 5 | public class NotEnoughInventoryException extends DomainException { 6 | } 7 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/domain/exception/CustomerNotFoundException.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.customercontext.domain.exception; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.exception.DomainException; 4 | 5 | public class CustomerNotFoundException extends DomainException { 6 | } 7 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/package-info.java: -------------------------------------------------------------------------------- 1 | 2 | @BoundedContext(name = "order", subDomain = SubDomain.Core) 3 | package xyz.zhangyi.diamond.demo.ordercontext; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.BoundedContext; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.SubDomain; -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/exception/ApplicationException.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.exception; 2 | 3 | public class ApplicationException extends RuntimeException { 4 | public ApplicationException(String message, DomainException ex) { 5 | super(message, ex); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/package-info.java: -------------------------------------------------------------------------------- 1 | 2 | @BoundedContext(name = "foundation", subDomain = SubDomain.Generic) 3 | package xyz.zhangyi.diamond.demo.foundation; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.BoundedContext; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.SubDomain; -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/package-info.java: -------------------------------------------------------------------------------- 1 | 2 | @BoundedContext(name = "customer", subDomain = SubDomain.Core) 3 | package xyz.zhangyi.diamond.demo.customercontext; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.BoundedContext; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.SubDomain; -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/domain/Notified.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.domain; 2 | 3 | public class Notified { 4 | public String id() { 5 | return null; 6 | } 7 | 8 | public String phoneNumber() { 9 | return null; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/package-info.java: -------------------------------------------------------------------------------- 1 | 2 | @BoundedContext(name = "inventory", subDomain = SubDomain.Core) 3 | package xyz.zhangyi.diamond.demo.inventorycontext; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.BoundedContext; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.SubDomain; -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/package-info.java: -------------------------------------------------------------------------------- 1 | 2 | @BoundedContext(name = "notification", subDomain = SubDomain.Supporting) 3 | package xyz.zhangyi.diamond.demo.notificationcontext; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.BoundedContext; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.SubDomain; -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/north/local/handler/OrderPlacedEventHandler.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.north.local.handler; 2 | 3 | import xyz.zhangyi.diamond.demo.notificationcontext.north.message.OrderPlaced; 4 | 5 | public interface OrderPlacedEventHandler { 6 | void handle(OrderPlaced event); 7 | } 8 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/north/local/handler/OrderCancelledEventHandler.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.north.local.handler; 2 | 3 | import xyz.zhangyi.diamond.demo.notificationcontext.north.message.OrderCancelled; 4 | 5 | public interface OrderCancelledEventHandler { 6 | void handle(OrderCancelled event); 7 | } 8 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/order/OrderId.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.order; 2 | 3 | public class OrderId { 4 | private String value; 5 | 6 | public OrderId(String value) { 7 | this.value = value; 8 | } 9 | 10 | public String value() { 11 | return this.value; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/adapter/publishers/EventPublisherAdapter.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.adapter.publishers; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Adapter; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 5 | 6 | @Adapter(PortType.Publisher) 7 | public class EventPublisherAdapter { 8 | } 9 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/domain/Notification.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.domain; 2 | 3 | public class Notification { 4 | public Notified to() { 5 | return null; 6 | } 7 | 8 | public void filledWith(Customer customer) { 9 | } 10 | 11 | public String content() { 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/Local.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface Local { 11 | } 12 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/domain/Availability.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.domain; 2 | 3 | public class Availability { 4 | private String productId; 5 | private boolean isAvailable; 6 | 7 | public Availability(String productId, boolean isAvailable) { 8 | this.productId = productId; 9 | this.isAvailable = isAvailable; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.domain; 2 | 3 | public class Customer { 4 | private String customerId; 5 | private String phoneNumber; 6 | 7 | public Customer(String customerId, String phoneNumber) { 8 | this.customerId = customerId; 9 | this.phoneNumber = phoneNumber; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | .idea 26 | 27 | .DS_Store 28 | 29 | *.iml 30 | 31 | target/ -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/Aggregate.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface Aggregate { 11 | } 12 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/south/port/client/SmsClient.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.south.port.client; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 5 | 6 | @Port(PortType.Client) 7 | public interface SmsClient { 8 | void send(String phoneNumber, String message); 9 | } 10 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/DomainService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface DomainService { 11 | } 12 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/Port.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface Port { 11 | PortType value(); 12 | } 13 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/Adapter.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface Adapter { 11 | PortType value(); 12 | } 13 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/Remote.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface Remote { 11 | RemoteType value(); 12 | } 13 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/shoppingcart/ShoppingCart.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.shoppingcart; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Aggregate; 4 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Product; 5 | 6 | import java.util.List; 7 | 8 | @Aggregate 9 | public class ShoppingCart { 10 | public void removeItems(List purchasedProducts) { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/exception/DomainException.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.exception; 2 | 3 | public class DomainException extends RuntimeException { 4 | public DomainException() { 5 | super(); 6 | } 7 | 8 | public DomainException(String message) { 9 | super(message); 10 | } 11 | 12 | public DomainException(String message, Throwable cause) { 13 | super(message, cause); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/north/message/OrderCancelled.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.north.message; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.model.message.ApplicationEvent; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 6 | 7 | @MessageContract(Direction.North) 8 | public class OrderCancelled extends ApplicationEvent { 9 | } 10 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/south/port/client/CustomerClient.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.south.port.client; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 5 | import xyz.zhangyi.diamond.demo.notificationcontext.domain.Customer; 6 | 7 | @Port(PortType.Client) 8 | public interface CustomerClient { 9 | Customer customerOf(String customerId); 10 | } 11 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/port/publishers/OrderPlacedEventPublisher.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.port.publishers; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 5 | import xyz.zhangyi.diamond.demo.ordercontext.north.message.OrderPlaced; 6 | 7 | @Port(PortType.Publisher) 8 | public interface OrderPlacedEventPublisher { 9 | void publish(OrderPlaced orderPlaced); 10 | } 11 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/stereotype/BoundedContext.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.stereotype; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.PACKAGE) 9 | @Retention(RetentionPolicy.SOURCE) 10 | public @interface BoundedContext { 11 | public String name(); 12 | public SubDomain subDomain() default SubDomain.Core; 13 | } -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/south/message/CustomerResponse.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.south.message; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 5 | import xyz.zhangyi.diamond.demo.notificationcontext.domain.Customer; 6 | 7 | @MessageContract(Direction.South) 8 | public class CustomerResponse { 9 | public Customer to() { 10 | return null; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/domain/InventoryReview.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.domain; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class InventoryReview { 7 | private List availabilities; 8 | 9 | public InventoryReview(List availabilities) { 10 | if (availabilities == null) { 11 | availabilities = new ArrayList<>(); 12 | } 13 | this.availabilities = availabilities; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/north/message/CustomerResponse.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.customercontext.north.message; 2 | 3 | import xyz.zhangyi.diamond.demo.customercontext.domain.Customer; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 6 | 7 | @MessageContract(Direction.North) 8 | public class CustomerResponse { 9 | public static CustomerResponse from(Customer customer) { 10 | return null; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/order/OrderItem.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.order; 2 | 3 | public class OrderItem { 4 | private String id; 5 | private Product purchasedProduct; 6 | 7 | public OrderItem(String id, Product purchasedProduct) { 8 | this.id = id; 9 | this.purchasedProduct = purchasedProduct; 10 | } 11 | 12 | public String id() { 13 | return id; 14 | } 15 | 16 | public Product purchased() { 17 | return purchasedProduct; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | diamond-demo 7 | xyz.zhangyi.diamond 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | diamond-foundation 13 | 14 | 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/port/clients/InventoryClient.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.port.clients; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 5 | import xyz.zhangyi.diamond.demo.ordercontext.domain.InventoryReview; 6 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 7 | 8 | @Port(PortType.Client) 9 | public interface InventoryClient { 10 | InventoryReview check(Order order); 11 | void lock(Order order); 12 | } 13 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/exception/OrderException.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.exception; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.exception.DomainException; 4 | 5 | public class OrderException extends DomainException { 6 | public OrderException() { 7 | super("Order Exception"); 8 | } 9 | 10 | public OrderException(String message) { 11 | super(message); 12 | } 13 | 14 | public OrderException(String message, Throwable cause) { 15 | super(message, cause); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/north/message/PlacingOrderRequest.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.north.message; 2 | 3 | import java.io.Serializable; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 7 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 8 | 9 | @MessageContract(Direction.North) 10 | public class PlacingOrderRequest implements Serializable { 11 | public Order to() { 12 | return new Order(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/north/message/CheckingInventoryRequest.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.north.message; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 5 | import xyz.zhangyi.diamond.demo.inventorycontext.domain.PurchasedProduct; 6 | 7 | import java.util.List; 8 | 9 | @MessageContract(Direction.North) 10 | public class CheckingInventoryRequest { 11 | public List to() { 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/south/port/repository/ProductRepository.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.south.port.repository; 2 | 3 | import org.springframework.stereotype.Repository; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 6 | import xyz.zhangyi.diamond.demo.inventorycontext.domain.Product; 7 | 8 | import java.util.List; 9 | 10 | @Repository 11 | @Port(PortType.Repository) 12 | public interface ProductRepository { 13 | List productsOf(List productIds); 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/south/port/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.customercontext.south.port.repository; 2 | 3 | import org.springframework.stereotype.Repository; 4 | import xyz.zhangyi.diamond.demo.customercontext.domain.Customer; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 7 | 8 | import java.util.Optional; 9 | 10 | @Repository 11 | @Port(PortType.Repository) 12 | public interface CustomerRepository { 13 | Optional customerOf(String customerId); 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/message/CheckingInventoryRequest.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.message; 2 | 3 | import java.io.Serializable; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 7 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 8 | 9 | @MessageContract(Direction.South) 10 | public class CheckingInventoryRequest implements Serializable { 11 | public static CheckingInventoryRequest from(Order order) { 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/message/LockingInventoryRequest.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.message; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 5 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 6 | 7 | import java.io.Serializable; 8 | 9 | @MessageContract(Direction.South) 10 | public class LockingInventoryRequest implements Serializable { 11 | public static LockingInventoryRequest from(Order order) { 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/port/repositories/ShoppingCartRepository.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.port.repositories; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 5 | import xyz.zhangyi.diamond.demo.ordercontext.domain.shoppingcart.ShoppingCart; 6 | 7 | import java.util.Optional; 8 | 9 | @Port(PortType.Repository) 10 | public interface ShoppingCartRepository { 11 | Optional customerShoppingCart(String customerId); 12 | 13 | void save(ShoppingCart shoppingCart); 14 | } 15 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/north/message/OrderPlaced.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.north.message; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.model.message.ApplicationEvent; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 6 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 7 | 8 | @MessageContract(Direction.North) 9 | public class OrderPlaced extends ApplicationEvent { 10 | public static OrderPlaced from(Order order) { 11 | return new OrderPlaced(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/domain/Product.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.domain; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Aggregate; 4 | 5 | import java.util.List; 6 | 7 | @Aggregate 8 | public class Product { 9 | private String productId; 10 | 11 | public Product(String productId) { 12 | this.productId = productId; 13 | } 14 | 15 | public String productId() { 16 | return this.productId; 17 | } 18 | 19 | public Availability checkAvailability(List purchasedProducts) { 20 | return null; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/north/message/InventoryReviewResponse.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.north.message; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 5 | import xyz.zhangyi.diamond.demo.inventorycontext.domain.InventoryReview; 6 | 7 | @MessageContract(Direction.North) 8 | public class InventoryReviewResponse { 9 | public static InventoryReviewResponse from(InventoryReview inventoryReview) { 10 | return null; 11 | } 12 | 13 | public InventoryReview to() { 14 | return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/north/message/OrderPlaced.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.north.message; 2 | 3 | 4 | import xyz.zhangyi.diamond.demo.foundation.model.message.ApplicationEvent; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 7 | import xyz.zhangyi.diamond.demo.notificationcontext.domain.Notification; 8 | 9 | @MessageContract(Direction.North) 10 | public class OrderPlaced extends ApplicationEvent { 11 | private String ownerId; 12 | 13 | public Notification to() { 14 | return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/message/InventoryReviewResponse.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.message; 2 | 3 | import java.io.Serializable; 4 | 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Direction; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.MessageContract; 7 | import xyz.zhangyi.diamond.demo.ordercontext.domain.InventoryReview; 8 | 9 | @MessageContract(Direction.South) 10 | public class InventoryReviewResponse implements Serializable { 11 | public static InventoryReviewResponse from(InventoryReview inventoryReview) { 12 | return null; 13 | } 14 | 15 | public InventoryReview to() { 16 | return null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/port/repositories/OrderRepository.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.port.repositories; 2 | 3 | import org.springframework.stereotype.Repository; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Port; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 6 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 7 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.OrderId; 8 | 9 | import java.util.Optional; 10 | 11 | @Repository 12 | @Port(PortType.Repository) 13 | public interface OrderRepository { 14 | Optional orderOf(OrderId orderId); 15 | void add(Order order); 16 | void save(Order order); 17 | } 18 | -------------------------------------------------------------------------------- /diamond-demo/diamond-foundation/src/main/java/xyz/zhangyi/diamond/demo/foundation/model/message/ApplicationEvent.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.foundation.model.message; 2 | 3 | import java.time.LocalDateTime; 4 | import java.util.UUID; 5 | 6 | public class ApplicationEvent implements Event { 7 | protected final String eventId; 8 | protected final String createdTimestamp; 9 | protected final String version; 10 | 11 | public ApplicationEvent() { 12 | this("v1.0"); 13 | } 14 | 15 | public ApplicationEvent(String version) { 16 | eventId = UUID.randomUUID().toString(); 17 | createdTimestamp = LocalDateTime.now().toString(); 18 | this.version = version; 19 | } 20 | 21 | @Override 22 | public String eventId() { 23 | return eventId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/domain/PurchasedProduct.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.domain; 2 | 3 | public class PurchasedProduct { 4 | private String productId; 5 | private int purchasedQuantity; 6 | private int inventoryQuantity; 7 | 8 | public PurchasedProduct(String productId, int purchasedQuantity, int inventoryQuantity) { 9 | this.productId = productId; 10 | this.purchasedQuantity = purchasedQuantity; 11 | this.inventoryQuantity = inventoryQuantity; 12 | } 13 | 14 | public String productId() { 15 | return this.productId; 16 | } 17 | 18 | public int purchasedQuantity() { 19 | return this.purchasedQuantity; 20 | } 21 | 22 | public int inventoryQuantity() { 23 | return this.inventoryQuantity; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/domain/CustomerService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.customercontext.domain; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import xyz.zhangyi.diamond.demo.customercontext.south.port.repository.CustomerRepository; 6 | import xyz.zhangyi.diamond.demo.customercontext.domain.exception.CustomerNotFoundException; 7 | import xyz.zhangyi.diamond.demo.foundation.stereotype.DomainService; 8 | 9 | import java.util.Optional; 10 | 11 | @Service 12 | @DomainService 13 | public class CustomerService { 14 | @Autowired 15 | private CustomerRepository customerRepository; 16 | 17 | public Customer customerOf(String customerId) { 18 | Optional optCustomer = customerRepository.customerOf(customerId); 19 | return optCustomer.orElseThrow(CustomerNotFoundException::new); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/adapter/repositories/OrderRepositoryAdapter.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.adapter.repositories; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Adapter; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 5 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.OrderId; 6 | import xyz.zhangyi.diamond.demo.ordercontext.south.port.repositories.OrderRepository; 7 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 8 | 9 | import java.util.Optional; 10 | 11 | @Adapter(PortType.Repository) 12 | public class OrderRepositoryAdapter implements OrderRepository { 13 | @Override 14 | public Optional orderOf(OrderId orderId) { 15 | return Optional.empty(); 16 | } 17 | 18 | @Override 19 | public void add(Order order) { 20 | 21 | } 22 | 23 | @Override 24 | public void save(Order order) { 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/domain/NotificationService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.domain; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.DomainService; 6 | import xyz.zhangyi.diamond.demo.notificationcontext.south.port.client.CustomerClient; 7 | import xyz.zhangyi.diamond.demo.notificationcontext.south.port.client.SmsClient; 8 | 9 | @Service 10 | @DomainService 11 | public class NotificationService { 12 | @Autowired 13 | private CustomerClient customerClient; 14 | @Autowired 15 | private SmsClient smsClient; 16 | 17 | public void notify(Notification notification) { 18 | Customer customer = customerClient.customerOf(notification.to().id()); 19 | notification.filledWith(customer); 20 | 21 | smsClient.send(notification.to().phoneNumber(), notification.content()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/north/local/appservice/InventoryAppService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.north.local.appservice; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Local; 6 | import xyz.zhangyi.diamond.demo.inventorycontext.domain.InventoryReview; 7 | import xyz.zhangyi.diamond.demo.inventorycontext.domain.InventoryService; 8 | import xyz.zhangyi.diamond.demo.inventorycontext.north.message.CheckingInventoryRequest; 9 | import xyz.zhangyi.diamond.demo.inventorycontext.north.message.InventoryReviewResponse; 10 | 11 | @Service 12 | @Local 13 | public class InventoryAppService { 14 | @Autowired 15 | private InventoryService inventoryService; 16 | 17 | public InventoryReviewResponse checkInventory(CheckingInventoryRequest request) { 18 | InventoryReview inventoryReview = inventoryService.reviewInventory(request.to()); 19 | return InventoryReviewResponse.from(inventoryReview); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/north/remote/controller/OrderController.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.north.remote.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.PostMapping; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Remote; 8 | import xyz.zhangyi.diamond.demo.foundation.stereotype.RemoteType; 9 | import xyz.zhangyi.diamond.demo.ordercontext.north.local.appservices.OrderAppService; 10 | import xyz.zhangyi.diamond.demo.ordercontext.north.message.PlacingOrderRequest; 11 | 12 | @RestController 13 | @RequestMapping(value="/orders") 14 | @Remote(RemoteType.Controller) 15 | public class OrderController 16 | { 17 | @Autowired 18 | private OrderAppService orderAppService; 19 | 20 | @PostMapping 21 | public void placeOrder(PlacingOrderRequest request) { 22 | // validate request ? 23 | orderAppService.placeOrder(request); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/domain/InventoryService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.domain; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.DomainService; 6 | import xyz.zhangyi.diamond.demo.inventorycontext.south.port.repository.ProductRepository; 7 | 8 | import java.util.List; 9 | import java.util.stream.Collectors; 10 | 11 | @Service 12 | @DomainService 13 | public class InventoryService { 14 | @Autowired 15 | private ProductRepository productRepository; 16 | 17 | public InventoryReview reviewInventory(List purchasedProducts) { 18 | List productIds = purchasedProducts.stream().map(p -> p.productId()).collect(Collectors.toList()); 19 | List products = productRepository.productsOf(productIds); 20 | 21 | List availabilities = products.stream().map(p -> p.checkAvailability(purchasedProducts)).collect(Collectors.toList()); 22 | return new InventoryReview(availabilities); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/north/remote/subscriber/OrderPlacedEventSubscriber.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.north.remote.subscriber; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.kafka.annotation.KafkaListener; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Remote; 7 | import xyz.zhangyi.diamond.demo.foundation.stereotype.RemoteType; 8 | import xyz.zhangyi.diamond.demo.notificationcontext.north.local.handler.OrderPlacedEventHandler; 9 | import xyz.zhangyi.diamond.demo.notificationcontext.north.message.OrderPlaced; 10 | 11 | @Remote(RemoteType.Subscriber) 12 | public class OrderPlacedEventSubscriber { 13 | @Autowired 14 | private OrderPlacedEventHandler eventHandler; 15 | 16 | @KafkaListener(id = "order-placed", clientIdPrefix = "order", topics = {"topic.e-commerce.order"}, containerFactory = "containerFactory") 17 | public void subscribeEvent(String eventData) { 18 | OrderPlaced orderPlaced = JSON.parseObject(eventData, OrderPlaced.class); 19 | eventHandler.handle(orderPlaced); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | diamond-demo 7 | xyz.zhangyi.diamond 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | diamond-customer 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | xyz.zhangyi.diamond 25 | diamond-foundation 26 | ${project.parent.version} 27 | 28 | 29 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/north/local/appservice/CustomerAppService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.customercontext.north.local.appservice; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import xyz.zhangyi.diamond.demo.customercontext.domain.Customer; 6 | import xyz.zhangyi.diamond.demo.customercontext.domain.CustomerService; 7 | import xyz.zhangyi.diamond.demo.customercontext.north.message.CustomerResponse; 8 | import xyz.zhangyi.diamond.demo.foundation.exception.ApplicationException; 9 | import xyz.zhangyi.diamond.demo.foundation.exception.DomainException; 10 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Local; 11 | 12 | @Service 13 | @Local 14 | public class CustomerAppService { 15 | @Autowired 16 | private CustomerService customerService; 17 | 18 | public CustomerResponse customerOf(String customerId) { 19 | try { 20 | Customer customer = customerService.customerOf(customerId); 21 | return CustomerResponse.from(customer); 22 | } catch (DomainException ex) { 23 | throw new ApplicationException("customer is not found", ex); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | diamond-demo 7 | xyz.zhangyi.diamond 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | diamond-notification 13 | 14 | 15 | 16 | org.springframework.kafka 17 | spring-kafka 18 | 2.4.3.RELEASE 19 | 20 | 21 | com.alibaba 22 | fastjson 23 | 1.2.62 24 | 25 | 26 | xyz.zhangyi.diamond 27 | diamond-foundation 28 | ${project.parent.version} 29 | 30 | 31 | -------------------------------------------------------------------------------- /diamond-demo/diamond-notification/src/main/java/xyz/zhangyi/diamond/demo/notificationcontext/north/local/appservice/NotificationAppService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.notificationcontext.north.local.appservice; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Local; 6 | import xyz.zhangyi.diamond.demo.notificationcontext.domain.NotificationService; 7 | import xyz.zhangyi.diamond.demo.notificationcontext.north.local.handler.OrderCancelledEventHandler; 8 | import xyz.zhangyi.diamond.demo.notificationcontext.north.local.handler.OrderPlacedEventHandler; 9 | import xyz.zhangyi.diamond.demo.notificationcontext.north.message.OrderCancelled; 10 | import xyz.zhangyi.diamond.demo.notificationcontext.north.message.OrderPlaced; 11 | 12 | @Service 13 | @Local 14 | public class NotificationAppService implements OrderPlacedEventHandler, OrderCancelledEventHandler { 15 | @Autowired 16 | private NotificationService notificationService; 17 | 18 | @Override 19 | public void handle(OrderPlaced event) { 20 | notificationService.notify(event.to()); 21 | } 22 | 23 | @Override 24 | public void handle(OrderCancelled event) { 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/shoppingcart/ShoppingCartService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.shoppingcart; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import xyz.zhangyi.diamond.demo.foundation.stereotype.DomainService; 5 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Product; 6 | import xyz.zhangyi.diamond.demo.ordercontext.south.port.repositories.ShoppingCartRepository; 7 | 8 | import java.util.List; 9 | import java.util.Optional; 10 | 11 | @DomainService 12 | public class ShoppingCartService { 13 | @Autowired 14 | private ShoppingCartRepository shoppingCartRepo; 15 | 16 | public void removeItems(String customerId, List purchasedProducts) { 17 | ShoppingCart shoppingCart = loadShoppingCart(customerId); 18 | if (shoppingCart == null) return; 19 | 20 | shoppingCart.removeItems(purchasedProducts); 21 | shoppingCartRepo.save(shoppingCart); 22 | } 23 | 24 | private ShoppingCart loadShoppingCart(String customerId) { 25 | Optional optionalShoppingCart = shoppingCartRepo.customerShoppingCart(customerId); 26 | 27 | if (!optionalShoppingCart.isPresent()) { 28 | return null; 29 | } 30 | 31 | ShoppingCart shoppingCart = optionalShoppingCart.get(); 32 | return shoppingCart; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | diamond-demo 7 | xyz.zhangyi.diamond 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | diamond-inventory 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | xyz.zhangyi.diamond 25 | diamond-foundation 26 | ${project.parent.version} 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-maven-plugin 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /diamond-demo/diamond-inventory/src/main/java/xyz/zhangyi/diamond/demo/inventorycontext/north/remote/resource/InventoryResource.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.inventorycontext.north.remote.resource; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.PostMapping; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Remote; 10 | import xyz.zhangyi.diamond.demo.foundation.stereotype.RemoteType; 11 | import xyz.zhangyi.diamond.demo.inventorycontext.north.local.appservice.InventoryAppService; 12 | import xyz.zhangyi.diamond.demo.inventorycontext.north.message.CheckingInventoryRequest; 13 | import xyz.zhangyi.diamond.demo.inventorycontext.north.message.InventoryReviewResponse; 14 | 15 | @RestController 16 | @RequestMapping(value="/inventories") 17 | @Remote(RemoteType.Resource) 18 | public class InventoryResource { 19 | @Autowired 20 | private InventoryAppService inventoryAppService; 21 | 22 | @PostMapping 23 | public ResponseEntity check(CheckingInventoryRequest request) { 24 | InventoryReviewResponse reviewResponse = inventoryAppService.checkInventory(request); 25 | return new ResponseEntity<>(reviewResponse, HttpStatus.OK); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/adapter/clients/InventoryClientLocalAdapter.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.adapter.clients; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Adapter; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 7 | import xyz.zhangyi.diamond.demo.inventorycontext.north.local.appservice.InventoryAppService; 8 | import xyz.zhangyi.diamond.demo.inventorycontext.north.message.CheckingInventoryRequest; 9 | import xyz.zhangyi.diamond.demo.inventorycontext.north.message.InventoryReviewResponse; 10 | import xyz.zhangyi.diamond.demo.ordercontext.domain.InventoryReview; 11 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 12 | import xyz.zhangyi.diamond.demo.ordercontext.south.port.clients.InventoryClient; 13 | 14 | @Component 15 | @Adapter(PortType.Client) 16 | public class InventoryClientLocalAdapter implements InventoryClient { 17 | 18 | @Autowired 19 | private InventoryAppService appService; 20 | 21 | @Override 22 | public InventoryReview check(Order order) { 23 | CheckingInventoryRequest checkingInventoryRequest = new CheckingInventoryRequest(); 24 | InventoryReviewResponse inventoryReviewResponse = appService.checkInventory(checkingInventoryRequest); 25 | 26 | return null; 27 | } 28 | 29 | @Override 30 | public void lock(Order order) { 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /diamond-demo/diamond-customer/src/main/java/xyz/zhangyi/diamond/demo/customercontext/north/remote/resource/CustomerResource.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.customercontext.north.remote.resource; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | import xyz.zhangyi.diamond.demo.customercontext.north.message.CustomerResponse; 11 | import xyz.zhangyi.diamond.demo.customercontext.north.local.appservice.CustomerAppService; 12 | import xyz.zhangyi.diamond.demo.foundation.exception.ApplicationException; 13 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Remote; 14 | import xyz.zhangyi.diamond.demo.foundation.stereotype.RemoteType; 15 | 16 | @RestController 17 | @RequestMapping(value="/customers") 18 | @Remote(RemoteType.Resource) 19 | public class CustomerResource { 20 | @Autowired 21 | private CustomerAppService customerAppService; 22 | 23 | @GetMapping("/{customerId}") 24 | public ResponseEntity customerOf(@PathVariable String customerId) { 25 | try { 26 | CustomerResponse customerResponse = customerAppService.customerOf(customerId); 27 | return new ResponseEntity<>(customerResponse, HttpStatus.FOUND); 28 | } catch (ApplicationException ex) { 29 | return new ResponseEntity<>(HttpStatus.NOT_FOUND); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | diamond-demo 7 | xyz.zhangyi.diamond 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | diamond-order 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | xyz.zhangyi.diamond 25 | diamond-foundation 26 | ${project.parent.version} 27 | 28 | 29 | xyz.zhangyi.diamond 30 | diamond-inventory 31 | ${project.parent.version} 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/order/Order.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.order; 2 | 3 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Aggregate; 4 | import xyz.zhangyi.diamond.demo.ordercontext.domain.exception.OrderException; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.stream.Collectors; 9 | 10 | @Aggregate 11 | public class Order { 12 | private OrderId id; 13 | private String customerId; 14 | private OrderStatus status; 15 | private List orderItems; 16 | 17 | public Order() { 18 | orderItems = new ArrayList<>(); 19 | } 20 | 21 | public void validate() { 22 | throw new OrderException(String.format("the order with id %s is invalid", id.value())); 23 | } 24 | 25 | public void cancel() { 26 | this.status = OrderStatus.Cancelled; 27 | } 28 | 29 | public void removeItem(String orderItemId) { 30 | if (orderItems.size() == 0) { 31 | return; 32 | } 33 | if (orderItems.size() == 1 && orderItems.get(0).id() == orderItemId) { 34 | throw new OrderException("Can not remove last item."); 35 | } 36 | 37 | while (orderItems.iterator().hasNext()) { 38 | OrderItem nextItem = orderItems.iterator().next(); 39 | if (nextItem.id() == orderItemId) { 40 | orderItems.remove(nextItem); 41 | } 42 | } 43 | } 44 | 45 | public List purchasedProducts() { 46 | return orderItems.stream().map(i -> i.purchased()).collect(Collectors.toList()); 47 | } 48 | 49 | public String customerId() { 50 | return customerId; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rhombic Symmetric Architecture 2 | 3 | The pattern of Rhombic Symmetric Architecture which nickname is "diamond" is based on Clean Architecture provided by Robert Martin, and borrowed some idea from Hexagon and Layered Architecture. This pattern should be recognized as DDD(Domain-Driven Design) pattern which is used in the strategic design phase. It can help us to create the clean architecture on bounded context level. 4 | 5 | Rhombic Symmetric Architecture suggests the whole bounded context should be composed by inner domain layer and outer gateway layers. According to the different direction of invoking request, the outer gateway layers contains the northbound gateway and the southbound gateway. Here is the diagram of this pattern: 6 | ![Rhombic Symmetric Architecture](/doc/images/rhombic.png) 7 | 8 | The components of Rhombic Symmetric Architecture and their descriptions are as below: 9 | * Domain: It contains domain model composed by domain services, aggregates(entities and value objects) and domain events 10 | * Northbound Gateway: It embodies the OHS(Open Host Service) pattern of context mapping, and contains: 11 | * remote: It provides the remote services which can be called across processes via RPC, HTTP, AMOP protocols etc. 12 | * local: In fact, it's an application service in DDD layered architecture. At the same time, it also contains message contracts which demonstrate the Published Language pattern of context mapping. 13 | * Southbound Gateway: It embodies the ACL(Anti-Corruption Layer) pattern of context mapping, and contains: 14 | * port: It is the abstract layer which provides the access ports to the external environments including DB, files, network, and other bounded contexts. 15 | * adapter: It is the implementation of the port. 16 | 17 | 18 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/south/adapter/clients/InventoryClientRemoteAdapter.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.south.adapter.clients; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.web.client.RestTemplate; 6 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Adapter; 7 | import xyz.zhangyi.diamond.demo.foundation.stereotype.PortType; 8 | import xyz.zhangyi.diamond.demo.ordercontext.south.message.CheckingInventoryRequest; 9 | import xyz.zhangyi.diamond.demo.ordercontext.south.message.InventoryReviewResponse; 10 | import xyz.zhangyi.diamond.demo.ordercontext.south.port.clients.InventoryClient; 11 | import xyz.zhangyi.diamond.demo.ordercontext.south.message.LockingInventoryRequest; 12 | import xyz.zhangyi.diamond.demo.ordercontext.domain.InventoryReview; 13 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 14 | 15 | @Component 16 | @Adapter(PortType.Client) 17 | public class InventoryClientRemoteAdapter implements InventoryClient { 18 | private static final String INVENTORIES_RESOURCE_URL = "http://inventory-service/inventories"; 19 | @Autowired 20 | private RestTemplate restTemplate; 21 | 22 | @Override 23 | public InventoryReview check(Order order) { 24 | CheckingInventoryRequest inventoryRequest = CheckingInventoryRequest.from(order); 25 | InventoryReviewResponse reviewResponse = restTemplate.postForObject(INVENTORIES_RESOURCE_URL, inventoryRequest, InventoryReviewResponse.class); 26 | return reviewResponse.to(); 27 | } 28 | 29 | @Override 30 | public void lock(Order order) { 31 | LockingInventoryRequest inventoryRequest = LockingInventoryRequest.from(order); 32 | restTemplate.put(INVENTORIES_RESOURCE_URL, inventoryRequest); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/north/local/appservices/OrderAppService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.north.local.appservices; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | import xyz.zhangyi.diamond.demo.foundation.exception.ApplicationException; 9 | import xyz.zhangyi.diamond.demo.foundation.exception.DomainException; 10 | import xyz.zhangyi.diamond.demo.foundation.stereotype.Local; 11 | import xyz.zhangyi.diamond.demo.ordercontext.south.port.publishers.OrderPlacedEventPublisher; 12 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.Order; 13 | import xyz.zhangyi.diamond.demo.ordercontext.domain.order.OrderService; 14 | import xyz.zhangyi.diamond.demo.ordercontext.north.message.OrderPlaced; 15 | import xyz.zhangyi.diamond.demo.ordercontext.north.message.PlacingOrderRequest; 16 | 17 | @Service 18 | @Local 19 | public class OrderAppService { 20 | @Autowired 21 | private OrderService orderService; 22 | @Autowired 23 | private OrderPlacedEventPublisher orderPlacedEventPublisher; 24 | 25 | private static final Logger logger = LoggerFactory.getLogger(OrderAppService.class); 26 | 27 | @Transactional(rollbackFor = ApplicationException.class) 28 | public void placeOrder(PlacingOrderRequest request) { 29 | try { 30 | Order order = request.to(); 31 | orderService.placeOrder(order); 32 | 33 | OrderPlaced orderPlaced = OrderPlaced.from(order); 34 | orderPlacedEventPublisher.publish(orderPlaced); 35 | } catch (DomainException ex) { 36 | logger.warn(ex.getMessage()); 37 | throw new ApplicationException(ex.getMessage(), ex); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /diamond-demo/diamond-order/src/main/java/xyz/zhangyi/diamond/demo/ordercontext/domain/order/OrderService.java: -------------------------------------------------------------------------------- 1 | package xyz.zhangyi.diamond.demo.ordercontext.domain.order; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import xyz.zhangyi.diamond.demo.foundation.stereotype.DomainService; 6 | import xyz.zhangyi.diamond.demo.ordercontext.domain.InventoryReview; 7 | import xyz.zhangyi.diamond.demo.ordercontext.domain.shoppingcart.ShoppingCartService; 8 | import xyz.zhangyi.diamond.demo.ordercontext.south.port.clients.InventoryClient; 9 | import xyz.zhangyi.diamond.demo.ordercontext.south.port.repositories.OrderRepository; 10 | import xyz.zhangyi.diamond.demo.ordercontext.domain.exception.OrderException; 11 | import xyz.zhangyi.diamond.demo.ordercontext.domain.exception.NotEnoughInventoryException; 12 | 13 | import java.util.Optional; 14 | 15 | @Service 16 | @DomainService 17 | public class OrderService { 18 | @Autowired 19 | private OrderRepository orderRepository; 20 | @Autowired 21 | private InventoryClient inventoryClient; 22 | @Autowired 23 | private ShoppingCartService shoppingCartService; 24 | 25 | public void placeOrder(Order order) { 26 | order.validate(); 27 | 28 | InventoryReview inventoryReview = inventoryClient.check(order); 29 | if (!inventoryReview.isAvailable()) { 30 | throw new NotEnoughInventoryException(); 31 | } 32 | 33 | orderRepository.add(order); 34 | shoppingCartService.removeItems(order.customerId(), order.purchasedProducts()); 35 | inventoryClient.lock(order); 36 | } 37 | 38 | public void cancelOrder(OrderId orderId) { 39 | Order order = loadOrder(orderId); 40 | order.cancel(); 41 | orderRepository.save(order); 42 | } 43 | 44 | public void removeItems(OrderId orderId, String orderItemId) { 45 | Order order = loadOrder(orderId); 46 | order.removeItem(orderItemId); 47 | orderRepository.save(order); 48 | } 49 | 50 | private Order loadOrder(OrderId orderId) { 51 | Optional optOrder = orderRepository.orderOf(orderId); 52 | if (!optOrder.isPresent()) { 53 | throw new OrderException("order is not found"); 54 | } 55 | return optOrder.get(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /diamond-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | xyz.zhangyi.diamond 8 | diamond-demo 9 | pom 10 | 1.0-SNAPSHOT 11 | 12 | 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-dependencies 17 | pom 18 | 2.1.9.RELEASE 19 | import 20 | 21 | 22 | 23 | 24 | 25 | diamond-foundation 26 | diamond-order 27 | diamond-inventory 28 | diamond-notification 29 | diamond-customer 30 | 31 | 32 | 33 | 34 | 1.8 35 | 36 | 5.0.8.RELEASE 37 | 8.0.17 38 | 39 | com.mysql.jdbc.Driver 40 | jdbc:mysql://localhost:3306/diamond?serverTimezone=UTC 41 | sa 42 | 123456 43 | 44 | 28.1-jre 45 | 1.7.5 46 | 47 | UTF-8 48 | 49 | ${java.version} 50 | ${java.version} 51 | 3.0.0 52 | 2.22.1 53 | 3.1 54 | 3.0.1 55 | 2.22.2 56 | 57 | 6.0.4 58 | 59 | 5.2.2.RELEASE 60 | 3.5.3 61 | 2.0.3 62 | 63 | 4.12 64 | 1.10.19 65 | 3.6.2 66 | 67 | 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-starter-web 72 | 73 | 74 | org.springframework.boot 75 | spring-boot-starter-data-jpa 76 | 77 | 78 | org.springframework.boot 79 | spring-boot-starter-test 80 | test 81 | 82 | 83 | org.mybatis.spring.boot 84 | mybatis-spring-boot-starter 85 | 2.1.1 86 | 87 | 88 | org.mybatis 89 | mybatis-typehandlers-jsr310 90 | 1.0.2 91 | 92 | 93 | com.alibaba 94 | druid 95 | 1.1.20 96 | 97 | 98 | 99 | com.google.guava 100 | guava 101 | ${guava.version} 102 | 103 | 104 | org.slf4j 105 | slf4j-api 106 | ${slf4j.version} 107 | 108 | 109 | mysql 110 | mysql-connector-java 111 | ${mysql.driver.version} 112 | 113 | 114 | junit 115 | junit 116 | ${junit.version} 117 | test 118 | 119 | 120 | org.mockito 121 | mockito-all 122 | ${mockito.version} 123 | test 124 | 125 | 126 | org.assertj 127 | assertj-core 128 | ${assertj-core.version} 129 | test 130 | 131 | 132 | 133 | 134 | 135 | org.apache.maven.plugins 136 | maven-compiler-plugin 137 | ${maven-compiler-plugin.version} 138 | 139 | ${maven.compiler.source} 140 | ${maven.compiler.target} 141 | ${project.build.sourceEncoding} 142 | true 143 | 144 | 145 | 146 | org.flywaydb 147 | flyway-maven-plugin 148 | ${flyway.version} 149 | 150 | ${db.driver} 151 | ${db.url} 152 | ${db.username} 153 | ${db.password} 154 | 155 | 156 | 157 | org.apache.maven.plugins 158 | maven-failsafe-plugin 159 | ${maven.failsafe.version} 160 | 161 | 162 | integration-test 163 | 164 | integration-test 165 | verify 166 | 167 | 168 | 169 | 170 | 171 | org.codehaus.mojo 172 | exec-maven-plugin 173 | 1.6.0 174 | 175 | 176 | org.codehaus.mojo 177 | cobertura-maven-plugin 178 | 2.7 179 | 180 | 181 | html 182 | xml 183 | 184 | 185 | 186 | 187 | 188 | 189 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------