Hexagonal architecture: a quick and practical example
This commit is contained in:
parent
592d3e27d7
commit
2e4de12d4f
|
@ -0,0 +1,44 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.0.5.RELEASE</version>
|
||||||
|
<relativePath />
|
||||||
|
</parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>hexagonal-architecture-poc</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>hexagonal-architecture-poc</name>
|
||||||
|
<description>Demo project for Hexagonal Architecture POC</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.hexagonal;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class HexagonalArchitecturePocApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(HexagonalArchitecturePocApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.hexagonal.boundary.input;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.baeldung.hexagonal.core.entities.Order;
|
||||||
|
|
||||||
|
@Service public interface OrderService {
|
||||||
|
Iterable<Order> getOrders();
|
||||||
|
Order createOrder(Double total);
|
||||||
|
boolean registerOrder(Order order);
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.hexagonal.boundary.output;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.baeldung.hexagonal.core.entities.Order;
|
||||||
|
|
||||||
|
@Repository public interface OrderRepository extends CrudRepository<Order, Long>{
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.hexagonal.core.entities;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity @Table(name="Orders") public class Order {
|
||||||
|
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;
|
||||||
|
Double total;
|
||||||
|
|
||||||
|
public Order() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
public Double getTotal() {
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
public void setTotal(Double total) {
|
||||||
|
this.total = total;
|
||||||
|
}
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.hexagonal.core.services;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.baeldung.hexagonal.boundary.input.OrderService;
|
||||||
|
import com.baeldung.hexagonal.boundary.output.OrderRepository;
|
||||||
|
import com.baeldung.hexagonal.core.entities.Order;
|
||||||
|
|
||||||
|
@Service public class OrderServiceImpl implements OrderService {
|
||||||
|
|
||||||
|
@Autowired OrderRepository orderRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Order> getOrders() {
|
||||||
|
return orderRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean registerOrder(Order order) {
|
||||||
|
if (order.getTotal() > 0)
|
||||||
|
orderRepository.save(order);
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Order createOrder(Double total) {
|
||||||
|
Order order = new Order();
|
||||||
|
order.setTotal(total);
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.hexagonal.outside;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.baeldung.hexagonal.boundary.input.OrderService;
|
||||||
|
import com.baeldung.hexagonal.core.entities.Order;
|
||||||
|
|
||||||
|
@RestController public class OrderController {
|
||||||
|
|
||||||
|
@Autowired OrderService orderService;
|
||||||
|
|
||||||
|
@GetMapping(path = "/orders/list")
|
||||||
|
public Iterable<Order> getOrders(){
|
||||||
|
return orderService.getOrders();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(path = "/orders/add")
|
||||||
|
public String placeOrder(@RequestParam Double total) {
|
||||||
|
boolean isPlaced = orderService.registerOrder(orderService.createOrder(total));
|
||||||
|
return isPlaced ? "Ok" : "Nok";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(path = "/orders/add-commission")
|
||||||
|
public String placeCommissionOrder(@RequestParam Double total) {
|
||||||
|
boolean isPlaced = orderService.registerOrder(orderService.createOrder(total*1.05));
|
||||||
|
return isPlaced ? "Ok" : "Nok";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
spring.datasource.url=jdbc:h2:mem:testdb
|
||||||
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
Loading…
Reference in New Issue