Class UseCaseBuilder

java.lang.Object
com.guinetik.hexafun.hexa.UseCaseBuilder

public class UseCaseBuilder extends Object
Builder for creating HexaApp instances with use cases, ports, and adapters.

Supports fluent DSL with implicit closure - each useCase() call automatically closes the previous one.

Example:


 HexaFun.dsl()
     .withPort(TaskRepository.class, new InMemoryTaskRepository())
     .withAdapter(TO_DTO, task -> new TaskDTO(task.id(), task.title()))
     .useCase(Keys.CREATE)
         .validate(validator)
         .handle(handler)
     .useCase(Keys.DELETE)
         .handle(deleteHandler)
     .build();
 
  • Constructor Details

    • UseCaseBuilder

      public UseCaseBuilder()
  • Method Details

    • withPort

      public <T> UseCaseBuilder withPort(Class<T> type, T impl)
      Register a port (output adapter) by its type. Ports are registered when build() is called.

      Example:

      
       HexaFun.dsl()
           .withPort(TaskRepository.class, new InMemoryTaskRepository())
           .withPort(EmailService.class, new SmtpEmailService())
           .useCase(...)
           .build();
       
      Type Parameters:
      T - The port type
      Parameters:
      type - The interface/class type to register
      impl - The implementation instance
      Returns:
      This builder for chaining
    • withAdapter

      public <From, To> UseCaseBuilder withAdapter(AdapterKey<From,To> key, Function<From,To> adapter)
      Register an adapter with a type-safe key. Adapters transform data from one type to another.

      Example:

      
       HexaFun.dsl()
           .withAdapter(TO_INVENTORY, req -> new InventoryCheck(req.itemId()))
           .withAdapter(TO_PAYMENT, req -> new PaymentRequest(req.total()))
           .useCase(...)
           .build();
       
      Type Parameters:
      From - The source type
      To - The target type
      Parameters:
      key - The type-safe adapter key
      adapter - The transformation function
      Returns:
      This builder for chaining
    • useCase

      public <I, O> UseCaseInputStep<I,O> useCase(UseCaseKey<I,O> key)
      Start defining a use case with a type-safe key. Implicitly closes any previous use case definition.
      Type Parameters:
      I - The input type of the use case
      O - The output type of the use case
      Parameters:
      key - The type-safe key for this use case
      Returns:
      A new UseCaseInputStep for chaining
    • build

      public HexaApp build()
      Build a HexaApp with all the registered use cases and ports.
      Returns:
      A new HexaApp instance