Implements external concerns và technical details. Provides concrete implementations cho Application interfaces.
Infrastructure/
├── Data/ # 🗃️ Database & Data Access
│ ├── Contexts/
│ │ ├── DataContext.cs # Main EF DbContext
│ │ ├── IDataContext.cs # DbContext interface
│ │ └── Schemas.cs # Database schema constants
│ │
│ └── Configurations/ # EF Entity Configurations
│ └── BaseEntityConfiguration.cs # Base entity config
│
├── Repositories/ # 📚 Repository Implementations
│ ├── UserRepository.cs # IUserRepository implementation
│ └── BaseRepository.cs # Base repository với common operations
│
├── Services/ # 🛠️ External Service Implementations
│ ├── Email/
│ │ ├── EmailService.cs # IEmailService implementation
│ │ └── EmailOptions.cs # Email configuration
│ │
│ ├── FileStorage/
│ │ ├── FileStorageService.cs # File upload/download
│ │ └── AzureBlobStorage.cs # Azure Blob implementation
│ │
│ ├── PasswordHasher.cs # IPasswordHasher implementation
│ ├── TokenService.cs # ITokenService implementation
│ └── DomainEventDispatcher.cs # Domain event dispatching
│
├── Authentication/ # 🔐 Authentication & Authorization
│ ├── JwtTokenService.cs # JWT token generation/validation
│ ├── PasswordHasher.cs # Password hashing với BCrypt
│ └── AuthenticationOptions.cs # Auth configuration
│
├── Caching/ # 💾 Caching Implementations
│ ├── MemoryCacheService.cs # In-memory caching
│ ├── RedisCacheService.cs # Redis distributed cache
│ └── CacheOptions.cs # Cache configuration
│
├── External/ # 🌐 External API Integrations
│ ├── PaymentGateway/
│ │ ├── StripePaymentService.cs
│ │ └── PaymentOptions.cs
│ │
│ └── LegalDatabase/
│ ├── LegalApiClient.cs
│ └── LegalApiOptions.cs
│
├── Messaging/ # 📨 Message Queues & Events
│ ├── ServiceBus/
│ │ ├── ServiceBusPublisher.cs
│ │ └── ServiceBusOptions.cs
│ │
│ └── EventBus/
│ ├── InMemoryEventBus.cs
│ └── RabbitMQEventBus.cs
│
└── Persistence/ # 📊 Database Specific Concerns
├── Migrations/ # EF Database migrations
│ └── 20240101_InitialMigration.cs
│
└── Seeders/ # Database seed data
├── UserSeeder.cs
└── RoleSeeder.cs
Entity Framework Core setup và configuration
DataContext:
- DbContext chính của application
- Entity configurations
- Database connection management
Configurations:
- EF entity mappings
- Relationship configurations
- Index definitions
- Constraints
Implement Application interfaces cho data access
public class UserRepository : IUserRepository
{
private readonly DataContext _context;
public async Task<User?> GetByEmailAsync(string email)
{
return await _context.Users
.FirstOrDefaultAsync(u => u.Email == email);
}
}Concrete implementations của Application interfaces
Categories:
- Authentication - JWT, OAuth, etc.
- Communication - Email, SMS, Push notifications
- Storage - File upload, cloud storage
- Integration - External APIs, webhooks
Authentication và Authorization mechanics
- JWT Token Service - Create/validate tokens
- Password Hashing - Secure password storage
- Claims Management - User permissions
- Session Management - User sessions
Caching strategies implementation
- Memory Cache - In-process caching
- Distributed Cache - Redis, SQL Server cache
- Cache Policies - TTL, eviction strategies
External service integrations
Examples:
- Payment gateways (Stripe, PayPal)
- Legal databases
- Document services
- Analytics services
- Cloud services (AWS, Azure)
Asynchronous communication
- Service Bus - Azure Service Bus, RabbitMQ
- Event Sourcing - Event store implementation
- Background Jobs - Hangfire, Quartz.NET
- Real-time - SignalR
Database-specific operations
Migrations:
- Schema changes
- Data migrations
- Version control
Seeders:
- Initial data setup
- Test data creation
- Reference data
Infrastructure layer cung cấp service registration extensions:
public static class InfrastructureServiceExtensions
{
public static IServiceCollection AddInfrastructure(
this IServiceCollection services,
IConfiguration configuration)
{
// Database
services.AddDbContext<DataContext>(options =>
options.UseSqlServer(connectionString));
// Repositories
services.AddScoped<IUserRepository, UserRepository>();
// Services
services.AddScoped<IPasswordHasher, PasswordHasher>();
services.AddScoped<ITokenService, JwtTokenService>();
// External Services
services.AddScoped<IEmailService, EmailService>();
return services;
}
}- ❌ No business logic - Business rules thuộc Domain
- ❌ No UI concerns - Presentation logic thuộc Web layer
- ❌ Direct domain manipulation - Always qua Application layer
- ✅ Implement interfaces - Từ Application layer
- ✅ Handle technical concerns - Database, external APIs
- ✅ Configuration management - Options pattern
- ✅ Error handling - Technical error wrapping
- ✅ Performance optimization - Caching, connection pooling
- Application Layer - Interfaces to implement
- Domain Layer - Domain entities và aggregates
- Entity Framework Core - ORM
- External packages - Specific to implementation
- Configuration - Use IOptions pattern
- Connection Management - Proper disposal
- Error Handling - Wrap external exceptions
- Logging - Log all external calls
- Resilience - Retry policies, circuit breakers
- Testing - Integration tests cho repositories
- Security - Secure external communications