usecase-bus
v1.1.0
Published
A mediator for UseCase and Command.
Downloads
9
Readme
@almin/usecase-bus
A mediator for UseCase and Command. This library provide Command Bus.
What is Command and Command handler pattern?
Command
- A bus send Command to a single Command Handler
- Command may be rejected by system
- Command may be failed during executing in Handler
- Command may be various effect in system state
- Command does not be over the boundary
- Command should have imperative named.
What is merits?
- Flexible
- Retry the UseCase
- Logging
- Adoptable
- Additional Event
What is de-merits?
- Add new Layer(CommandBus)
Install
Install with npm:
npm install @almin/usecase-bus
Usage
import { UseCase, Context } from "almin";
import { UseCaseCommandBus } from "@almin/usecase-bus"
// async code
(async () => {
const context = new Context({
store: new NopeStore()
});
class CommandA {
type = "CommandA";
}
class CommandB {
type = "CommandB";
}
const executed: (CommandA | CommandB)[] = [];
class TestUseCaseA extends UseCase {
execute(command: CommandA) {
executed.push(command);
}
}
class TestUseCaseB extends UseCase {
execute(command: CommandB) {
executed.push(command);
}
}
const createTestUseCaseB = (_command: CommandB) => {
return new TestUseCaseB();
};
// create binding between Command Constructor and UseCase/UseCaseFactory.
const bus = UseCaseCommandBus.create(context)
.bind(CommandA, new TestUseCaseA())
.bindFactory(CommandB, createTestUseCaseB);
// send CommandA => execute TestUseCaseA
await bus.send(new CommandA());
assert.strictEqual(executed.length, 1);
assert.ok(executed[0] instanceof CommandA);
// send CommandB => execute createTestUseCaseB()
await bus.send(new CommandB());
assert.strictEqual(executed.length, 2);
assert.ok(executed[1] instanceof CommandB);
})()
Changelog
See Releases page.
Reference
- Implementing the microservice application layer using the Web API | Microsoft Docs
- predaddy/src/predaddy/messagehandling at 3.0 · szjani/predaddy
- jbogard/MediatR: Simple, unambitious mediator implementation in .NET
Contributing
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author
License
MIT © azu