Welcome to visit my blog post

This project is a Go microservice framework based on Hexagonal Architecture and Domain-Driven Design. It provides a clear project structure and design patterns to help developers build maintainable, testable, and scalable applications.
Hexagonal Architecture (also known as Ports and Adapters Architecture) divides the application into internal and external parts, implementing Separation of Concerns and Dependency Inversion Principle through well-defined interfaces (ports) and implementations (adapters). This architecture decouples business logic from technical implementation details, facilitating unit testing and feature extension.
.
├── adapter/ # Adapter Layer - External system interactions
│ ├── amqp/ # Message queue adapters
│ ├── dependency/ # Dependency injection configuration
│ │ └── wire.go # Wire DI setup with interface bindings
│ ├── job/ # Scheduled task adapters
│ └── repository/ # Data repository adapters
│ ├── mysql/ # MySQL implementation
│ │ └── entity/ # Database entities and repo implementations
│ ├── postgre/ # PostgreSQL implementation
│ ├── mongo/ # MongoDB implementation
│ └── redis/ # Redis implementation
│ └── enhanced_cache.go # Enhanced cache with advanced features
├── api/ # API Layer - HTTP requests and responses
│ ├── dto/ # Data Transfer Objects for API
│ ├── error_code/ # Error code definitions
│ ├── grpc/ # gRPC API handlers
│ └── http/ # HTTP API handlers
│ ├── handle/ # Request handlers using domain interfaces
│ ├── middleware/ # HTTP middleware
│ ├── paginate/ # Pagination handling
│ └── validator/ # Request validation
├── application/ # Application Layer - Use cases coordinating domain objects
│ ├── core/ # Core interfaces and base implementations
│ │ └── interfaces.go # UseCase and UseCaseHandler interfaces
│ └── example/ # Example use case implementations
│ ├── create_example.go # Create example use case
│ ├── delete_example.go # Delete example use case
│ ├── get_example.go # Get example use case
│ ├── update_example.go # Update example use case
│ └── find_example_by_name.go # Find example by name use case
├── cmd/ # Command-line entry points
│ └── main.go # Main application entry point
├── config/ # Configuration management
│ ├── config.go # Configuration structure and loading
│ └── config.yaml # Configuration file
├── domain/ # Domain Layer - Core business logic
│ ├── aggregate/ # Domain aggregates
│ ├── dto/ # Domain Data Transfer Objects
│ ├── event/ # Domain events
│ ├── model/ # Domain models
│ ├── repo/ # Repository interfaces
│ ├── service/ # Domain services
│ └── vo/ # Value Objects
└── tests/ # Test utilities and examples
├── migrations/ # Database migrations for testing
├── mysql.go # MySQL test utilities
├── postgresql.go # PostgreSQL test utilities
└── redis.go # Redis test utilities
domain/)Independent of external concerns
Application Layer (application/)
Contains no business rules
Adapter Layer (adapter/)
Contains technical details and frameworks
API Layer (api/)
This structure enforces the Hexagonal Architecture principles:
Dependency flows inward, with outer layers depending on inner layers
Dependency Inversion:
All dependencies are injected through interfaces
Domain-Centric Design:
Service interfaces define business operations
Clean Boundaries:
Dependencies point inward toward the domain
Interface Segregation
Prevents interface pollution
Single Responsibility
Focused and maintainable code
Open/Closed
The domain layer is the core of the application, containing business logic and rules. It is independent of other layers and does not depend on any external components.
Example: Example entity, containing basic properties like ID, name, alias, etc.
Repository Interfaces: Define data access interfaces
IExampleRepo: Example repository interface, defining operations like create, read, update, delete, etc.IExampleCacheRepo: Example cache interface, defining health check methodsTransaction: Transaction interface, supporting transaction begin, commit, and rollback
Domain Services: Handle business logic across entities
IExampleService: Service interface defining contracts for example-related operationsExampleService: Implementation of the example service interface, handling business logic for example entities
Domain Events: Define events within the domain
ExampleCreatedEvent: Example creation eventExampleUpdatedEvent: Example update eventExampleDeletedEvent: Example deletion eventAsyncEventBus: Asynchronous event processing with persistenceThe application layer coordinates domain objects to complete specific application tasks. It depends on domain interfaces but not on concrete implementations, following the Dependency Inversion Principle.
CreateExampleUseCase: Create example use caseGetExampleUseCase: Get example use caseUpdateExampleUseCase: Update example use caseDeleteExampleUseCase: Delete example use caseFindExampleByNameUseCase: Find example by name use case
Commands and Queries: Implement CQRS pattern
Each use case defines Input and Output structures, representing command/query inputs and results
Event Handlers: Process domain events
LoggingEventHandler: Logging event handler, recording all eventsExampleEventHandler: Example event handler, processing events related to examplesThe adapter layer implements interaction with external systems, such as databases and message queues.
EntityExample: MySQL implementation of example repositoryNoopTransaction: No-operation transaction implementation, simplifying testingMySQL: MySQL connection and transaction management
-$ claude mcp add go-hexagonal \
-- python -m otcore.mcp_server <graph>