0Pricing
Clean Architecture & Design Patterns in Practice · บทเรียน

การจัดการข้อกังวลที่ครอบคลุมหลายส่วน

นำแนวทางสำหรับการบันทึกล็อก การยืนยันตัวตน และการอนุญาตมาใช้โดยไม่ละเมิดกฎการพึ่งพาหรือทำให้ตรรกะหลักปะปน

การจัดการข้อกังวลที่ครอบคลุมหลายส่วน เป็นบทเรียน Clean Architecture & Design Patterns in Practice ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clean Architecture & Design Patterns in Practice และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Cross-Cutting Concerns?

In software development, cross-cutting concerns are aspects of a system that affect many parts of the application but are not part of its core business logic. Think of them as system-wide services.

  • Logging: Recording events for debugging or auditing.
  • Authentication: Verifying a user's identity.
  • Authorization: Determining what an authenticated user can do.
  • Caching: Storing frequently accessed data for faster retrieval.
  • Transaction Management: Ensuring data consistency across multiple operations.

The Clean Architecture Dilemma

Clean Architecture emphasizes keeping your core business logic (Entities and Use Cases) independent of external frameworks and delivery mechanisms. This is enforced by the Dependency Rule: dependencies must only flow inwards.

The challenge arises because cross-cutting concerns often rely on specific external frameworks (e.g., a logging library, a security framework). How can we implement these concerns without violating the Dependency Rule and coupling our core logic to external details?

Logging Without Pollution

Let's take logging as an example. If a CreateUserUseCase directly calls a logging framework like Log4j or SLF4J, it creates a dependency on that specific framework.

CreateUserUseCase → Log4j

This violates the Dependency Rule because the inner layer (Use Case) would depend on an outer layer (Frameworks/Drivers). Our core logic should not care how logs are written, only that they need to be written.

Defining a Logging Port

To solve this, we introduce an interface in our inner (Use Case) layer. This interface, often called an Output Port, defines the contract for logging. The Use Case depends on this abstraction, not a concrete implementation.

package application.ports;

public interface ILogger {
    void info(String message);
    void error(String message, Throwable t);
}

Implementing the Logger Adapter

The actual logging framework implementation lives in an outer layer (e.g., Infrastructure). This concrete class acts as an Adapter, implementing our ILogger port and delegating to the specific logging library.

Notice how ConsoleLogger depends on ILogger (an inner layer abstraction), respecting the Dependency Rule.

package infrastructure.logging;

import application.ports.ILogger; // Depends on inner layer

public class ConsoleLogger implements ILogger {
    @Override
    public void info(String message) {
        System.out.println("[INFO] " + message);
    }

    @Override
    public void error(String message, Throwable t) {
        System.err.println("[ERROR] " + message + " - " + t.getMessage());
    }
}

Logger in a Use Case

Now, our CreateUserUseCase can depend on the ILogger interface. The concrete ConsoleLogger is injected at runtime, typically by a Dependency Injection (DI) container, but we'll do it manually here for clarity.

package application.usecases;

import application.ports.ILogger;
import infrastructure.logging.ConsoleLogger;

public class CreateUserUseCase {
    private final ILogger logger;

    public CreateUserUseCase(ILogger logger) {
        this.logger = logger;
    }

    public void execute(String username) {
        logger.info("Attempting to create user: " + username);
        // ... business logic to create user ...
        logger.info("User created successfully: " + username);
    }
}

public class Main {
    public static void main(String[] args) {
        // Manual Dependency Injection for demonstration
        ILogger consoleLogger = new ConsoleLogger();
        CreateUserUseCase useCase = new CreateUserUseCase(consoleLogger);
        useCase.execute("Alice");
    }
}

Handling Authentication

Authentication (verifying who a user is) should occur at the application's entry points, not within the core Use Cases. The Use Case should only receive already authenticated user information.

  • Middleware/Filters: In web applications, these intercept requests before they reach controllers.
  • Input Port Decorators: You can wrap a Use Case with a decorator that handles authentication before delegating to the actual Use Case.

This ensures the Use Case remains focused on business logic, free from security framework details.

Authorization with Policies

Authorization (what an authenticated user can do) is often more complex. While basic authorization can also be handled at the entry point, more granular checks might involve the Use Case.

  • Authorization Policies: Define separate objects or services that encapsulate authorization rules.
  • Interceptors/Aspects: Apply these policies around Use Case execution, checking permissions before the core logic runs.
  • The Use Case might depend on an IAuthorizationService interface (another Port) to query specific permissions.

DI for Cross-Cutting Concerns

The overarching principle for handling cross-cutting concerns in Clean Architecture is the Dependency Inversion Principle (DIP).

  • High-level modules (Use Cases) should not depend on low-level modules (frameworks).
  • Both should depend on abstractions (interfaces).
  • Abstractions are defined in the inner layers, and their concrete implementations reside in the outer layers, injected at runtime.

This keeps your core logic clean, testable, and independent of external technologies.

Applying Clean Concerns

Consider a ProcessOrderUseCase that needs to log critical steps. Which approach aligns best with Clean Architecture's Dependency Rule?

Recap: Keeping Core Clean

We've explored strategies to handle cross-cutting concerns like logging, authentication, and authorization within Clean Architecture:

  • Abstract Concerns: Define interfaces (Ports) in inner layers for concerns like logging.
  • Implement Adapters: Provide concrete implementations of these interfaces in outer layers (Adapters).
  • Boundary Handling: Use middleware, decorators, or interceptors at application boundaries for authentication and authorization.
  • Dependency Inversion: This principle is crucial for ensuring your core business logic remains clean, independent, and free from external framework details.

คำถามที่พบบ่อย

บทเรียน “การจัดการข้อกังวลที่ครอบคลุมหลายส่วน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการข้อกังวลที่ครอบคลุมหลายส่วน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clean Architecture & Design Patterns in Practice ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อกังวลที่ครอบคลุมหลายส่วน”

นำแนวทางสำหรับการบันทึกล็อก การยืนยันตัวตน และการอนุญาตมาใช้โดยไม่ละเมิดกฎการพึ่งพาหรือทำให้ตรรกะหลักปะปน คุณปฏิบัติ Clean Architecture & Design Patterns in Practice ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clean Architecture & Design Patterns in Practice หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clean Architecture & Design Patterns in Practice บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการข้อกังวลที่ครอบคลุมหลายส่วน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Clean Architecture & Design Patterns in Practice นี้ได้ไหม

ได้ บทเรียน Clean Architecture & Design Patterns in Practice ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจัดการข้อกังวลที่ครอบคลุมหลายส่วน
  2. สถาปัตยกรรมสะอาดที่ขับเคลื่อนด้วยเหตุการณ์
  3. สถาปัตยกรรมสะอาดในไมโครเซอร์วิส
  4. CQRS ภายในสถาปัตยกรรมสะอาด
← กลับไปที่ Clean Architecture & Design Patterns in Practice