Skip to content

Commit 98b4bc8

Browse files
authored
Legacy docs review: Unit of work sample (#7169)
1 parent e50598a commit 98b4bc8

File tree

8 files changed

+40
-67
lines changed

8 files changed

+40
-67
lines changed

samples/unit-of-work/Core_8/Sample/CustomManageUnitOfWork.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#region CustomManageUnitOfWork
99

10-
public class CustomManageUnitOfWork :
10+
sealed class CustomManageUnitOfWork :
1111
IManageUnitsOfWork
1212
{
1313
static ILog log = LogManager.GetLogger("CustomManageUnitOfWork");
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
using NServiceBus;
22

3-
public class MessageThatWillSucceed :
4-
IMessage
5-
{
6-
7-
}
3+
public record MessageThatWillSucceed : IMessage;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
using NServiceBus;
22

3-
public class MessageThatWillThrow :
4-
IMessage
5-
{
6-
7-
}
3+
public record MessageThatWillThrow : IMessage;

samples/unit-of-work/Core_8/Sample/Program.cs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,25 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using NServiceBus;
55

6-
class Program
7-
{
8-
static async Task Main()
9-
{
10-
Console.Title = "UnitOfWork";
11-
var endpointConfiguration = new EndpointConfiguration("Samples.UnitOfWork");
12-
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
13-
endpointConfiguration.UseTransport(new LearningTransport());
14-
var recoverability = endpointConfiguration.Recoverability();
15-
recoverability.Immediate(
16-
customizations: immediate =>
17-
{
18-
immediate.NumberOfRetries(0);
19-
});
20-
recoverability.Delayed(
21-
customizations: delayed =>
22-
{
23-
delayed.NumberOfRetries(0);
24-
});
6+
Console.Title = "UnitOfWork";
257

26-
#region ComponentRegistration
8+
var endpointConfiguration = new EndpointConfiguration("Samples.UnitOfWork");
9+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
10+
endpointConfiguration.UseTransport(new LearningTransport());
2711

28-
endpointConfiguration.RegisterComponents(
29-
registration: components =>
30-
{
31-
components.AddTransient<CustomManageUnitOfWork>();
32-
});
12+
var recoverability = endpointConfiguration.Recoverability();
13+
recoverability.Immediate(
14+
customizations: immediate => { immediate.NumberOfRetries(0); });
15+
recoverability.Delayed(
16+
customizations: delayed => { delayed.NumberOfRetries(0); });
3317

34-
#endregion
18+
#region ComponentRegistration
3519

36-
var endpointInstance = await Endpoint.Start(endpointConfiguration);
37-
await Runner.Run(endpointInstance);
38-
await endpointInstance.Stop();
39-
}
40-
}
20+
endpointConfiguration.RegisterComponents(
21+
registration: components => { components.AddTransient<CustomManageUnitOfWork>(); });
22+
23+
#endregion
24+
25+
var endpointInstance = await Endpoint.Start(endpointConfiguration);
26+
await Runner.Run(endpointInstance);
27+
await endpointInstance.Stop();

samples/unit-of-work/Core_8/Sample/Runner.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ public class Runner
66
{
77
public static async Task Run(IEndpointInstance endpointInstance)
88
{
9-
Console.WriteLine("Press 's' to send a Success message");
10-
Console.WriteLine("Press 't' to send a Throw message");
11-
Console.WriteLine("Press any key to exit");
9+
await Console.Out.WriteAsync("Press 's' to send a Success message");
10+
await Console.Out.WriteAsync("Press 't' to send a Throw message");
11+
await Console.Out.WriteAsync("Press any key to exit");
1212

1313
while (true)
1414
{
1515
var key = Console.ReadKey();
16-
Console.WriteLine();
17-
Console.WriteLine();
16+
await Console.Out.WriteLineAsync();
17+
await Console.Out.WriteLineAsync();
1818
switch (key.Key)
1919
{
2020
case ConsoleKey.S:
@@ -30,11 +30,8 @@ public static async Task Run(IEndpointInstance endpointInstance)
3030

3131
break;
3232
default:
33-
{
3433
return;
35-
}
3634
}
3735
}
3836
}
39-
4037
}

samples/unit-of-work/Core_8/Sample/SuccessHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#region SuccessHandler
66

7-
public class SuccessHandler :
7+
sealed class SuccessHandler :
88
IHandleMessages<MessageThatWillSucceed>
99
{
1010
static ILog log = LogManager.GetLogger<SuccessHandler>();

samples/unit-of-work/Core_8/Sample/ThrowHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#region ThrowHandler
77

8-
public class ThrowHandler :
8+
sealed class ThrowHandler :
99
IHandleMessages<MessageThatWillThrow>
1010
{
1111
static ILog log = LogManager.GetLogger<ThrowHandler>();

samples/unit-of-work/sample.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
---
22
title: Unit of Work Usage
33
summary: How to create a custom unit of work
4-
reviewed: 2023-06-02
4+
reviewed: 2025-05-30
55
component: Core
66
related:
77
- nservicebus/pipeline/unit-of-work
88
---
99

1010
> [!WARNING]
11-
> Managing a unit of work via `IManageUnitOfWork` is obsolete as of NServiceBus version 9. Instead, [use a pipeline behavior to manage a unit of work](/samples/pipeline/unit-of-work/).
11+
> `IManageUnitOfWork` is obsolete as of NServiceBus version 9. [Use a pipeline behavior to manage a unit of work instead.](/samples/pipeline/unit-of-work/)
1212
13-
14-
This sample shows how to create a custom [unit of work](/nservicebus/pipeline/unit-of-work.md).
13+
This sample demonstrates how to implement a custom [unit of work](/nservicebus/pipeline/unit-of-work.md).
1514

1615
include: uow-access-to-context
1716

18-
1. Run the solution.
19-
1. Press <kbd>s</kbd> to send a message that will succeed. Press <kbd>t</kbd> to send a message that will throw.
20-
17+
1. Run the solution.
18+
1. Press <kbd>s</kbd> to send a message that succeeds.
19+
1. Press <kbd>t</kbd> to send a message that throws an exception.
2120

2221
## Code walk-through
2322

24-
[Immediate retries](/nservicebus/recoverability/configure-immediate-retries.md) and [delayed retries](/nservicebus/recoverability/configure-delayed-retries.md) have been disabled to reduce the number of errors logged to the console.
25-
23+
[Immediate retries](/nservicebus/recoverability/configure-immediate-retries.md) and [delayed retries](/nservicebus/recoverability/configure-delayed-retries.md) are disabled to avoid excessive error logging in the console.
2624

2725
### CustomManageUnitOfWork
2826

29-
The unit of work logs both the begining and end.
30-
27+
Logs both the start and end of the unit of work.
3128

3229
snippet: CustomManageUnitOfWork
3330

3431
### Component registration
3532

36-
snippet: componentRegistration
33+
Registers the custom unit of work.
3734

35+
snippet: componentRegistration
3836

3937
### SuccessHandler
4038

41-
The `SuccessHandler` logs when a message has been received.
39+
Logs when a message is successfully received.
4240

4341
snippet: SuccessHandler
4442

45-
4643
### ThrowHandler
4744

48-
The `ThrowHandler` logs when a message has been received, then throws an exception
45+
Logs when a message is received, then throws an exception.
4946

5047
snippet: ThrowHandler

0 commit comments

Comments
 (0)