Skip to content

Commit 3c9c97f

Browse files
committed
Refactor command handlers information
1 parent 71a896f commit 3c9c97f

5 files changed

Lines changed: 20 additions & 18 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Command } from './Command';
2+
3+
export class CommandNotRegisteredError extends Error {
4+
constructor(command: Command) {
5+
super(`The command <${command.constructor.name}> hasn't a command handler associated`);
6+
}
7+
}

src/Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Command } from '../../domain/Command';
22
import { CommandHandler } from '../../domain/CommandHandler';
3+
import { CommandNotRegisteredError } from '../../domain/CommandNotRegisteredError';
34

45
export class CommandHandlersInformation {
56
private commandHandlersMap: Map<Command, CommandHandler<Command>>;
@@ -18,7 +19,13 @@ export class CommandHandlersInformation {
1819
return handlersMap;
1920
}
2021

21-
public getCommandHandler(command: Command): CommandHandler<Command> | undefined {
22-
return this.commandHandlersMap.get(command.constructor);
22+
public search(command: Command): CommandHandler<Command> {
23+
const commandHandler = this.commandHandlersMap.get(command.constructor);
24+
25+
if (!commandHandler) {
26+
throw new CommandNotRegisteredError(command);
27+
}
28+
29+
return commandHandler;
2330
}
2431
}

src/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import { Command } from '../../domain/Command';
22
import { CommandBus } from './../../domain/CommandBus';
33
import { CommandHandlersInformation } from './CommandHandlersInformation';
4-
import { NoHandlerForMessageError } from './NoHandlerForMessageError';
54

65
export class InMemoryCommandBus implements CommandBus {
76
constructor(private commandHandlersInformation: CommandHandlersInformation) {}
87

98
dispatch(command: Command): void {
10-
const handler = this.commandHandlersInformation.getCommandHandler(command);
11-
12-
if (!handler) {
13-
throw new NoHandlerForMessageError(command);
14-
}
9+
const handler = this.commandHandlersInformation.search(command);
1510

1611
handler.handle(command);
1712
}

src/Contexts/Shared/infrastructure/CommandBus/NoHandlerForMessageError.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Command } from '../../../../../src/Contexts/Shared/domain/Command';
22
import { CommandHandler } from '../../../../../src/Contexts/Shared/domain/CommandHandler';
3+
import { CommandNotRegisteredError } from '../../../../../src/Contexts/Shared/domain/CommandNotRegisteredError';
34
import { CommandHandlersInformation } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/CommandHandlersInformation';
45
import { InMemoryCommandBus } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/InMemoryCommandBus';
5-
import { NoHandlerForMessageError } from '../../../../../src/Contexts/Shared/infrastructure/CommandBus/NoHandlerForMessageError';
66

77
class UnhandledCommand extends Command {
88
static COMMAND_NAME = 'unhandled.command';
@@ -29,8 +29,8 @@ describe('InMemoryCommandBus', () => {
2929
try {
3030
commandBus.dispatch(unhandledCommand);
3131
} catch (error) {
32-
expect(error).toBeInstanceOf(NoHandlerForMessageError);
33-
expect(error.message).toBe('There is not handler for command of type UnhandledCommand');
32+
expect(error).toBeInstanceOf(CommandNotRegisteredError);
33+
expect(error.message).toBe(`The command <UnhandledCommand> hasn't a command handler associated`);
3434
done();
3535
}
3636
});

0 commit comments

Comments
 (0)