131 lines
2.4 KiB
Markdown
131 lines
2.4 KiB
Markdown
# 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
|
|
|
|
```csharp
|
|
public record CreateUserCommand { }
|
|
// Endpoint: POST /api/command/createUser
|
|
```
|
|
|
|
### Custom Name
|
|
|
|
```csharp
|
|
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.
|
|
|
|
```csharp
|
|
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).
|
|
|
|
```csharp
|
|
[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:
|
|
|
|
```csharp
|
|
[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
|
|
|
|
```csharp
|
|
[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
|
|
|
|
## See Also
|
|
|
|
- [Command Registration](command-registration.md)
|
|
- [Query Attributes](../queries/query-attributes.md)
|
|
- [Metadata Discovery](../../architecture/metadata-discovery.md)
|