Mastering Level 4 Architecture: Implementing Fraud Detection with C4 Class Diagrams

FraudDetectionService class diagram showing relationships with FraudResult, Transaction, and FraudRule.

In the hierarchy of software architecture modeling, the C4 Model provides a structured way to visualize system design at different levels of abstraction. While Levels 1 through 3 focus on containers, components, and their interactions at a high level, Level 4 (often called the “Code Diagram”) dives into the implementation details. This level is crucial for developers as it maps the actual classes, interfaces, and methods that bring the system to life.

This tutorial explores the architecture of a Fraud Detection Service using a Level 4 class diagram. We will analyze the provided PlantUML code and the resulting visual structure to understand how to model complex logic involving repositories, transactions, and business rules.

Understanding the Role of Level 4 Diagrams

A Level 4 diagram is the most granular view in the C4 Model. It corresponds to the actual code structure within your application. It is used to answer questions like:

  • Which specific classes are responsible for calculating risk scores?
  • How does the service interact with external data repositories?
  • What are the inputs and outputs of the core business logic methods?

Unlike higher-level diagrams which might group logic into a generic “Fraud Component,” a Level 4 diagram exposes the internal mechanics, such as the FraudDetectionService class and its dependency on FraudRulesRepository.

Deconstructing the Fraud Detection Architecture

The diagram presented below illustrates the internal structure of a fraud detection engine. It utilizes object-oriented design principles to separate concerns between data handling, rule evaluation, and result aggregation.

1. The Core Service: FraudDetectionService

This is the central orchestrator of the system. It does not implement the logic for calculating risk itself but coordinates the necessary data and rules to do so.

  • Dependencies: It holds references to FraudRulesRepository and TransactionRepository. This indicates a dependency injection pattern, allowing the service to fetch business rules and transaction history without hardcoding them.
  • Public Interface: The checkFraud method is the entry point. It accepts a Transaction object and returns a FraudResult.
  • Private Logic: Methods like calculateRiskScore and isVelocityExceeded are marked as private (indicated by the minus sign -), meaning they are internal helpers used by the public method.

2. The Data Model: Transaction and FraudRule

At the bottom of the hierarchy, we see the fundamental data structures.

  • Transaction: Represents the raw data event (amount, user ID, merchant, timestamp). This is the input payload the service validates.
  • FraudRule: Defines the logic criteria. It contains fields like threshold and isEnabled, and crucially, an evaluate method that determines if a specific transaction violates the rule.

3. The Outcome: FraudResult

After the service processes the transaction, it returns a FraudResult object. This object encapsulates the decision, including whether the transaction is blocked, the calculated risk score, and a list of the specific rules that triggered the block.

Implementing the Diagram with PlantUML

To recreate or understand the diagram shown in the architecture, we use PlantUML. The code below defines the class structure and the relationships between them.

The diagram uses standard UML notation:

  • - denotes private members.
  • + denotes public members.
  • --> denotes a standard dependency or usage relationship.
  • ..> denotes an aggregation or composition relationship (e.g., FraudResult contains a list of FraudRule names).
@startuml
title FraudDetectionService - Class Diagram

class FraudDetectionService {
    - FraudRulesRepository fraudRepo
    - TransactionRepository txnRepo
    + checkFraud(txn: Transaction): FraudResult
    - evaluateRules(txn: Transaction): List<Rule>
    - calculateRiskScore(txn: Transaction): Double
    - isVelocityExceeded(userId: String): Boolean
}

class FraudResult {
    + isBlocked: boolean
    + riskScore: double
    + blockedRules: List<String>
    + getRiskLevel(): RiskLevel
}

class FraudRule {
    + id: Long
    + ruleName: String
    + threshold: Double
    + isEnabled: boolean
    + evaluate(txn: Transaction): boolean
}

class Transaction {
    + id: String
    + amount: BigDecimal
    + userId: String
    + timestamp: DateTime
    + merchantId: String
}

FraudDetectionService --> FraudResult : returns
FraudDetectionService --> FraudRule : uses
FraudDetectionService --> Transaction : validates
FraudResult ..> FraudRule : contains

@enduml

Visualizing Relationships

The diagram highlights three critical relationships that drive the system’s logic:

  1. FraudDetectionService → Transaction (Validates): The service takes raw transaction data and processes it.
  2. FraudDetectionService → FraudRule (Uses): The service iterates through available rules to check against the transaction.
  3. FraudResult → FraudRule (Contains): The output report includes a traceability link back to the specific rules that were triggered, providing auditability.

Best Practices for Level 4 Modeling

Creating Level 4 diagrams manually can be tedious and prone to becoming outdated quickly. Here are best practices for managing them:

  1. Keep it High-Level: Do not diagram every single method. Focus on the core classes and the public APIs that interact with the system. Internal getters and setters are usually noise at the architecture level.
  2. Use Code Engineering: As noted in the context, tools like IntelliJ IDEA or Visual Paradigm can reverse-engineer your actual Java/C# code to generate these diagrams automatically. This ensures the diagram is always in sync with the source code.
  3. Focus on Interfaces: If your code uses interfaces heavily, diagram the interfaces and the implementations rather than just concrete classes, to show the architectural contract.

Conclusion

By utilizing Level 4 diagrams, architects and developers can bridge the gap between high-level design and low-level implementation. The FraudDetectionService example demonstrates a clean separation of concerns: the service manages the flow, the transaction defines the data, the rule defines the logic, and the result defines the outcome. This structure is scalable and maintainable, making it a robust foundation for any financial or security-critical application.