Skip to content

sbeardsley/cqrs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cqrs simple implementation

Installation

go get github.com/sbeardsley/cqrs

Usage

Command and CommandHandler

package command

// CreateUser command
type CreateUser struct {
	Username string
	Password string
}

// CreateUserHandler command handler
type CreateUserHandler struct {
	repo UserRepo
}

// NewCreateUserHandler command handler constructor
func NewCreateUserHandler(repo UserRepo) CreateUserHandler {
	return CreateUserHandler{
		repo: repo
	}
}

// NewCommand used during registration of handler
func (h CreateUserHandler) NewCommand() interface{} {
	return &CreateUser{}
}

// Handle command
func (h CreateUserHandler) Handle(message interface{}) error {

	cmd := message.(*CreateUser)
	err := repo.CreateUser(cmd.Username, cmd.Password)
	if err != nil {
		return err
	}

	return nil
}

Initialization and registration of your handler

package main

import (
    "github.com/sbeardsley/cqrs"
)

func main () {
    dispatcher := cqrs.NewInMemorySender()
    userRepo := repository.NewUserRepository(db)
    cmdHandler := command.NewCreateUserHandler(userRepo)

    dispatcher.Register(cmdHandler)

    err := dispatcher.Send(&command.CreateUser{
        Username: "test_user",
        Password: "P@ssw0rd1",
    })

    if err != nil {
        panic(err)
    }
}

Snippets

vscode user snippets are located in the .vscode folder

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages