cqrs_dig

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 2 Imported by: 0

README

Go - CQRS Dig

Methods to register golang-cqrs assets into dig container.

Status

Quality Gate Status Coverage Maintainability Rating

Installing

go get -u github.com/mitz-it/golang-cqrs-dig

Usage

This package provides methods to register CQRS implementations with dig-injected services. For all the following samples, let's assume that every CQRS implementation receives a *PingPongService into the constructor function. To understand how to create the CQRS implementation, please read the documentation available here.

Create the service to be injected:

type PingPongService struct {
}

func (s *PingPongService) Play(ping string, pong string) string {
  return fmt.Sprintf("%s %s", ping, pong)
}

func NewPingPongService() *PingPongService {
  return &PingPongService{}
}

Create and configure the *dig.Container instance:

container := dig.New()
container.Provide(NewPingPongService)

Commands Usage

// The command handler constructor func with injected *PongService
func NewPingCommandHandler(service *PongService) cqrs.ICommandHandler[*PingCommand, *PingResponse] {
  return &CommandHandler{
    service: service,
  }
}

// register the command handler constructor into the DI container
cqrs_dig.ProvideCommandHandler[*PingCommand, *PingResponse](container, NewPingCommandHandler)

Queries Usage

// The query handler constructor func with injected *PongService
func NewPingQueryHandler(service *PingPongService) cqrs.IQueryHandler[*PingQuery, *PingResponse] {
  return &PingQueryHandler{
    service: service,
  }
}

// register the query handler constructor into the DI container
cqrs_dig.ProvideQueryHandler[*PingQuery, *PingResponse](container, NewPingQueryHandler)

Behaviors Usage

// The behavior constructor func with injected *PongService
func NewPingBehavior(service *PingPongService) *PingBehavior {
  return &PingBehavior{
    service: service,
  }
}

// register the behavior constructor for commands into the DI container
cqrs_dig.ProvideCommandBehavior[*PingBehavior](container, 0, NewPingBehavior)

// register the behavior constructor for queries into the DI container
cqrs_dig.ProvideQueryBehavior[*PingBehavior](container, 0, NewPingBehavior)

Events Usage

// The event handler constructor func with injected *PongService
func NewPingEventHandler(service *PingPongService) cqrs.IEvenHandler[*PingEvent] {
  return &PingEventHandler{
    service: service,
  }
}

func NewPingEventHandler2(service *PingPongService) cqrs.IEvenHandler[*PingEvent] {
  return &PingEventHandler2{
    service: service,
  }
}

// register the event handler constructor into the DI container
cqrs_dig.ProvideEventSubscriber[*PingEvent](container, NewPingEventHandler)

// use this method if you have more than one handler per event type
cqrs_dig.ProvideEventSubscribers[*PingEvent](container, NewPingEventHandler, NewPingEventHandler2)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProvideCommandBehavior

func ProvideCommandBehavior[TBehavior cqrs.IBehavior](container *dig.Container, order int, constructor interface{}) error

func ProvideCommandHandler

func ProvideCommandHandler[TCommand any, TResponse any](container *dig.Container, constructor interface{}) error

func ProvideEventSubscriber

func ProvideEventSubscriber[TEvent any](container *dig.Container, constructor interface{}) error

func ProvideEventSubscribers

func ProvideEventSubscribers[TEvent any](container *dig.Container, constructors ...interface{}) error

func ProvideQueryBehavior

func ProvideQueryBehavior[TBehavior cqrs.IBehavior](container *dig.Container, order int, constructor interface{}) error

func ProvideQueryHandler

func ProvideQueryHandler[TQuery any, TResponse any](container *dig.Container, constructor interface{}) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL