2.4 KiB
2.4 KiB
Command Attributes
Control command behavior using attributes.
Overview
Attributes customize how commands are discovered, named, and exposed as endpoints.
[CommandName]
Override the default endpoint name.
Default Naming
public record CreateUserCommand { }
// Endpoint: POST /api/command/createUser
Custom Name
using Svrnty.CQRS.Abstractions;
[CommandName("register")]
public record CreateUserCommand
{
public string Name { get; init; } = string.Empty;
public string Email { get; init; } = string.Empty;
}
// Endpoint: POST /api/command/register
[IgnoreCommand]
Prevent endpoint generation for internal commands.
using Svrnty.CQRS.Abstractions;
[IgnoreCommand]
public record InternalSyncCommand
{
public int BatchId { get; init; }
}
// No endpoint created - internal use only
Use cases:
- Internal commands called from code
- Background job commands
- System commands
- Migration commands
[GrpcIgnore]
Skip gRPC service generation (HTTP only).
[GrpcIgnore]
public record HttpOnlyCommand
{
public string Data { get; init; } = string.Empty;
}
// HTTP: POST /api/command/httpOnly
// gRPC: Not generated
Custom Attributes
Create your own attributes for metadata:
[AttributeUsage(AttributeTargets.Class)]
public class RequiresAdminAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Class)]
public class AuditedCommandAttribute : Attribute
{
public string Category { get; set; } = string.Empty;
}
// Usage
[RequiresAdmin]
[AuditedCommand(Category = "UserManagement")]
public record DeleteUserCommand
{
public int UserId { get; init; }
}
Then check in custom middleware or authorization service.
Attribute Combinations
[CommandName("users/create")]
[AuditedCommand(Category = "Users")]
public record CreateUserCommand { }
[IgnoreCommand]
[GrpcIgnore]
public record InternalCleanupCommand { }
Best Practices
✅ DO
- Use [CommandName] for clearer APIs
- Use [IgnoreCommand] for internal commands
- Document why commands are ignored
- Keep custom attributes simple
❌ DON'T
- Don't overuse custom naming
- Don't create too many custom attributes
- Don't put logic in attributes