Skip to content

Latest commit

 

History

History
129 lines (104 loc) · 5.37 KB

File metadata and controls

129 lines (104 loc) · 5.37 KB

🏛️ Domain Layer

Đây là lõi của ứng dụng - chứa business logic thuần túy, không phụ thuộc vào bất kỳ layer nào khác.

📁 Cấu trúc Folders

Domain/
├── Entities/            # 🏛️ Pure Domain Entities
│   ├── User.cs         # User entity (data + basic behavior)
│   ├── Conversation.cs # Conversation entity
│   └── Message.cs      # Message entity
│
├── Aggregates/          # 🏗️ Aggregate Roots (Business Logic)
│   ├── User/           
│   │   └── UserAggregate.cs      # User business operations
│   └── Conversation/
│       └── ConversationAggregate.cs # Conversation business operations
│
├── Common/              # 🔗 Base Classes & Shared Types
│   ├── BaseEntity.cs    # Base entity với Id, timestamps
│   ├── BaseAggregateRoot.cs # Base cho aggregates + domain events
│   ├── IDomainEvent.cs  # Interface cho domain events
│   ├── Result.cs        # Result pattern cho error handling
│   └── Error.cs         # Error types và codes
│
├── Events/              # 📢 Domain Events
│   ├── User/
│   │   ├── UserCreatedEvent.cs     # Khi user được tạo
│   │   ├── UserUpdatedEvent.cs     # Khi user được cập nhật
│   │   └── UserDeactivatedEvent.cs # Khi user bị vô hiệu hóa
│   ├── Conversation/
│   └── Examples/        # Ví dụ cách sử dụng domain events
│
├── Enums/               # 📋 Domain Enumerations
│   └── UserRole.cs      # Roles: Admin, User, LegalExpert, etc.
│
├── Services/            # 🛠️ Domain Services
│   ├── UserDomainService.cs        # Business logic không thuộc 1 entity
│   └── ConversationDomainService.cs # Logic liên quan conversation
│
├── Specifications/      # 🔍 Specification Pattern
│   ├── ISpecification.cs           # Base interface
│   ├── CompositeSpecifications.cs  # And, Or, Not operations
│   ├── UserSpecifications.cs       # User-specific queries
│   └── ConversationSpecifications.cs
│
└── ValueObjects/        # 💎 Value Objects
    ├── Email.cs         # Email với validation
    └── Password.cs      # Password với hashing rules

🎯 Mục đích từng folder

🏛️ Entities/ - Pure Domain Objects

  • Domain Entities - Objects với identity và basic behavior
  • Data + Basic Logic - Properties và simple methods
  • Navigation Properties - Relationships với other entities
  • Validation - Basic domain validation

Ví dụ: User.HasRole(), Conversation.IsAccessibleBy(), Message.CanBeEditedBy()

🏗️ Aggregates/ - Business Logic Orchestrators

  • Aggregate Roots - Manage business operations và consistency
  • Complex Business Logic - Operations spanning multiple entities
  • Domain Events - Raise events khi business actions occur
  • Invariants - Enforce complex business rules

Ví dụ: UserAggregate.Create(), ConversationAggregate.AddMessage(), UserAggregate.Deactivate()

📢 Events/

  • Domain Events - Thông báo khi có sự kiện quan trọng xảy ra
  • Immutable - Events không thể thay đổi sau khi tạo
  • Past Tense - Named theo sự kiện đã xảy ra

Khi nào dùng: Khi cần trigger side effects hoặc notify other contexts

🛠️ Services/

  • Domain Services - Logic không thuộc về 1 entity cụ thể
  • Coordination - Phối hợp giữa nhiều aggregates
  • Complex Business Rules - Rules phức tạp cần nhiều inputs

Ví dụ: Check permissions, calculate complex business values

🔍 Specifications/

  • Query Logic - Encapsulate complex query conditions
  • Reusable - Có thể combine và reuse
  • Testable - Dễ test business query logic

Ví dụ: ActiveUserSpecification, AdminUserSpecification

💎 ValueObjects/

  • Immutable - Không thể thay đổi sau khi tạo
  • Validation - Built-in validation rules
  • Equality - So sánh theo value, không theo reference

Ví dụ: Email, Money, Address, Phone Number

🚫 Điều KHÔNG ĐƯỢC làm trong Domain

  • Không reference Infrastructure hoặc UI layers
  • Không có database concerns (EF, SQL, etc.)
  • Không có HTTP, JSON, hoặc external service calls
  • Không có logging frameworks (chỉ có abstractions)
  • Không có dependency injection containers

✅ Điều NÊN làm trong Domain

  • Pure business logic - Logic nghiệp vụ thuần túy
  • Domain events - Thông báo business events
  • Validation rules - Business validation
  • Invariants - Business constraints
  • Factory methods - Tạo objects đúng cách

🔗 Dependencies

Domain layer KHÔNG phụ thuộc vào layer nào khác!

Chỉ có thể reference:

  • .NET BCL (Base Class Library)
  • MediatR (chỉ cho INotification interface)

💡 Best Practices

  1. Rich Domain Models - Entities có behavior, không chỉ data
  2. Tell, Don't Ask - Gọi methods thay vì manipulate properties
  3. Aggregate Boundaries - Giữ aggregates nhỏ và focused
  4. Domain Events - Communicate giữa aggregates qua events
  5. Ubiquitous Language - Dùng ngôn ngữ của business domain