Home > Java > javaTutorial > body text

Using bounded contexts to build a Java application

Linda Hamilton
Release: 2024-11-07 18:40:03
Original
530 people have browsed it

Using bounded contexts to build a Java application

What are bounded contexts?

A bounded context is one of the core patterns in Domain-Driven Design (DDD). It represents how to divide a large project into domains. This separation allows for flexibility and easier maintenance.

What is a Hexagonal Architecture?

Hexagonal architecture separates the application's core from its external dependencies. It uses ports and adapters to decouple business logic from outside services. Making the business logic independent of frameworks, databases, or user interfaces allows the application to adapt easily to future requirements.

The architecture is made out of three main components:

  1. Business Model: the business rules and core logic. It is completely isolated from external dependencies and only communicates through ports.
  2. Ports: exit and entry to the business model. They separate the core from external layers.
  3. Adapters: translate external interactions (HTTP requests, database operations) into something the core understands. There are in adapters used for incoming communication and out adapters used for outgoing communication.

Why use Hexagonal Architecture?

  • Testability: you can write unit tests for the business logic without mocking databases, external APIs, or frameworks.
  • Maintainability: you can easily swap out dependencies without affecting the core business logic.
  • Scalability: independent scaling of the layers, enhancing overall performance.
  • Flexibility: different external systems can interact with the same core logic.

Building an application using Hexagonal Architecture in Java

This walk-through project uses bounded contexts and hexagonal architecture in Java.

The goal is to create a ticketing system for an amusement park called Techtopia. The project has 3 main bounded contexts: Tickets, Attractions, and Entrance Gates. Each bounded context has its directory and includes components like in and our ports, adapters, use cases, etc.

We will walk through the code process of buying a ticket for the park.

  1. Define the Domain

Create a " domain " directory and include the business logic, free from any framework or external dependency.

Create the "Ticket" entity.

package java.boundedContextA.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@Setter
@ToString
public class Ticket {
    private TicketUUID ticketUUID;
    private LocalDateTime start;
    private LocalDateTime end;
    private double price;
    private TicketAction ticketAction;
    private final Guest.GuestUUID owner;
    private ActivityWindow activityWindow;

    public record TicketUUID(UUID uuid) {
    }

    public Ticket(TicketUUID ticketUUID, Guest.GuestUUID owner) {
        this.ticketUUID = ticketUUID;
        this.owner = owner;
    }

    public Ticket(TicketUUID ticketUUID, LocalDateTime start, LocalDateTime end, double price, TicketAction ticketAction, Guest.GuestUUID owner) {
        this.ticketUUID = ticketUUID;
        this.start = start;
        this.end = end;
        this.price = price;
        this.ticketAction = ticketAction;
        this.owner = owner;
    }

    public Ticket(TicketUUID ticketUUID, LocalDateTime start, LocalDateTime end, double price, Guest.GuestUUID owner, ActivityWindow activityWindow) {
        this.ticketUUID = ticketUUID;
        this.start = start;
        this.end = end;
        this.price = price;
        this.owner = owner;
        this.activityWindow = activityWindow;
    }

    public void addTicketActivity(TicketActivity ticketActivity) {
        this.activityWindow.add(ticketActivity);
    }
}
Copy after login
Copy after login

Moreover, create another domain class named "BuyTicket".

package java.boundedContextA.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@Setter
@ToString
public class Ticket {
    private TicketUUID ticketUUID;
    private LocalDateTime start;
    private LocalDateTime end;
    private double price;
    private TicketAction ticketAction;
    private final Guest.GuestUUID owner;
    private ActivityWindow activityWindow;

    public record TicketUUID(UUID uuid) {
    }

    public Ticket(TicketUUID ticketUUID, Guest.GuestUUID owner) {
        this.ticketUUID = ticketUUID;
        this.owner = owner;
    }

    public Ticket(TicketUUID ticketUUID, LocalDateTime start, LocalDateTime end, double price, TicketAction ticketAction, Guest.GuestUUID owner) {
        this.ticketUUID = ticketUUID;
        this.start = start;
        this.end = end;
        this.price = price;
        this.ticketAction = ticketAction;
        this.owner = owner;
    }

    public Ticket(TicketUUID ticketUUID, LocalDateTime start, LocalDateTime end, double price, Guest.GuestUUID owner, ActivityWindow activityWindow) {
        this.ticketUUID = ticketUUID;
        this.start = start;
        this.end = end;
        this.price = price;
        this.owner = owner;
        this.activityWindow = activityWindow;
    }

    public void addTicketActivity(TicketActivity ticketActivity) {
        this.activityWindow.add(ticketActivity);
    }
}
Copy after login
Copy after login

*BuyTicket * represents the logic for buying a ticket. By making it a separate Spring component, you can isolate the ticket-buying logic in its class, which can evolve independently of other components. This separation improves maintainability and makes the codebase more modular.

  1. Create ports

In the "ports/in" directory you create use cases. Here, we will make the use case where a ticket is bought.

package java.boundedContextA.domain;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.UUID;

@Component
public class BuyTicket {
    public Ticket buyTicket(TicketAction ticketAction, LocalDateTime start, LocalDateTime end, double price, Guest.GuestUUID owner) {
        return new Ticket(new Ticket.TicketUUID(UUID.randomUUID()), start, end, price, ticketAction, owner);
    }
}
Copy after login

Create a record of a ticket to save it.

package java.boundedContextA.ports.in;

public interface BuyingATicketUseCase {
    void buyTicket(BuyTicketsAmountCommand buyTicketsAmountCommand);
}
Copy after login

Next, in the "ports/out" directory you create ports that represent each step of buying said ticket. Create interfaces like "CreateTicketPort", "TicketLoadPort", "TicketUpdatePort".

package java.boundedContextA.ports.in;

import java.boundedContextA.domain.Guest;
import java.boundedContextA.domain.TicketAction;

import java.time.LocalDateTime;

public record BuyTicketsAmountCommand(double price, TicketAction action, LocalDateTime start, LocalDateTime end, Guest.GuestUUID owner) {}
Copy after login
  1. Create port interfaces

In a separate directory, named "core", implement the interface of the buying ticket use case.

package java.boundedContextA.ports.out;

import java.boundedContextA.domain.Ticket;

public interface TicketCreatePort {
    void createTicket(Ticket ticket);
}
Copy after login
  1. Create adapters

In the "adapters/out" directory, create JPA entities of the Ticket to mirror the domain. This is how the application communicates with the database and creates a table of the tickets.

package java.boundedContextA.core;

import java.boundedContextA.domain.BuyTicket;
import java.boundedContextA.ports.in.BuyTicketsAmountCommand;
import java.boundedContextA.ports.in.BuyingATicketUseCase;
import java.boundedContextA.ports.out.TicketCreatePort;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class DefaultBuyingATicketUseCase implements BuyingATicketUseCase {
    final BuyTicket buyTicket;
    private final List<TicketCreatePort> ticketCreatePorts;

    @Override
    public void buyTicket(BuyTicketsAmountCommand buyTicketsAmountCommand) {
        var ticket = buyTicket.buyTicket(buyTicketsAmountCommand.action(), buyTicketsAmountCommand.start(), buyTicketsAmountCommand.end(), buyTicketsAmountCommand.price(), buyTicketsAmountCommand.owner());
        ticketCreatePorts.stream().forEach(ticketCreatedPort -> ticketCreatedPort.createTicket(ticket));
    }
}
Copy after login

Don't forget to create a repository of the entity. This repository will communicate with the service, just like any other architecture.

package java.adapters.out.db;

import java.boundedContextA.domain.TicketAction;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.JdbcTypeCode;

import java.sql.Types;
import java.time.LocalDateTime;
import java.util.UUID;

@Entity
@Table(schema="boundedContextA",name = "boundedContextA.tickets")
@Getter
@Setter
@NoArgsConstructor
public class TicketBoughtJpaEntity {
    @Id
    @JdbcTypeCode(Types.VARCHAR)
    private UUID uuid;

    public TicketBoughtJpaEntity(UUID uuid) {
        this.uuid = uuid;
    }

    @JdbcTypeCode(Types.VARCHAR)
    private UUID owner;

    @Column
    private LocalDateTime start;
    @Column
    private LocalDateTime end;

    @Column
    private double price;
}
Copy after login

In the "adapters/in" directory, create a controller of the Ticket. This application will communicate with external sources.

package java.boundedContextA.adapters.out.db;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
import java.util.UUID;

public interface TicketRepository extends JpaRepository<TicketBoughtJpaEntity, UUID> {
    Optional<TicketBoughtJpaEntity> findByOwner(UUID uuid);
}
Copy after login
  1. Finalize the ticket-buying process

To signify that the ticket was bought, create a record of the event in an "events" directory.

Events represent significant occurrences in the application that are important for the system to communicate to other systems or components. They serve as another way of communicating with the outside about a process that finished, a state that changed, or the need for further action.

package java.boundedContextA.adapters.in;

import java.boundedContextA.ports.in.BuyTicketsAmountCommand;
import java.boundedContextA.ports.in.BuyingATicketUseCase;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class TicketsController {
    private final BuyingATicketUseCase buyingATicketUseCase;

    public TicketsController(BuyingATicketUseCase buyingATicketUseCase) {
        this.buyingATicketUseCase = buyingATicketUseCase;
    }

    @PostMapping("/ticket")
    public void receiveMoney(@RequestBody BuyTicketsAmountCommand command) {
        try {
            buyingATicketUseCase.buyTicket(command);
        } catch (IllegalArgumentException e) {
            System.out.println("An IllegalArgumentException occurred: " + e.getMessage());
        }
    }
}
Copy after login

Don't forget to include a main class to run everything all at once.

package java.boundedContextA.events;

import java.time.LocalDateTime;
import java.util.UUID;

public record TicketIsBoughtEvent(UUID uuid, LocalDateTime start, LocalDateTime end) {
}
Copy after login

**This is a very brief explanation, for a more in-depth code, and how to connect to a React interface, check out this GitHub repository: https://github.com/alexiacismaru/techtopia.

Conclusion

Implementing this architecture in Java involves defining a clean core domain with business logic and interfaces, creating adapters to interact with external systems, and writing everything together while keeping the core isolated.

By following this architecture, your Java applications will be better structured, easier to maintain, and flexible enough to adapt to future changes.

The above is the detailed content of Using bounded contexts to build a Java application. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template