Skip to content

Instantly share code, notes, and snippets.

@geggleto
Created June 22, 2020 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geggleto/f072939ba9adb2ea938caacb23919570 to your computer and use it in GitHub Desktop.
Save geggleto/f072939ba9adb2ea938caacb23919570 to your computer and use it in GitHub Desktop.
const {
Command,
CommandBus,
CommandHandlerMiddleware,
ClassNameExtractor,
InMemoryLocator,
HandleInflector,
LoggerMiddleware
} = require('simple-command-bus');
// CreateAccount Command
class CreateAccountCommand extends Command {
constructor(firstName, lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
// CreateAccount Handler
class CreateAccountHandler {
handle(command) {
// Logic to create an account.
}
};
// Handler middleware
var commandHandlerMiddleware = new CommandHandlerMiddleware(
new ClassNameExtractor(),
new InMemoryLocator({ CreateAccountHandler: new CreateAccountHandler() }),
new HandleInflector()
);
// Command bus instance
var commandBus = new CommandBus([
new LoggerMiddleware(console),
commandHandlerMiddleware
]);
const createAccountCommand = new CreateAccountCommand('John', 'Doe');
var result = commandBus.handle(createAccountCommand);
console.log('Result:', result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment