You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/guide/durability/marten/event-sourcing.md
+109-2
Original file line number
Diff line number
Diff line change
@@ -267,7 +267,7 @@ public static void Handle(OrderEventSourcingSample.MarkItemReady command, IEvent
267
267
}
268
268
}
269
269
```
270
-
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Persistence/OrderEventSourcingSample/Alternatives/Signatures.cs#L25-L54'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_markitemreadyhandler_with_explicit_stream'title='Start of snippet'>anchor</a></sup>
270
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Persistence/OrderEventSourcingSample/Alternatives/Signatures.cs#L26-L55'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_markitemreadyhandler_with_explicit_stream'title='Start of snippet'>anchor</a></sup>
271
271
<!-- endSnippet -->
272
272
273
273
Just as in other Wolverine [message handlers](/guide/handlers/), you can use
@@ -365,9 +365,116 @@ public class MarkItemReady
365
365
publicstringItemName { get; init; }
366
366
}
367
367
```
368
-
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Persistence/OrderEventSourcingSample/Alternatives/Signatures.cs#L8-L19'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_markitemready_with_explicit_identity'title='Start of snippet'>anchor</a></sup>
368
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Persistence/OrderEventSourcingSample/Alternatives/Signatures.cs#L9-L20'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_markitemready_with_explicit_identity'title='Start of snippet'>anchor</a></sup>
369
369
<!-- endSnippet -->
370
370
371
371
## Forwarding Events
372
372
373
373
See [Event Forwarding](./event-forwarding) for more information.
374
+
375
+
## Returning the Updated Aggregate <Badgetype="tip"text="3.5" />
376
+
377
+
A common use case for the "aggregate handler workflow" has been to respond with the now updated state of the projected
378
+
aggregate that has just been updated by appending new events. Until now, that's effectively meant making a completely separate
379
+
call to the database through Marten to retrieve the latest updates.
380
+
381
+
::: info
382
+
To understand more about the inner workings of the next section, see the Marten documentation on its [FetchLatest](https://martendb.io/events/projections/read-aggregates.html#fetchlatest)
383
+
API.
384
+
:::
385
+
386
+
As a quick tip for performance, assuming that you are *not* mutating the projected documents within your command
387
+
handlers, you can opt for this significant Marten optimization to eliminate extra database round trips while
388
+
using the aggregate handler workflow:
389
+
390
+
```csharp
391
+
builder.Services.AddMarten(opts=>
392
+
{
393
+
// Other Marten configuration
394
+
395
+
// Use this setting to get the very best performance out
396
+
// of the UpdatedAggregate workflow and aggregate handler
397
+
// workflow over all
398
+
opts.Events.UseIdentityMapForAggregates=true;
399
+
}).IntegrateWithWolverine();
400
+
```
401
+
402
+
::: info
403
+
The setting above cannot be a default in Marten because it can break some existing code with a very different
404
+
workflow that what the Critter Stack team recommends for the aggregate handler workflow.
405
+
:::
406
+
407
+
Wolverine.Marten has a special response type for message handlers or HTTP endpoints we can use as a directive to tell Wolverine
408
+
to respond with the latest state of a projected aggregate as part of the command execution. Let's make this concrete by
409
+
taking the `MarkItemReady` command handler we've used earlier in this guide and building a slightly new version that
// This will return the updated version of the Order
464
+
// aggregate that incorporates whatever events were appended
465
+
// in the course of processing the command
466
+
returnbus.InvokeAsync<Order>(command);
467
+
}
468
+
```
469
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Persistence/OrderEventSourcingSample/Alternatives/Signatures.cs#L103-L113'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_using_updatedaggregate_with_invoke_async'title='Start of snippet'>anchor</a></sup>
470
+
<!-- endSnippet -->
471
+
472
+
Likewise, you can use `UpdatedAggregate` as the response body of an HTTP endpoint with Wolverine.HTTP [as shown here](/guide/http/marten.html#responding-with-the-updated-aggregate~~~~).
473
+
474
+
::: info
475
+
This feature has been more or less requested several times, but was finally brought about because of the need
476
+
to consume Wolverine + Marten commands within Hot Chocolate mutations and always return the current state of
477
+
the projected aggregate being updated to the user interface.
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L153-L174'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_configure_endpoints'title='Start of snippet'>anchor</a></sup>
47
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L157-L178'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_configure_endpoints'title='Start of snippet'>anchor</a></sup>
Copy file name to clipboardexpand all lines: docs/guide/http/marten.md
+42-6
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ public static OrderShipped Ship(ShipOrder2 command, [Aggregate] Order order)
124
124
returnnewOrderShipped();
125
125
}
126
126
```
127
-
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L121-L136'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_aggregate_attribute_1'title='Start of snippet'>anchor</a></sup>
127
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L131-L146'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_aggregate_attribute_1'title='Start of snippet'>anchor</a></sup>
@@ -143,7 +143,7 @@ public static OrderShipped Ship3([Aggregate] Order order)
143
143
returnnewOrderShipped();
144
144
}
145
145
```
146
-
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L138-L150'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_aggregate_attribute_2'title='Start of snippet'>anchor</a></sup>
146
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L148-L160'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_aggregate_attribute_2'title='Start of snippet'>anchor</a></sup>
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L11-L73'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_order_aggregate_for_http'title='Start of snippet'>anchor</a></sup>
239
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L11-L83'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_order_aggregate_for_http'title='Start of snippet'>anchor</a></sup>
@@ -245,7 +255,7 @@ public static OrderShipped Ship(ShipOrder command, Order order)
245
255
returnnewOrderShipped();
246
256
}
247
257
```
248
-
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L106-L119'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_emptyresponse'title='Start of snippet'>anchor</a></sup>
258
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L116-L129'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_using_emptyresponse'title='Start of snippet'>anchor</a></sup>
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L200-L230'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_returning_multiple_events_from_http_endpoint'title='Start of snippet'>anchor</a></sup>
294
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L210-L240'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_returning_multiple_events_from_http_endpoint'title='Start of snippet'>anchor</a></sup>
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Orders.cs#L268-L282'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_returning_updated_aggregate_as_response_from_http_endpoint'title='Start of snippet'>anchor</a></sup>
285
321
<!--endSnippet-->
286
322
287
323
### Compiled Query Resource Writer Policy
@@ -294,7 +330,7 @@ Register it in `WolverineHttpOptions` like this:
294
330
```cs
295
331
opts.UseMartenCompiledQueryResultPolicy();
296
332
```
297
-
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L180-L182'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_user_marten_compiled_query_policy'title='Start of snippet'>anchor</a></sup>
333
+
<sup><ahref='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L184-L186'title='Snippet source file'>snippetsource</a>|<ahref='#snippet-sample_user_marten_compiled_query_policy'title='Start of snippet'>anchor</a></sup>
0 commit comments