-
-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2373 add: Vertical Slice Architecture.
- Loading branch information
1 parent
cb2d794
commit 777a5dc
Showing
30 changed files
with
1,355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Vertical-Slice-Architecture | ||
aka: Layer-By-Feature | ||
category: Architectural | ||
language: en | ||
tag: | ||
- Decoupling | ||
--- | ||
|
||
## Intent | ||
|
||
package the application based on features. Each feature will have its own set of layers ( | ||
Models, Services, Repository and Controllers ). | ||
|
||
## Explanation | ||
|
||
> With vertical slice architecture we can have high cohesion within package and low coupling | ||
> among the packages. In Conceptual term | ||
> Consider that you are going to make a backend service for a online e-commerce application. | ||
> initially you make it with usual grouping of controllers, models etc. but as the application | ||
> grows more it's requires implementation of new features. Let's say that you thought of having | ||
> orders, customers and products associated layers. But now you need to include another set of | ||
> features with Cart system and wishlists. Now it's really hard to integrate those features it | ||
> requires lot's of dependency modifications and mocking. So if you make the package by feature | ||
> it will be really feasible for future additions. General example. | ||
## Class diagram | ||
|
||
![Vertical Slice Architecture](./etc/vertical-slice-architecture.urm.png) | ||
|
||
## Applicability | ||
|
||
Use Vertical Slice Architecture when | ||
|
||
* You want future modification ( new addition of features ). | ||
* You want to reduce the amount of mocking. | ||
* You want to make it more modular by feature. | ||
|
||
## Resources | ||
|
||
* [How to Implement Vertical Slice Architecture by Gary Woodfine](https://garywoodfine.com/implementing-vertical-slice-architecture/) | ||
* [youtube](https://www.youtube.com/watch?v=B1d95I7-zsw) | ||
* [medium](https://medium.com/sahibinden-technology/package-by-layer-vs-package-by-feature-7e89cde2ae3a) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions
125
vertical-slice-architecture/etc/vertical-slice-architecture.urm.puml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
@startuml | ||
package com.iluwatar.vertical-slice-architecture { | ||
|
||
!define ENTITY class | ||
!define SERVICE class | ||
!define REPOSITORY class | ||
!define VIEW class | ||
|
||
package Customer { | ||
ENTITY Customer { | ||
+id: int | ||
name: String | ||
email: String | ||
+getId(): int | ||
+getName(): String | ||
+getEmail(): String | ||
+builder(): Builder | ||
} | ||
|
||
SERVICE CustomerService { | ||
+createCustomer(name: String, email: String): Customer | ||
+getCustomerById(id: int): Customer | ||
+getAllCustomers(): List<Customer> | ||
} | ||
|
||
REPOSITORY CustomerRepository { | ||
+save(customer: Customer): Customer | ||
+findById(id: int): Optional<Customer> | ||
+findAll(): List<Customer> | ||
} | ||
|
||
|
||
VIEW CustomerView { | ||
-customerService: CustomerService | ||
-LOGGER: logger | ||
+render(): void | ||
} | ||
} | ||
|
||
package Product { | ||
ENTITY Product { | ||
+id: int | ||
name: String | ||
price: double | ||
+getId(): int | ||
+getName(): String | ||
+getPrice(): Double | ||
+builder(): Builder | ||
} | ||
|
||
SERVICE ProductService { | ||
+createProduct(name: String, price: double): Product | ||
+getProductById(id: int): Product | ||
+getAllProducts(): List<Product> | ||
} | ||
|
||
REPOSITORY ProductRepository { | ||
+save(product: Product): Product | ||
+findById(id: int): Optional<Product> | ||
+findAll(): List<Product> | ||
} | ||
|
||
|
||
VIEW ProductView { | ||
-productService: ProductService | ||
-LOGGER: logger | ||
+render(): void | ||
} | ||
} | ||
|
||
package Order { | ||
ENTITY Orders { | ||
+id: int | ||
customer: Customer | ||
product: Product | ||
+getId(): int | ||
+getCustomer(): Customer | ||
+getProduct(): Product | ||
+builder(): Builder | ||
} | ||
|
||
SERVICE OrderService { | ||
+createOrder(customer: Customer, product: Product): void | ||
+getOrderById(id: int): Orders | ||
+getOrdersByCustomer(customer: Customer): List<Orders> | ||
} | ||
|
||
REPOSITORY OrderRepository { | ||
+save(order: Orders): Orders | ||
+findById(id: int): Optional<Orders> | ||
+findByCustomer(customer: Customer): List<Orders> | ||
} | ||
|
||
|
||
VIEW OrderView { | ||
-orderService: OrderService | ||
-LOGGER: logger | ||
+render(customer: Customer): void | ||
+showAllOrders(orders: List<Orders>): void | ||
} | ||
} | ||
|
||
class App { | ||
+initializeData(): void | ||
+run(): void | ||
} | ||
|
||
Customer.Customer --> Customer.CustomerService | ||
Customer.CustomerService --> Customer.CustomerRepository | ||
Customer.CustomerService --> Customer.CustomerView | ||
|
||
Product.Product --> Product.ProductService | ||
Product.ProductService --> Product.ProductRepository | ||
Product.ProductService --> Product.ProductView | ||
|
||
Order.Orders --> Order.OrderService | ||
Order.OrderService --> Order.OrderRepository | ||
Order.OrderService --> Order.OrderView | ||
|
||
App --> Customer.CustomerService | ||
App --> Product.ProductService | ||
App --> Order.OrderService | ||
|
||
} | ||
@enduml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
The MIT License | ||
Copyright © 2014-2022 Ilkka Seppälä | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
--> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>vertical-slice-architecture</artifactId> | ||
<parent> | ||
<groupId>com.iluwatar</groupId> | ||
<artifactId>java-design-patterns</artifactId> | ||
<version>1.26.0-SNAPSHOT</version> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<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> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>repackage</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>com.iluwatar.vertical.slice.architecture.App</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
41 changes: 41 additions & 0 deletions
41
vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
* | ||
* The MIT License | ||
* Copyright © 2014-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.iluwatar.verticalslicearchitecture; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* main application. | ||
*/ | ||
|
||
@SpringBootApplication | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(App.class, args); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/Runner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). | ||
* | ||
* The MIT License | ||
* Copyright © 2014-2022 Ilkka Seppälä | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.iluwatar.verticalslicearchitecture; | ||
|
||
import com.iluwatar.verticalslicearchitecture.customer.Customer; | ||
import com.iluwatar.verticalslicearchitecture.customer.CustomerService; | ||
import com.iluwatar.verticalslicearchitecture.customer.CustomerView; | ||
import com.iluwatar.verticalslicearchitecture.order.OrderService; | ||
import com.iluwatar.verticalslicearchitecture.order.OrderView; | ||
import com.iluwatar.verticalslicearchitecture.product.Product; | ||
import com.iluwatar.verticalslicearchitecture.product.ProductService; | ||
import com.iluwatar.verticalslicearchitecture.product.ProductView; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Seeding test data. | ||
*/ | ||
|
||
@Component | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class Runner implements CommandLineRunner { | ||
|
||
CustomerService customerService; | ||
OrderService orderService; | ||
ProductService productService; | ||
|
||
@Override | ||
public void run(String... args) { | ||
initializeData(); | ||
new OrderView(orderService).render(customerService.getCustomerById(1)); | ||
new CustomerView(customerService).render(); | ||
new ProductView(productService).render(); | ||
} | ||
|
||
/** | ||
* method for data seeds. | ||
*/ | ||
|
||
public void initializeData() { | ||
|
||
Customer customer = Customer.builder().id(1).name("sugan0tech").email("[email protected]").build(); | ||
customerService.createCustomer(customer); | ||
|
||
Product oreo = Product.builder().id(1).price(2.00).name("Oreo").build(); | ||
Product cone = Product.builder().id(3).price(1.15).name("Ice Cream Cone").build(); | ||
Product apple = Product.builder().id(4).price(2.00).name("Apple").build(); | ||
Product sandwich = Product.builder().id(2).price(6.00).name("Sandwich").build(); | ||
productService.createProduct(oreo); | ||
productService.createProduct(cone); | ||
productService.createProduct(apple); | ||
productService.createProduct(sandwich); | ||
|
||
orderService.createOrder(1, customer, oreo); | ||
orderService.createOrder(2, customer, sandwich); | ||
orderService.createOrder(3, customer, apple); | ||
} | ||
} |
Oops, something went wrong.