service

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package service provides BaseService with decorator support for building domain service layers with cross-cutting concerns.

BaseService delegates all CRUD operations to an interfaces.Repository, keeping the service layer thin. Use the decorators sub-package to wrap services with logging, authorization, and panic recovery via a fluent builder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpChain

func OpChain(
	name string,
	timeout time.Duration,
	logger interfaces.Logger,
	metrics interfaces.Metrics,
	tracer interfaces.Tracer,
	authFn func(ctx context.Context, action string) error,
) decorate.OpMiddleware

OpChain builds the custom-operation decoration chain for a service named name, reproducing the order the CRUD decorator builder applies to custom ops (behavior-preserving) — outermost → innermost: recovery → tracing → metrics → logging → auth → timeout. Recovery is ALWAYS outermost (the builder always adds it; a nil logger still recovers, just without logging). Validation is entity-based and a passthrough for custom ops, so it is omitted. Any nil dependency (nil authFn, or a non-positive timeout) omits its middleware, matching the builder. Custom service methods run through this via decorate.Exec[R].

Types

type BaseService

type BaseService[T any, P any, ID comparable] struct {
	// contains filtered or unexported fields
}

BaseService wraps a Repository and implements the core Service interface. It is the standard entry point for building decorated service stacks.

func New

func New[T, P any, ID comparable](
	repo interfaces.Repository[T, P, ID],
	name string,
) *BaseService[T, P, ID]

New creates a new BaseService wrapping the given repository.

func (*BaseService[T, P, ID]) Create

func (s *BaseService[T, P, ID]) Create(ctx context.Context, entity *T) (*T, error)

Create persists a new entity and returns the created record.

func (*BaseService[T, P, ID]) Delete

func (s *BaseService[T, P, ID]) Delete(ctx context.Context, id ID) error

Delete removes an entity by its identifier.

func (*BaseService[T, P, ID]) Get

func (s *BaseService[T, P, ID]) Get(ctx context.Context, id ID) (*T, error)

Get retrieves an entity by its identifier.

func (*BaseService[T, P, ID]) List

func (s *BaseService[T, P, ID]) List(
	ctx context.Context,
	params P,
	page types.PageRequest,
) (*types.Page[T], error)

List retrieves a paginated list of entities matching the given filter parameters.

func (*BaseService[T, P, ID]) Name

func (s *BaseService[T, P, ID]) Name() string

Name returns the service name (used for decorator labels).

func (*BaseService[T, P, ID]) Update

func (s *BaseService[T, P, ID]) Update(
	ctx context.Context,
	id ID,
	entity *T,
) (*T, error)

Update modifies an existing entity identified by id and returns the updated record.

type CRUDNoListRepo

type CRUDNoListRepo[T any, ID comparable] interface {
	// Reader contributes Get (read by id).
	interfaces.Reader[T, ID]
	// Writer contributes Create and Update.
	interfaces.Writer[T, ID]
	// Deleter contributes Delete (remove by id).
	interfaces.Deleter[ID]
}

CRUDNoListRepo is the list-less repository surface a list-less service delegates to: Get + Create + Update + Delete (services expose no Exists). A list-less repository (e.g. repository.CRUDNoListRepository: Reader+Writer+Deleter+Exister) satisfies it.

type CRUDNoListService

type CRUDNoListService[T any, ID comparable] struct {
	// contains filtered or unexported fields
}

CRUDNoListService is a decorated domain service for a list-less resource (Reader + Writer + Deleter, no Lister). It delegates each op to the repository through the shared service OpChain, so it carries the shared recovery/observability/timeout stack without embedding (and panicking) a List the resource cannot serve. It is the service-tier sibling of the full-CRUD decorator builder for resources whose collection is not listable (e.g. one addressed only by id or an external id).

func NewCRUDNoListService

func NewCRUDNoListService[T any, ID comparable](
	name string,
	repo CRUDNoListRepo[T, ID],
	timeout time.Duration,
	logger interfaces.Logger,
	metrics interfaces.Metrics,
	tracer interfaces.Tracer,
	authFn func(ctx context.Context, action string) error,
) CRUDNoListService[T, ID]

NewCRUDNoListService builds a decorated list-less service over the repository under the given name (stamped on metrics/logs/spans, and used as the "<name>.<Op>" authorization action prefix). Any nil observability collaborator (or nil authFn / non-positive timeout) skips its middleware; recovery is always present (the service OpChain always adds it).

func (CRUDNoListService[T, ID]) Create

func (s CRUDNoListService[T, ID]) Create(ctx context.Context, entity *T) (*T, error)

Create persists a new entity through the decorated service chain.

func (CRUDNoListService[T, ID]) Delete

func (s CRUDNoListService[T, ID]) Delete(ctx context.Context, id ID) error

Delete removes an entity by id through the decorated service chain.

func (CRUDNoListService[T, ID]) Get

func (s CRUDNoListService[T, ID]) Get(ctx context.Context, id ID) (*T, error)

Get retrieves an entity by id through the decorated service chain.

func (CRUDNoListService[T, ID]) Update

func (s CRUDNoListService[T, ID]) Update(
	ctx context.Context,
	id ID,
	entity *T,
) (*T, error)

Update modifies an existing entity through the decorated service chain.

Directories

Path Synopsis
Package decorators provides fluent decorator composition for services.
Package decorators provides fluent decorator composition for services.

Jump to

Keyboard shortcuts

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