Overview
The domain layer is the heart of your application. It contains the core business logic, entities, and rules that define your application’s purpose. This layer is completely independent of any framework, library, or external dependency.The domain layer should never depend on Angular, HttpClient, or any other infrastructure concern. It defines what your application does, not how it does it.
What Belongs in the Domain Layer
The domain layer typically contains:- Entities/Models: Core business objects that represent your domain concepts
- Repository Interfaces: Contracts that define how to interact with data, without implementation details
- Business Rules: Domain-specific validation and logic
- Value Objects: Immutable objects that represent domain concepts
Why Framework-Agnostic?
Keeping the domain layer framework-agnostic provides several benefits:- Portability: Your business logic can be moved to different frameworks or platforms
- Testability: Easy to unit test without mocking framework dependencies
- Clarity: Clear separation of business concerns from technical implementation
- Longevity: Business logic survives framework changes and upgrades
Domain Models
Domain models represent the core entities in your business domain. They should be simple, focusing on the data structure without any behavior that depends on external systems.Usuario Model
Here’s theUsuario entity from the authentication domain:
Location: ~/workspace/source/src/app/usuario/domain/models/usuario.ts:1
usuario.ts
Unique identifier for the user
Username for authentication
User’s password (encrypted in practice)
In a production application, you would never store or return plain-text passwords. This model would typically only contain the hashed password for storage, and authentication flows would handle password comparison securely.
Repository Interfaces
Repository interfaces define the contract for data access without specifying the implementation. They use Observable (from RxJS) to handle asynchronous operations, but this is purely for type safety - the domain layer doesn’t implement these Observables.UsuarioRepository Interface
Location:~/workspace/source/src/app/usuario/domain/repositories/usuario.repository.ts:1
usuario.repository.ts
Why Abstract Classes?
The repository is defined as an abstract class rather than an interface because:- Dependency Injection: Angular’s DI system works better with abstract classes as tokens
- Future Extension: You can add non-abstract helper methods later
- Type Safety: Provides better type checking in some scenarios
Business Rules
While the current Usuario domain is relatively simple, business rules would typically include:- Password complexity requirements
- Username format validation
- Account status checks (active, suspended, etc.)
- Role-based access control
Domain Layer Structure
Key Principles
- Independence
- Contracts
- Business Focus
The domain layer has no dependencies on:
- Angular framework (
@angular/core,@angular/common) - HTTP clients
- Routing
- UI components
Related Documentation
- Application Layer - Learn how use cases orchestrate domain logic
- Infrastructure Layer - See how repository interfaces are implemented
- Clean Architecture Overview - Understanding the full architecture
- User Authentication Feature - Full authentication implementation
