domain

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: MIT Imports: 7 Imported by: 0

README

Domain

This package provides basic definitions for building application domain models in Go following a DDD approach.

Things included are:

  • Entity identifiers generator.
  • Domain event recording traits.
  • Domain event dispatching and publishing definitions.
  • Continuation token pagination primitives.

Installation

go get github.com/tangelo-labs/go-domain

Example

Defining aggregate roots:

package main

import (
    "github.com/tangelo-labs/go-domain"
)

type NameChangedEvent struct {
	ID      domain.ID
    Before  string
    After   string
}

type User struct {
    id      domain.ID
    name    string
    age     int

    events.BaseRecorder
}

func NewUser(id domain.ID) *User {
    return &User{
        id: id,
    }
}

func (u *User) ID() domain.ID {
    return u.id
}

func (u *User) ChangeName(n string) {   
    u.Record(NameChangedEvent{
        ID: u.ID(),
        Before: u.name,
        After: n,
    })
    
     u.name = n    
}

See the "examples" directory for more complete example.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ULIDGenerator = func() ID {
	return ID(ulid.MustNew(ulid.Timestamp(time.Now()), rand.Reader).String())
}

ULIDGenerator is the default ID generator function that uses ULID algorithm.

View Source
var UUIDGenerator = func() ID {
	return ID(uuid.New().String())
}

UUIDGenerator is an ID generator function that uses UUID algorithm.

Functions

func SetIDGenerator

func SetIDGenerator(fn func() ID)

SetIDGenerator sets the ID generator function. Call this function before using any of the ID generating functions, preferably in an init() function.

By default, the ULID algorithm is used.

Types

type AggregateRoot

type AggregateRoot interface {
	Entity
	events.Recorder
}

AggregateRoot is the base interface for all domain aggregate roots.

type Entity

type Entity interface {
	ID() ID
}

Entity is the base interface for all domain entities.

type ID

type ID string

ID defines a globally unique identifier for an Entity.

func NewID

func NewID() ID

NewID creates a new ID using an ULID generator.

func (ID) Equals

func (id ID) Equals(other ID) bool

Equals returns true if this ID is equal to another.

func (ID) IsEmpty

func (id ID) IsEmpty() bool

IsEmpty whether this ID is an empty ID or not (zero-valued).

func (ID) MarshalBinary

func (id ID) MarshalBinary() (data []byte, err error)

MarshalBinary encodes this ID as binary.

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON encodes this ID as JSON.

func (ID) MarshalText

func (id ID) MarshalText() (text []byte, err error)

MarshalText marshals into a textual form.

func (ID) String

func (id ID) String() string

String returns a string representation of this ID.

func (*ID) UnmarshalBinary

func (id *ID) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes this ID back from binary.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(bytes []byte) error

UnmarshalJSON decodes this ID back from JSON.

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(text []byte) error

UnmarshalText unmarshal a textual representation of an ID.

type IDs

type IDs []ID

IDs a convenience definition for dealing with collection of IDs in memory.

func (IDs) Contains

func (ids IDs) Contains(id ID) bool

Contains whether this list contains the specified ID.

func (IDs) Subtract

func (ids IDs) Subtract(others IDs) IDs

Subtract returns a new list in which the provided list is subtracted from the current list. Examples:

* {} Subtract {} = {}. * {a,b,c,d,d} Subtract {b,d} = {a,c}. * {a,b,c,d} Subtract {} = {a,b,c,d}. * {a,b,c,d} Subtract {e} = {a,b,c,d}.

Directories

Path Synopsis
dispatcher
Package dispatcher provides a simple mechanism for dispatching domain events locally using a simple in-memory broker.
Package dispatcher provides a simple mechanism for dispatching domain events locally using a simple in-memory broker.
drain
Package drain implements a composable message distribution package for Go.
Package drain implements a composable message distribution package for Go.
examples
ordersapp
Package main provides a simple application example that uses the different "go-domain" package definitions.
Package main provides a simple application example that uses the different "go-domain" package definitions.
Package pagination provides generic definitions for implementing `Timestamp_ID` Continuation Token pagination algorithm.
Package pagination provides generic definitions for implementing `Timestamp_ID` Continuation Token pagination algorithm.

Jump to

Keyboard shortcuts

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