Infrastructure Layerలో Business Logic ఉండదు.
ఈ Layerలో బాహ్య (External) Technologies మరియు Services ఉంటాయి.
ఉదాహరణలు:
- SQL Server
- Entity Framework Core (EF Core)
- RabbitMQ
- Azure Service Bus
- Redis
- Email Service
- Azure Storage
ఈ Layer యొక్క ప్రధాన పని:
- Databaseతో కమ్యూనికేట్ చేయడం
- Message Queueలను నిర్వహించడం
- Email/SMS పంపడం
- Cache నిర్వహించడం
- Cloud Servicesతో అనుసంధానం చేయడం
ఉదాహరణ
AccountRepository
↓
Entity Framework Core
↓
SQL Server
ఇక్కడ:
AccountRepositoryఅనేది Repository Interface యొక్క Implementation.- అది Entity Framework ద్వారా SQL Serverతో మాట్లాడుతుంది.
గమనిక: Business Rules ఏవీ ఇక్కడ ఉండకూడదు.
Domain Events
ఒక ఉదాహరణ చూద్దాం.
ఒక Customer మరొక Accountకు డబ్బు Transfer చేశాడు.
ఇప్పుడు కేవలం Balance Update చేయడం మాత్రమే సరిపోదు.
ఇంకా చేయాల్సిన పనులు:
- Email పంపాలి.
- SMS పంపాలి.
- Push Notification పంపాలి.
- Audit Logలో నమోదు చేయాలి.
- Analytics Update చేయాలి.
అయితే ఇవన్నీ ఒకే Methodలో నేరుగా Call చేస్తే Code చాలా Tight Coupling అవుతుంది.
అందుకే DDDలో Domain Events ఉపయోగిస్తారు.
ఉదాహరణ Event:
MoneyTransferredEvent
Flow:
MoneyTransferredEvent
↓
Notification Service
↓
Email Service
↓
Audit Service
↓
Analytics Service
దీనివల్ల ప్రతి Service స్వతంత్రంగా పనిచేస్తుంది.
దీనినే Loose Coupling అంటారు.
Domain Event ఉదాహరణ
public class MoneyTransferredEvent
{
public Guid FromAccount { get; set; }
public Guid ToAccount { get; set; }
public decimal Amount { get; set; }
}
ఈ Event Raise అయిన తర్వాత ఇతర Services దీనిని Subscribe చేసి తమ పనిని పూర్తి చేస్తాయి.
CQRS (Command Query Responsibility Segregation)
DDDలో తరచుగా ఉపయోగించే ముఖ్యమైన Pattern CQRS.
దీని అర్థం:
Commands మరియు Queries ను వేర్వేరుగా ఉంచడం.
Commands
Commands అంటే Dataను మార్చే Operations.
ఉదాహరణలు:
Deposit
Withdraw
Transfer
ఇవి Databaseలో మార్పులు చేస్తాయి.
Queries
Queries అంటే Dataను మాత్రమే చదవడం.
ఉదాహరణలు:
Get Balance
Get Transactions
Get Customer
ఇవి Database నుండి సమాచారం మాత్రమే తీసుకుంటాయి.
ఎలాంటి మార్పులు చేయవు.
ముఖ్యమైన నియమం
Commands మరియు Queries రెండింటినీ ఒకే Logicలో కలపకూడదు.
వాటిని పూర్తిగా వేరు చేయాలి.
CQRS Architecture
Commands Flow:
API
↓
Command
↓
Handler
↓
Domain
↓
Repository
↓
Database
Queries Flow:
API
↓
Read Database
↓
DTO
ఈ విధానం వల్ల:
- Performance మెరుగుపడుతుంది.
- Scalability పెరుగుతుంది.
- Business Logic స్పష్టంగా ఉంటుంది.
- Read మరియు Write Operations వేర్వేరుగా నిర్వహించవచ్చు.
Project Folder Structure
BankingSystem
│
├── Banking.API
├── Banking.Application
│
├── Banking.Domain
│
├── Banking.Infrastructure
│
├── Banking.Shared
│
└── Banking.Tests
ప్రతి Projectకి ప్రత్యేకమైన బాధ్యత ఉంటుంది.
Domain Project Structure
Domain
│
├── Entities
├── ValueObjects
├── Events
├── Repositories
├── Aggregates
├── Exceptions
├── Enums
ఇక్కడ Business Domainకు సంబంధించిన అన్ని Components ఉంటాయి.
Application Project Structure
Application
│
├── Commands
├── Queries
├── DTOs
├── Interfaces
├── Handlers
ఈ Layer అన్ని Use Casesను నిర్వహిస్తుంది.
Infrastructure Project Structure
Infrastructure
│
├── Persistence
├── Repositories
├── Messaging
├── Azure
├── Email
├── Redis
ఈ Projectలో Database మరియు ఇతర External Services ఉంటాయి.
API Project Structure
Controllers
Program.cs
Middleware
Swagger
Filters
ఇది Client Requestsను స్వీకరించి Application Layerకు పంపుతుంది.
Money Transfer Flow
ఒక Customer Angular Application ద్వారా డబ్బు Transfer చేస్తున్నాడని అనుకుందాం.
Flow ఇలా ఉంటుంది:
Angular
↓
Transfer API
↓
Transfer Command
↓
Command Handler
↓
Account Aggregate
↓
Withdraw()
↓
Deposit()
↓
Raise Domain Event
↓
Repository
↓
SQL Server
↓
Notification
ఈ Flowలో జరిగే దశలు
- Angular నుండి Transfer Request వస్తుంది.
- API ఒక
TransferCommandను సృష్టిస్తుంది. Command Handlerఆ Commandను Process చేస్తుంది.Account AggregateBusiness Rulesను తనిఖీ చేస్తుంది.- Sender Account నుండి
Withdraw()జరుగుతుంది. - Receiver Accountలో
Deposit()జరుగుతుంది. MoneyTransferredEventRaise అవుతుంది.- Repository ద్వారా Changes Databaseలో Save అవుతాయి.
- Notification Service Customerకు సమాచారం పంపుతుంది.
ఈ మొత్తం Flowలో ప్రతి Layer తనకున్న బాధ్యతను మాత్రమే నిర్వహిస్తుంది.
దీని వల్ల Code:
- శుభ్రంగా (Clean)
- నిర్వహించడానికి సులభంగా (Maintainable)
- పరీక్షించడానికి సులభంగా (Testable)
- విస్తరించడానికి అనుకూలంగా (Scalable)
ఉంటుంది.
