The reference implementation
The reference implementation is defined in the NPM package @tmorin/ceb-messaging-simple.
The reference implementation relies on an in-memory and single process approach. So that, the implementation is free of network or any other concerns related to distributed systems.
The SimpleGateway
A SimpleGateway instance can be got from the following three approaches: the global instance, the factory method or the constructor.
The global instance
A global instance of the SimpleGateway is available from the static field SimpleGateway.GOBAL
.
It's a lazy property, in fact the instance is only created once at its first get.
import { MessageBuilder } from "@tmorin/ceb-messaging-core"
import { SimpleGateway } from "@tmorin/ceb-messaging-simple"
// create an event and publish it using the global SimpleGateway instance
const event = MessageBuilder.event("EventA").build()
SimpleGateway.GLOBAL.events.publish(event)
The factory method
A SimpleGateway instance can be easily created using the factory method, i.e. the static method SimpleGateway.create()
.
The method returns a fresh new SimpleGateway instance at each call.
import { Gateway, MessageBuilder } from "@tmorin/ceb-messaging-core"
import { SimpleGateway } from "@tmorin/ceb-messaging-simple"
// create a SimpleGateway instance
const gateway: Gateway = SimpleGateway.create()
// create an event and publish it
const event = MessageBuilder.event("EventA").build()
gateway.events.publish(event)
// dispose the created SimpleGateway
gateway.dispose().catch((e) => console.error(e))
The constructor
The constructor approach provides a fine grain control of the Gateway dependencies: the CommandBus, the QueryBus, the EventBus and the GatewayObserver.
import { GatewayEmitter, MessageBuilder } from "@tmorin/ceb-messaging-core"
import {
SimpleCommandBus,
SimpleEventBus,
SimpleGateway,
SimpleQueryBus,
} from "@tmorin/ceb-messaging-simple"
// create the SimpleGateway dependencies
const emitter = new GatewayEmitter()
const events = new SimpleEventBus(emitter)
const commands = new SimpleCommandBus(events, emitter)
const queries = new SimpleQueryBus(emitter)
// create a SimpleGateway instance
const gateway = new SimpleGateway(events, commands, queries, emitter)
// create an event and publish it
const event = MessageBuilder.event("EventA").build()
gateway.events.publish(event)
// dispose the created SimpleGateway
gateway.dispose().catch((e) => console.error(e))
The Inversion Module
The package provides an Inversion Module which can be used to create (optionally) and register it on the registry.
Create a container with the default module behavior, i.e. the SimpleGateway will be created from scratch automatically:
import { ContainerBuilder } from "@tmorin/ceb-inversion-core"
import {
Gateway,
GatewaySymbol,
MessageBuilder,
} from "@tmorin/ceb-messaging-core"
import { SimpleModule } from "@tmorin/ceb-messaging-simple-inversion"
ContainerBuilder.get()
// let the module created it-self the SimpleGateway instance
.module(new SimpleModule())
.build()
.initialize()
.then((container) => {
// resolve the gateway
const gateway = container.registry.resolve<Gateway>(GatewaySymbol)
// publish a simple message
gateway.events.publish(MessageBuilder.event("Hello").build())
})
.catch((e) => console.error(e))
Create a container with a provided SimpleGateway instance:
import { ContainerBuilder } from "@tmorin/ceb-inversion-core"
import {
Gateway,
GatewaySymbol,
MessageBuilder,
} from "@tmorin/ceb-messaging-core"
import { SimpleGateway } from "@tmorin/ceb-messaging-simple"
import { SimpleModule } from "@tmorin/ceb-messaging-simple-inversion"
ContainerBuilder.get()
.module(
new SimpleModule({
// the provided instance, there the GLOBAL one
gateway: SimpleGateway.GLOBAL,
})
)
.build()
.initialize()
.then((container) => {
// resolve the gateway
const gateway = container.registry.resolve<Gateway>(GatewaySymbol)
// publish a simple message
gateway.events.publish(MessageBuilder.event("Hello").build())
})
.catch((e) => console.error(e))