Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2373 feature add: Vertical Slice Architecture. #3136

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
777a5dc
#2373 add: Vertical Slice Architecture.
sugan0tech Oct 2, 2023
3f1828a
fixed code smell
sugan0tech Oct 3, 2023
1b130b1
App code coverage changes
sugan0tech Oct 3, 2023
272cb87
local variable in var declaration
sugan0tech Oct 3, 2023
c189ae9
added more context with example
sugan0tech Oct 3, 2023
cf06c64
Merge branch 'iluwatar:master' into master
sugan0tech Oct 8, 2023
24d5063
Added better explanation and updated README.md
sugan0tech Oct 17, 2023
4e0c2c2
Merge branch 'master' into master
sugan0tech Oct 23, 2023
0b160cb
Merge branch 'master' into master
sugan0tech Dec 2, 2023
b84112d
Merge branch 'master' into master
sugan0tech Dec 16, 2023
72c9c87
Merge branch 'master' into master
sugan0tech Mar 10, 2024
dda150f
Merge branch 'master' into master
sugan0tech Apr 2, 2024
c52566d
correction updated with pattern template.
sugan0tech Apr 2, 2024
70fde67
Merge remote-tracking branch 'origin/master'
sugan0tech Apr 2, 2024
08b77c5
added file structure.
sugan0tech Apr 3, 2024
e704103
Merge branch 'master' into master
sugan0tech Apr 3, 2024
7e775f3
Merge branch 'master' into master
sugan0tech Jun 5, 2024
6d83016
Merge branch 'iluwatar:master' into master
sugan0tech Oct 20, 2024
0a39bed
Merge branch 'iluwatar:master' into master
sugan0tech Dec 7, 2024
b12e751
fix: align logging dependencies with Spring Boot by excluding incompa…
sugan0tech Dec 7, 2024
b1c2f35
fix: prevent H2 shutdown warnings by disabling DB_CLOSE_ON_EXIT
sugan0tech Dec 7, 2024
a53c5d9
Merge branch 'iluwatar:master' into master
sugan0tech Feb 7, 2025
0dc87ed
Merge branch 'master' into master
sugan0tech Feb 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
<module>component</module>
<module>context-object</module>
<module>optimistic-offline-lock</module>
<module>vertical-slice-architecture</module>
<module>curiously-recurring-template-pattern</module>
<module>microservices-log-aggregation</module>
<module>anti-corruption-layer</module>
Expand Down
84 changes: 84 additions & 0 deletions vertical-slice-architecture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: Vertical Slice Architecture
category: Architectural
language: en
tag:
- Decoupling
---

## Intent

Organize the application according to its features.
Each feature will comprise its distinct set of layers (Models, Services, Repository, and Controllers).

## Explanation

Real-World Examples (Consider E-commerce)

> In the context of an e-commerce application, the concept of vertical slice architecture becomes clear.
> Imagine you're building a backend service for an online store.
> Initially, you may organize it with the typical grouping of controllers, models, and other components.
> As the application grows, the need arises to implement new features.

> For instance, you might have distinct layers for orders, customers, and products. However, as the application
> evolves, you realize the necessity of integrating additional features like a Cart system and wishlists.
> At this point, integrating these new features into the existing structure becomes challenging.
> It demands significant dependency modifications and mocking, which can be time-consuming and error-prone.

> This is where vertical slice architecture proves its value.
> By structuring the application based on features,
> you create self-contained modules that encapsulate all the necessary components
> (Models, Services, Repository, and Controllers) for a particular feature.
> When you need to add new features, you can do so in a more isolated and manageable manner.

In Plain Words

> Vertical slice architecture is like organizing your toolbox.
> Instead of having all your tools mixed together, you group them based on the type of task they perform.
> This way, when you need a specific tool for a particular job,
> you can quickly find it without rummaging through a jumble of items.

> Similarly, in software development, vertical slice architecture involves organizing the codebase based on features.
> Each feature has its own self-contained set of components, making it easier to add, modify, or remove features without disrupting the entire application.

**File structure**
> have a look in the below file structure, as per vertical slice architecture we are grouping model, view and controller per package associated with the feature.

```
- ecommerce
├── customer
│ ├── Customer.java
│ ├── CustomerRepository.java
│ ├── CustomerService.java
│ └── CustomerView.java
├── order
│ ├── Orders.java
│ ├── OrderRepository.java
│ ├── OrderService.java
│ └── OrderView.java
├── product
│ ├── Product.java
│ ├── ProductRepository.java
│ ├── ProductService.java
│ └── ProductView.java
└── App.java
```

## 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)
* [A reference application](https://github.com/sugan0tech/Event-Manager)
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 vertical-slice-architecture/etc/vertical-slice-architecture.urm.puml
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
141 changes: 141 additions & 0 deletions vertical-slice-architecture/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?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>
<!--
Exclude problematic versions from the parent:

The parent POM includes newer versions of SLF4J (2.x) and Logback (1.5.x)
that are not compatible with the Spring Boot 2.7.x logging setup, which
relies on SLF4J 1.7.x and Logback 1.2.x. If these newer versions remain,
they cause runtime errors due to classes like org/slf4j/impl/StaticLoggerBinder
no longer existing where Spring Boot expects them.

By excluding these dependencies, we ensure that our module only uses versions
of SLF4J and Logback that align with the Spring Boot ecosystem. This approach
helps prevent `NoClassDefFoundError` issues and ensures stable, expected
logging behavior within this module.
--> <dependency>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Add Spring Boot starters -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>

<!-- Force recommended SLF4J and Logback versions if you must specify them explicitly -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version> <!-- A version compatible with Spring Boot 2.7.x -->
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version> <!-- A version compatible with Spring Boot 2.7.x -->
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.11</version>
</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>
Loading
Loading