uow

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 1 Imported by: 0

README

uow

import "github.com/brpaz/lib-go/uow"

Package uow defines the Unit of Work interfaces for managing cross-repository transactions without exposing database implementation details to the service layer.

The concrete implementation lives in internal/infra/db and is wired at the application layer. Service code depends only on these interfaces.

Interfaces

Manager creates a new unit of work. UnitOfWork holds an active transaction and exposes the repository bundle T scoped to it.

T is a plain struct defined by each module grouping the repositories it needs inside a single transaction:

type TxRepos struct {
    Users    UsersRepository
    Profiles ProfilesRepository
}
Usage
func (s *Service) CreateWithProfile(ctx context.Context, input Input) error {
    tx, err := s.txManager.Begin(ctx)
    if err != nil {
        return apperror.Internal("begin failed").WithCause(err)
    }
    defer tx.Rollback(ctx) // no-op after a successful Commit

    if err := tx.Repositories().Users.Create(ctx, user); err != nil {
        return err
    }
    if err := tx.Repositories().Profiles.Create(ctx, profile); err != nil {
        return err
    }

    return tx.Commit(ctx)
}

See the inter-repository-transactions guide in docs/guides/ for a full wiring example including the app layer and test fakes.

Index

type Manager

Manager creates units of work. Inject this into services that need cross-repository transactions.

type Manager[T any] interface {
    Begin(ctx context.Context) (UnitOfWork[T], error)
}

type UnitOfWork

UnitOfWork represents an atomic unit of database operations. T is the module-specific repository bundle available within the transaction.

type UnitOfWork[T any] interface {
    Repositories() T
    Commit(ctx context.Context) error
    Rollback(ctx context.Context) error
}

Generated by gomarkdoc

Documentation

Overview

Package uow defines the Unit of Work interfaces for managing cross-repository transactions without exposing database implementation details to the service layer.

The concrete implementation lives in internal/infra/db and is wired at the application layer. Service code depends only on these interfaces.

Interfaces

Manager creates a new unit of work. UnitOfWork holds an active transaction and exposes the repository bundle T scoped to it.

T is a plain struct defined by each module grouping the repositories it needs inside a single transaction:

type TxRepos struct {
    Users    UsersRepository
    Profiles ProfilesRepository
}

Usage

func (s *Service) CreateWithProfile(ctx context.Context, input Input) error {
    tx, err := s.txManager.Begin(ctx)
    if err != nil {
        return apperror.Internal("begin failed").WithCause(err)
    }
    defer tx.Rollback(ctx) // no-op after a successful Commit

    if err := tx.Repositories().Users.Create(ctx, user); err != nil {
        return err
    }
    if err := tx.Repositories().Profiles.Create(ctx, profile); err != nil {
        return err
    }

    return tx.Commit(ctx)
}

See the inter-repository-transactions guide in docs/guides/ for a full wiring example including the app layer and test fakes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager[T any] interface {
	Begin(ctx context.Context) (UnitOfWork[T], error)
}

Manager creates units of work. Inject this into services that need cross-repository transactions.

type UnitOfWork

type UnitOfWork[T any] interface {
	Repositories() T
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

UnitOfWork represents an atomic unit of database operations. T is the module-specific repository bundle available within the transaction.

Jump to

Keyboard shortcuts

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