Skip to content

Onion Architecture #3266

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Sri-Kameswari
Copy link

Pull Request

What does this PR do?

Copy link

github-actions bot commented Apr 26, 2025

PR Summary

This PR implements the Onion Architecture pattern in a Java application. The application manages a TODO list, demonstrating the architecture's layered structure with separation of concerns. The core domain logic is independent of infrastructure concerns, promoting testability and maintainability.

Changes

File Summary
onion-architecture/README.md This file provides a comprehensive guide to Onion Architecture, explaining its principles, benefits, and real-world applications. It includes a detailed Java example and comparisons with other architectural patterns.
onion-architecture/src/main/Main.java This is the main application class. It uses a Scanner to interact with the user, allowing them to add, complete, and view TODO items via a command-line interface.
onion-architecture/src/main/application/TodoService.java This service class handles the application logic for creating and completing TODO items. It interacts with the TodoRepository to persist data.
onion-architecture/src/main/domain/TodoItem.java This class represents a single TODO item, containing its ID, title, and completion status. It's part of the core domain model.
onion-architecture/src/main/domain/TodoRepository.java This interface defines the contract for interacting with TODO item data. It's used by the TodoService to abstract away data storage details.
onion-architecture/src/main/infrastructure/TodoRepositoryImpl.java This class implements the TodoRepository interface, providing a concrete implementation for storing TODO items in memory. It handles persistence.
onion-architecture/src/test/application/TodoServiceTest.java This test class contains unit tests for the TodoService, verifying its core functionality. It uses an in-memory repository for testing purposes.
onion-architecture/src/test/domain/TodoItemTest.java This test class contains unit tests for the TodoItem class, verifying its properties and methods. It ensures the domain model functions correctly.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
Files Processed (8)
  • onion-architecture/README.md (1 hunk)
  • onion-architecture/src/main/Main.java (1 hunk)
  • onion-architecture/src/main/application/TodoService.java (1 hunk)
  • onion-architecture/src/main/domain/TodoItem.java (1 hunk)
  • onion-architecture/src/main/domain/TodoRepository.java (1 hunk)
  • onion-architecture/src/main/infrastructure/TodoRepositoryImpl.java (1 hunk)
  • onion-architecture/src/test/application/TodoServiceTest.java (1 hunk)
  • onion-architecture/src/test/domain/TodoItemTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
E Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Copy link
Owner

@iluwatar iluwatar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addionally, please add onion-architecture module to the parent pom.xml, otherwise it's not build by CI.

import infrastructure.TodoRepositoryImpl;

import java.util.Scanner;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here above the Main, explain the pattern and describe how this code example implements it.

Scanner scanner = new Scanner(System.in);
TodoService service = new TodoService(new TodoRepositoryImpl());

System.out.println("Welcome to the TODO App!");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lombok's @slf4j logging utility

Comment on lines +8 to +12
public TodoItem(int id, String title) {
this.id = id;
this.title = title;
this.isCompleted = false;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Lombok's @AllArgsConstructor

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and @Getter

Comment on lines +9 to +31
public class TodoServiceTest {
public static void main(String[] args) {
TodoRepositoryImpl fakeRepo = new TodoRepositoryImpl();
TodoService service = new TodoService(fakeRepo);

service.createTodo("Learn onion architecture");
service.createTodo("Write unit tests");

List<TodoItem> todos = service.getTodos();

assert todos.size() == 2 : "Should have 2 todos";
assert todos.get(0).getTitle().equals("Learn onion architecture");
assert !todos.get(0).isCompleted();

int idToComplete = todos.get(0).getId();
service.completeTodo(idToComplete);

todos = service.getTodos();
assert todos.get(0).isCompleted() : "First item should be completed";

System.out.println("TodoServiceTest passed");
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like a valid JUnit test. You should mark it with @test annotation.

Comment on lines +5 to +19
public class TodoItemTest {
public static void main(String[] args) {
TodoItem item = new TodoItem(1, "Write tests");

assert item.getId() == 1 : "ID should be 1";
assert item.getTitle().equals("Write tests") : "Title should match";
assert !item.isCompleted() : "Item should not be completed initially";

item.markCompleted();

assert item.isCompleted() : "Item should be marked as completed";

System.out.println("TodoItemTest passed");
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Not a valid test.

System.out.println("\nWhat would you like to do?");
System.out.println("[a] Add tasks");
System.out.println("[d] Mark tasks as done");
System.out.println("[v] View tasks");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add "delete task" function as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants