Skip to content

Commit aa36752

Browse files
author
vp
committed
feat(docs): enhance command/query documentation with marker attribute explanations and return type clarifications
1 parent 513d065 commit aa36752

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

docs/features-application-commands-queries.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,26 @@ Add the code generation package to the project that contains the commands and qu
8585
Commands modify state and return `Result<Unit>` or `Result<T>`.
8686

8787
```csharp
88-
[Command]
88+
[Command] // Marker attribute to indicate this is a command
8989
public partial class CustomerCreateCommand
9090
{
91-
public string FirstName { get; init; }
91+
public string FirstName { get; init; } // Properties are defined normally
9292
9393
public string LastName { get; init; }
9494

9595
public string Email { get; init; }
9696

9797
[Handle]
98-
private async Task<Result<CustomerModel>> HandleAsync(
99-
IMapper mapper,
98+
private async Task<Result<Customer>> HandleAsync(
99+
// DI services declared as parameters are resolved automatically
100100
IGenericRepository<Customer> repository,
101101
CancellationToken cancellationToken)
102102
{
103103
var customer = mapper.Map<CustomerCreateCommand, Customer>(this);
104104
await repository.InsertAsync(customer, cancellationToken);
105105

106-
return Success(mapper.Map<Customer, CustomerModel>(customer));
106+
// Returning Success with a value, which will be the Result<Customer> type of the command
107+
return Success(customer);
107108
}
108109
}
109110
```
@@ -134,7 +135,7 @@ public partial class CustomerRenameCommand
134135
For more complex rules, the `[Validate]` marker can be used:
135136

136137
```csharp
137-
[Command]
138+
[Command] // Marker attribute to indicate this is a command
138139
public partial class CustomerImportCommand
139140
{
140141
[ValidateNotEmpty("At least one email address is required.")]
@@ -161,22 +162,23 @@ public partial class CustomerImportCommand
161162
Queries retrieve data and return `Result<T>`.
162163

163164
```csharp
164-
[Query]
165+
[Query] // Marker attribute to indicate this is a query
165166
public partial class CustomerFindOneQuery
166167
{
167168
[ValidateNotEmptyGuid("CustomerId is required.")]
168169
public string CustomerId { get; }
169170

170171
[Handle]
171-
private async Task<Result<CustomerModel>> HandleAsync(
172+
private async Task<Result<Customer>> HandleAsync(
172173
IMapper mapper,
173174
IGenericRepository<Customer> repository,
174175
CancellationToken cancellationToken)
175176
{
176177
var customer = await repository.FindOneAsync(CustomerId, cancellationToken: cancellationToken);
177178

179+
// Returning Success with a value, which will be the Result<Customer> type of the query
178180
return customer != null
179-
? Success(mapper.Map<Customer, CustomerModel>(customer))
181+
? Success(customer)
180182
: Failure($"Customer with ID {CustomerId} was not found.");
181183
}
182184
}
@@ -187,6 +189,7 @@ public partial class CustomerFindOneQuery
187189
Inject and use `IRequester`:
188190

189191
```csharp
192+
// In a controller, service, or any class with DI
190193
var requester = serviceProvider.GetRequiredService<IRequester>();
191194

192195
var command = new CustomerCreateCommand
@@ -196,7 +199,7 @@ var command = new CustomerCreateCommand
196199
Email = "john.doe@example.com"
197200
};
198201

199-
var commandResult = await requester.SendAsync(command);
202+
var commandResult = await requester.SendAsync(command); // Returns Result<Customer>
200203
if (commandResult.IsSuccess)
201204
{
202205
Console.WriteLine($"Created customer: {commandResult.Value.Id}");
@@ -207,7 +210,7 @@ else
207210
}
208211

209212
var query = new CustomerFindOneQuery("some-guid");
210-
var queryResult = await requester.SendAsync(query);
213+
var queryResult = await requester.SendAsync(query); // Returns Result<Customer>
211214
if (queryResult.IsSuccess)
212215
{
213216
Console.WriteLine($"Found customer: {queryResult.Value.FirstName}");

0 commit comments

Comments
 (0)