Camadas da Arquitetura - ecosif-masterdata
Visão Geral das Camadas
O projeto ecosif-masterdata segue a arquitetura em camadas do Spring Boot, com separação clara de responsabilidades.
Estrutura de Camadas
┌─────────────────────────────────────────────────────────────┐
│ PRESENTATION LAYER │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ Controllers (26 controllers) │ │
│ │ - CompanyController │ │
│ │ - BranchController │ │
│ │ - ChartOfAccountsController │ │
│ │ - DocumentController │ │
│ │ - EntryController │ │
│ │ - ... (e mais 21 controllers) │ │
│ └───────────────────────────────────────────────────────┘ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ Filters │ │
│ │ - TokenAuthenticationFilter │ │
│ └───────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ BUSINESS LAYER │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ Services (63 services) │ │
│ │ - CompanyService / CompanyServiceImpl │ │
│ │ - BranchService / BranchServiceImpl │ │
│ │ - ChartOfAccountsService │ │
│ │ - DocumentService / DocumentServiceImpl │ │
│ │ - EntryService / EntryServiceImpl │ │
│ │ - ConsolidationService │ │
│ │ - ... (e mais 57 services) │ │
│ └───────────────────────────────────────────────────────┘ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ DTOs (40+ DTOs) │ │
│ │ - CompanyDTO │ │
│ │ - BranchDTO │ │
│ │ - ChartOfAccountsDTO │ │
│ │ - DocumentDTO │ │
│ │ - EntryDTO │ │
│ │ - ... (e mais 35 DTOs) │ │
│ └───────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ DATA ACCESS LAYER │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ Repositories (29 repositories) │ │
│ │ - CompanyRepository │ │
│ │ - BranchRepository │ │
│ │ - ChartOfAccountsRepository │ │
│ │ - DocumentRepository │ │
│ │ - EntryRepository │ │
│ │ - ... (e mais 24 repositories) │ │
│ └───────────────────────────────────────────────────────┘ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ Entities (da biblioteca ecosif-database) │ │
│ │ - Company │ │
│ │ - Branch │ │
│ │ - ChartOfAccounts │ │
│ │ - Document │ │
│ │ - Entry │ │
│ │ - ... (e mais 45 entidades) │ │
│ └───────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ INFRASTRUCTURE LAYER │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ Database (PostgreSQL) │ │
│ │ - Schema: public │ │
│ │ - Tables: ~50 tabelas │ │
│ └───────────────────────────────────────────────────────┘ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ Configuration │ │
│ │ - WebSecurityConfig │ │
│ │ - OpenApiConfig │ │
│ │ - GlobalExceptionHandler │ │
│ │ - AppProperties │ │
│ └───────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Detalhamento das Camadas
1. Presentation Layer
Responsável por receber requisições HTTP, validar entradas e retornar respostas.
Características: - 26 controllers REST organizados por funcionalidade - Validação de DTOs usando Bean Validation - Tratamento de erros via GlobalExceptionHandler - Documentação Swagger/OpenAPI
Principais Controllers:
- CompanyController: Empresas
- BranchController: Filiais
- ChartOfAccountsController: Plano de contas
- DocumentController: Documentos
- EntryController: Lançamentos
- BatchController: Lotes
- CalendarController: Calendários
- ConsolidationController: Consolidação
2. Business Layer
Implementa a lógica de negócio do sistema.
Características: - 63 services (interfaces + implementações) - Validações de regras de negócio - Transações gerenciadas - Mapeamento entre entidades e DTOs (ModelMapper)
Principais Services:
- CompanyService: Lógica de empresas
- BranchService: Lógica de filiais
- ChartOfAccountsService: Lógica do plano de contas
- DocumentService: Lógica de documentos
- EntryService: Lógica de lançamentos
- ConsolidationService: Lógica de consolidação
- AccountBalanceService: Cálculos de saldos
3. Data Access Layer
Acesso a dados via Spring Data JPA.
Características: - 29 repositories JPA - Queries customizadas quando necessário - Transações gerenciadas pelo Spring
Principais Repositories:
- CompanyRepository
- BranchRepository
- ChartOfAccountsRepository
- DocumentRepository
- EntryRepository
- BatchRepository
- CalendarRepository
4. Infrastructure Layer
Infraestrutura e configurações.
Características: - PostgreSQL como banco de dados - Configurações de segurança (JWT, OAuth2) - Tratamento global de exceções - Configuração do Swagger/OpenAPI
Fluxo de Dados
Exemplo: Criação de Empresa
1. Cliente → CompanyController
POST /company
{ name, cnpj, zipCode }
2. CompanyController → CompanyService
companyService.save(companyDTO)
3. CompanyService → IntegrationService
integrationService.validateCNPJ(cnpj) [opcional]
4. CompanyService → CompanyRepository
companyRepository.save(company)
5. CompanyRepository → Database
INSERT INTO gr_empresa ...
6. Database → CompanyRepository
Company entity (com ID gerado)
7. CompanyRepository → CompanyService
Company entity
8. CompanyService → ModelMapper
modelMapper.map(company, CompanyDTO.class)
9. CompanyService → CompanyController
CompanyDTO
10. CompanyController → Cliente
201 Created + CompanyDTO
Princípios Aplicados
- Separation of Concerns: Cada camada tem responsabilidades claras
- Dependency Inversion: Camadas superiores dependem de abstrações
- Single Responsibility: Cada classe tem uma única responsabilidade
- DRY: Lógica comum extraída para serviços compartilhados