ddd

package
v0.0.0-...-6d78eec Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ddd provides helpers for tactical Domain-Driven Design (DDD).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregateRoot

type AggregateRoot[ID comparable, SerializationType any] interface {
	// ID uniquely identifies the entity.
	ID() ID

	// Serialize returns a serialization for persistence implementations.
	Serialize() SerializationType
}

AggregateRoot is embedded into structs that represent DDD aggregate roots.

type CrudRepository

type CrudRepository[
	A AggregateRoot[I, S],
	I comparable,
	S Serialization[A],
] interface {
	Getter[A, I, S]
	Inserter[A, I, S]
	Updater[A, I, S]
	Upserter[A, I, S]
	Deleter[A, I, S]
}

CrudRepository defines the basic interface of a CRUD repository.

type Deleter

type Deleter[
	A AggregateRoot[I, S],
	I comparable,
	S Serialization[A],
] interface {
	// Delete the entity with the given ID from the repository.
	// If none is found, it returns an apperror.NotFound error.
	Delete(ctx context.Context, id I) error
}

Deleter abstracts deleting an entity from the repository.

type Entity

type Entity struct {
	// contains filtered or unexported fields
}

Entity is embedded into structs that represent DDD entities.

type Getter

type Getter[
	A AggregateRoot[I, S],
	I comparable,
	S Serialization[A],
] interface {
	// Get returns the entity with the specified ID.
	// If none is found, it returns an apperror.NotFound error.
	Get(ctx context.Context, id I) (A, error)
}

Getter abstracts getting an entity by ID from a repository.

type InMemoryCrudRepository

type InMemoryCrudRepository[
	A AggregateRoot[I, S],
	I comparable,
	S Serialization[A],
] struct {
	Mutex          sync.RWMutex
	Serializations map[I]S
}

InMemoryCrudRepository provides a generic CRUD repository implementation for any aggregate root. Mainly meant to be used in in-memory databases for tests and prototyping.

Implements CrudRepository.

func (*InMemoryCrudRepository[A, I, S]) Delete

func (r *InMemoryCrudRepository[A, I, S]) Delete(
	_ context.Context,
	id I,
) error

Delete the entity with the given ID from the repository. If none is found, it returns an apperror.NotFound error.

Provides Deleter.

func (*InMemoryCrudRepository[A, I, S]) Get

func (r *InMemoryCrudRepository[A, I, S]) Get(
	_ context.Context,
	id I,
) (A, error)

Get returns the entity with the specified ID. If none is found, it returns an apperror.NotFound error.

Provides Getter.

func (*InMemoryCrudRepository[A, I, S]) Insert

func (r *InMemoryCrudRepository[A, I, S]) Insert(
	_ context.Context,
	item A,
) error

Insert a new entity into the repository. If there is already a value with the same ID, it returns an apperror.Conflict error.

Provides Inserter.

func (*InMemoryCrudRepository[A, I, S]) Reset

func (r *InMemoryCrudRepository[A, I, S]) Reset()

Reset (re-)initializes the repository. It must be called before other methods.

func (*InMemoryCrudRepository[A, I, S]) Update

func (r *InMemoryCrudRepository[A, I, S]) Update(
	_ context.Context,
	id I,
	update func(x A) error,
) error

Update an entity already present in the repository. If none is found, it returns an apperror.NotFound error.

Provides Updater.

func (*InMemoryCrudRepository[A, I, S]) Upsert

func (r *InMemoryCrudRepository[A, I, S]) Upsert(
	_ context.Context,
	id I,
	insert func() (A, error),
	update func(x A) error,
) error

Upsert does one of two things:

  • If the entity with the specified ID does not exist, it inserts a new entity using the provided insert function.
  • If the entity with the specified ID exists, it updates the entity using the provided update function.

Provides Upserter.

type Inserter

type Inserter[
	A AggregateRoot[I, S],
	I comparable,
	S Serialization[A],
] interface {
	// Insert a new entity into the repository.
	// If there is already a value with the same ID,
	// it returns an apperror.Conflict error.
	Insert(ctx context.Context, x A) error
}

Inserter abstracts inserting a new entity into a repository.

type Serialization

type Serialization[AggregateRoot any] interface {
	Deserialize() AggregateRoot
}

Serialization is the serialized representation of an aggregate root.

A Serialization should contain all the data necessary to persist an aggregate. They are expected to only have exported fields and to contain no business logic. They are commonly used by persistence implementations to deserialize persisted data back into entities.

type Updater

type Updater[
	A AggregateRoot[I, S],
	I comparable,
	S Serialization[A],
] interface {
	// Update an entity already present in the repository.
	// If none is found, it returns an apperror.NotFound error.
	Update(ctx context.Context, id I, update func(x A) error) error
}

Updater abstracts updating an entity already present in a repository.

type Upserter

type Upserter[
	A AggregateRoot[I, S],
	I comparable,
	S Serialization[A],
] interface {
	// Upsert does one of two things:
	//   - If the entity with the specified ID does not exist,
	//     it inserts a new entity using the provided insert function.
	//   - If the entity with the specified ID exists,
	//     it updates the entity using the provided update function.
	Upsert(ctx context.Context,
		id I,
		insert func() (A, error),
		update func(x A) error,
	) error
}

Upserter abstracts inserting a value (if it is not already present in the repository) or updating it (if it is already present).

type Value

type Value struct{}

Value is embedded into structs that represent DDD value objects.

Jump to

Keyboard shortcuts

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