Overview
TheRegisterUserCommand is a CQRS command object that encapsulates the data required to register a new user in the system. It follows the Command pattern, separating the request for user registration from the actual execution logic.
CQRS Pattern
This implementation follows the Command Query Responsibility Segregation (CQRS) pattern:- Commands (like
RegisterUserCommand) represent write operations that change system state - Command Handlers (like
RegisterUserHandler) contain the business logic to execute commands - This separation provides better testability, maintainability, and scalability
RegisterUserCommand Class
Source Code
src/app/usuario/application/commands/register-user.command.ts
Parameters
The username for the new user account. This is passed as a readonly property to ensure immutability.
The password for the new user account. This is passed as a readonly property to ensure immutability.
Properties
The command has two readonly properties that are set via the constructor:usuario: The username stringcontrasena: The password string
RegisterUserHandler
Source Code
src/app/usuario/application/usecases/commandHandlers/register-user.handler.ts
Handler Method
handle(command: RegisterUserCommand): Observable<Usuario>
Executes the user registration command.
Parameters:
command: TheRegisterUserCommandinstance containing user registration data
Observable<Usuario>: An RxJS Observable that emits the createdUsuarioobject
- Receives the command with user credentials
- Delegates to the
UsuarioRepositoryto persist the new user - Returns an Observable of the created user
Dependencies
Repository interface that handles user persistence operations. Injected via Angular’s dependency injection.
Usage Example
Creating and Executing the Command
In an Angular Component
Architecture Flow
Related Types
Usuario Interface
UsuarioRepository Interface
Best Practices
- Immutability: Command properties are readonly to prevent modification after creation
- Single Responsibility: The command only carries data; the handler contains logic
- Dependency Injection: Handler receives repository via Angular DI
- Observable Pattern: Returns Observable for async operations and reactive programming
- Separation of Concerns: Clear separation between command creation and execution
Benefits of This Pattern
- Testability: Easy to unit test handlers independently
- Maintainability: Clear structure and separation of concerns
- Scalability: Can add middleware, validation, or logging without changing core logic
- Reusability: Commands can be reused across different parts of the application
- Type Safety: Strong typing ensures compile-time checks
See Also
- LoginUserQuery - Query for user authentication
- UsuarioRepository - User data persistence interface
