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 2 commits 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 introduces Onion Architecture to the project. It includes the implementation of a TODO app, demonstrating the architecture's principles through a domain layer, application layer, and infrastructure layer. The architecture promotes separation of concerns, 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 explanation with a Java example and diagrams.
onion-architecture/pom.xml This file configures the Maven project for the Onion Architecture example, defining dependencies for logging, testing, and other necessary components.
onion-architecture/src/main/java/com/iluwatar/onionarchitecture/Main.java This is the main application class that demonstrates the Onion Architecture implementation. It provides a command-line interface for interacting with the TODO app.
onion-architecture/src/main/java/com/iluwatar/onionarchitecture/application/TodoService.java This class implements the application layer logic for the TODO app, handling the creation, retrieval, completion, and deletion of TODO items.
onion-architecture/src/main/java/com/iluwatar/onionarchitecture/domain/TodoItem.java This class represents the core domain model for a TODO item, encapsulating its properties and business logic, such as marking it as completed.
onion-architecture/src/main/java/com/iluwatar/onionarchitecture/domain/TodoRepository.java This interface defines the contract for interacting with the TODO item data, abstracting away the specific implementation details.
onion-architecture/src/main/java/com/iluwatar/onionarchitecture/infrastructure/TodoRepositoryImpl.java This class implements the TodoRepository interface, providing a concrete implementation for persisting TODO items using an in-memory list.
onion-architecture/src/test/java/com/iluwatar/onionarchitecture/application/TodoServiceTest.java This file contains unit tests for the TodoService class, verifying its functionality and ensuring the application layer works as expected.
onion-architecture/src/test/java/com/iluwatar/onionarchitecture/domain/TodoItemTest.java This file contains unit tests for the TodoItem class, verifying its functionality and ensuring the domain model works as expected.
pom.xml The onion-architecture module was added to the root pom.xml to include it in the project build.

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?

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 (9)
  • onion-architecture/pom.xml (1 hunk)
  • onion-architecture/src/main/java/com/iluwatar/onionarchitecture/Main.java (1 hunk)
  • onion-architecture/src/main/java/com/iluwatar/onionarchitecture/application/TodoService.java (1 hunk)
  • onion-architecture/src/main/java/com/iluwatar/onionarchitecture/domain/TodoItem.java (1 hunk)
  • onion-architecture/src/main/java/com/iluwatar/onionarchitecture/domain/TodoRepository.java (1 hunk)
  • onion-architecture/src/main/java/com/iluwatar/onionarchitecture/infrastructure/TodoRepositoryImpl.java (1 hunk)
  • onion-architecture/src/test/java/com/iluwatar/onionarchitecture/application/TodoServiceTest.java (1 hunk)
  • onion-architecture/src/test/java/com/iluwatar/onionarchitecture/domain/TodoItemTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

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