The CQRS library as TypeScript for angular framework
- Using npm
$ npm i -g cb-cqrs $ npm i cb-cqrs --save
-
Add module CbCqrsModule in your main module
@NgModule({ imports: [ ... CbCqrsModule ... ]
-
Register containers for handlers
export class AppModule { constructor(cIoC: CommandContainer, qIoC: QueryContainer, eIoC:EventContainer) { } }
-
Sample containers IoC
@Injectable({providedIn: 'root'}) export class CommandContainer { constructor(sampleCommandHandler:SampleCommandHandler) {} } @Injectable({providedIn: 'root'}) export class QueryContainer { constructor(sampleQueryHandler:SampleQueryHandler) {} } @Injectable({providedIn: 'root'}) export class EventContainer { constructor(sampleEventHandler_WhenOnStart:SampleEventHandler_WhenOnStart, sampleEventHandler_WhenOnPause:SampleEventHandler_WhenOnPause) {} }
-
Create SampleCommand of ICommand
export class SampleCommand implements ICommand{ body: string; constructor(body: string) { this.body = body } }
-
Create SampleCommandHandler of IHandleCommand
@Injectable({providedIn: 'root'}) export class SampleCommandHandler implements IHandleCommand<SampleCommand> { constructor(private ioc: BaseContainerIoC) { ioc.registerCommand(this); } handle(command: SampleCommand) { console.log('===> Message from SampleCommandHandler', command.body); } }
-
Handle SampleCommand
constructor(private serviceBus: ServiceBus) {} ngOnInit(): void { this.serviceBus.sendCommand(new SampleCommand("Helow world")) } // console result // ===> Message from SampleCommandHandler Helow world
-
Create SampleQuery of IQuery
export class SampleQuery implements IQuery{ body: string; constructor(body: string) { this.body = body} }
-
Create SampleResponse of IResponse
export class SampleResponse implements IResponse{ body: string; constructor(body: string) { this.body = body } }
-
Create SampleQueryHandler of IHandleQuery<SampleQuery, SampleResponse>
@Injectable({providedIn: 'root'}) export class SampleQueryHandler implements IHandleQuery<SampleQuery, SampleResponse> { constructor(private ioc: BaseContainerIoC) { ioc.registerQuery(this); } handle(query: SampleQuery): SampleResponse { let result = new SampleResponse('===> Message from SampleQueryHandler ' + query.body); return result; } }
-
Handle SampleQuery
constructor(private serviceBus: ServiceBus) {} ngOnInit(): void { let log = this.serviceBus.query<SampleResponse>(new SampleQuery("Helow World")) console.log(log.body) } // console result // ===> Message from SampleQueryHandler Helow World
-
Create SampleEvent of IEvent
export class SampleEvent implements IEvent { body: string; constructor(body: string) { this.body = body } }
-
Create SampleEventHandler_WhenOnStart of IHandleEvent
@Injectable({providedIn: 'root'}) export class SampleEventHandler_WhenOnStart implements IHandleEvent<SampleEvent> { constructor(private ioc: BaseContainerIoC) { ioc.registerEvent(this); } handle(event: SampleEvent) { console.log('===> Message from SampleEventHandler_WhenOnStart ' + event.body); } }
-
Create SampleEventHandler_WhenOnPause of IHandleEvent
@Injectable({providedIn: 'root'}) export class SampleEventHandler_WhenOnPause implements IHandleEvent<SampleEvent> { constructor(private ioc: BaseContainerIoC) { ioc.registerEvent(this); } handle(event: SampleEvent) { console.log('===> Message from SampleEventHandler_WhenOnPause ' + event.body); } }
-
Handle SampleEvent
constructor(private serviceBus: ServiceBus) {} ngOnInit(): void { this.serviceBus.sendEvent(new SampleEvent("Helow World")) } // console result // ===> Message from SampleEventHandler_WhenOnStart Helow World // ===> Message from SampleEventHandler_WhenOnPause Helow World