Skip to content

Commit a473d45

Browse files
committed
2 parents 349a130 + f53b7b2 commit a473d45

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

README.md

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,33 @@ public void ConfigureServices(IServiceCollection services)
5353
// This is resolved by the MessageDelegator.
5454
services.AddSingleton<IMessageHandlerResolver>((serviceProvider) =>
5555
{
56-
// Register command handlers to the message handler registration.
5756
// Commands can only have one handler so use SingleMessageHandlerRegistration.
5857
var commandHandlerRegistration = new SingleMessageHandlerRegistration();
58+
59+
// Register command handlers to the single message handler registration.
5960
60-
// RegisterProductCommand
61-
commandHandlerRegistration.Register<RegisterProductCommand>((message, ct) =>
61+
// ActivateProductCommand
62+
commandHandlerRegistration.Register<RegisterProductCommand>((message, cancellationToken) =>
6263
{
64+
// You can also manually instantiate if that's how you roll.
6365
var handler = serviceProvider.GetRequiredService<RegisterProductCommandHandler>();
64-
return handler.HandleRegisterProductCommandAsync(message, ct);
66+
return handler.HandleRegisterProductCommandAsync(message, cancellationToken);
6567
});
6668

6769
// ActivateProductCommand
68-
commandHandlerRegistration.Register<ActivateProductCommand>((message, ct) =>
70+
commandHandlerRegistration.Register<ActivateProductCommand>((message, cancellationToken) =>
6971
{
72+
// You can also manually instantiate if that's how you roll.
7073
var handler = serviceProvider.GetRequiredService<ActivateProductCommandHandler>();
71-
return handler.HandleActivateProductCommandAsync(message, ct);
74+
return handler.HandleActivateProductCommandAsync(message, cancellationToken);
7275
});
7376

7477
// DeactivateProductCommand
75-
commandHandlerRegistration.Register<DeactivateProductCommand>((message, ct) =>
78+
commandHandlerRegistration.Register<DeactivateProductCommand>((message, cancellationToken) =>
7679
{
80+
// You can also manually instantiate if that's how you roll.
7781
var handler = serviceProvider.GetRequiredService<DeactivateProductCommandHandler>();
78-
return handler.HandleDeactivateProductCommandAsync(message, ct);
82+
return handler.HandleDeactivateProductCommandAsync(message, cancellationToken);
7983
});
8084

8185
return commandHandlerRegistration.BuildMessageHandlerResolver();
@@ -105,56 +109,33 @@ public void ConfigureServices(IServiceCollection services)
105109
// This is resolved by the MessageDelegator.
106110
services.AddSingleton<IMessageHandlerResolver>((serviceProvider) =>
107111
{
108-
// Register event handlers to the message handler registration.
109112
// Events can have multiple handlers so use MultiMessageHandlerRegistration.
110113
var eventHandlerRegistration = new MultiMessageHandlerRegistration();
111-
112-
// In the sample, all events are published as IDomainEvent by the PublishingRepository,
113-
// so register event handlers for IDomainEvent and check if domain event can be handled.
114-
114+
115+
// Register event handlers to the message handler registration.
116+
115117
// ProductRegisteredEvent
116-
eventHandlerRegistration.Register<IDomainEvent>((message, ct) =>
118+
eventHandlerRegistration.Register<ProductRegisteredEvent>((message, cancellationToken) =>
117119
{
118-
ProductRegisteredEvent domainEvent = message as ProductRegisteredEvent;
119-
if (domainEvent != null)
120-
{
121-
// Handle only if domain event is a ProductRegisteredEvent.
122-
var handler = serviceProvider.GetRequiredService<ProductDomainEventsHandler>();
123-
return handler.HandleProductRegisteredEventAsync(domainEvent, ct);
124-
}
125-
126-
// Do nothing.
127-
return Task.CompletedTask;
120+
// You can also manually instantiate if that's how you roll.
121+
var handler = serviceProvider.GetRequiredService<ProductDomainEventsHandler>();
122+
return handler.HandleProductRegisteredEventAsync(message, cancellationToken);
128123
});
129124

130125
// ProductActivatedEvent
131-
eventHandlerRegistration.Register<IDomainEvent>((message, ct) =>
126+
eventHandlerRegistration.Register<ProductActivatedEvent>((message, cancellationToken) =>
132127
{
133-
ProductActivatedEvent domainEvent = message as ProductActivatedEvent;
134-
if (domainEvent != null)
135-
{
136-
// Handle only if domain event is a ProductActivatedEvent.
137-
var handler = serviceProvider.GetRequiredService<ProductDomainEventsHandler>();
138-
return handler.HandleProductActivatedEventAsync(domainEvent, ct);
139-
}
140-
141-
// Do nothing.
142-
return Task.CompletedTask;
128+
// You can also manually instantiate if that's how you roll.
129+
var handler = serviceProvider.GetRequiredService<ProductDomainEventsHandler>();
130+
return handler.HandleProductActivatedEventAsync(message, cancellationToken);
143131
});
144132

145133
// ProductDeactivatedEvent
146-
eventHandlerRegistration.Register<IDomainEvent>((message, ct) =>
134+
eventHandlerRegistration.Register<ProductDeactivatedEvent>((message, cancellationToken) =>
147135
{
148-
ProductDeactivatedEvent domainEvent = message as ProductDeactivatedEvent;
149-
if (domainEvent != null)
150-
{
151-
// Handle only if domain event is a ProductDeactivatedEvent.
152-
var handler = serviceProvider.GetRequiredService<ProductDomainEventsHandler>();
153-
return handler.HandleProductDeactivatedEventAsync(domainEvent, ct);
154-
}
155-
156-
// Do nothing.
157-
return Task.CompletedTask;
136+
// You can also manually instantiate if that's how you roll.
137+
var handler = serviceProvider.GetRequiredService<ProductDomainEventsHandler>();
138+
return handler.HandleProductDeactivatedEventAsync(message, cancellationToken);
158139
});
159140

160141
return eventHandlerRegistration.BuildMessageHandlerResolver();

0 commit comments

Comments
 (0)