uow

package
v0.0.0-...-c3afde3 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: BSD-3-Clause Imports: 1 Imported by: 1

Documentation

Overview

Package uow defines the contract for Unit of Work.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	// Begin begins the transaction.
	Begin(ctx context.Context) (Tx, error)
	// Exec executes the query without getting the data from the row.
	Exec(ctx context.Context, query string, args ...interface{}) (int64, error)
	// Query runs the query given.
	Query(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

DB defines an interface for database functionality. This interface exists for the sake of Unit of Work (UoW) pattern. The moment this project is being made, Go doesn't have proper UoW library that can abstract the process.

I will agree to the opinion that says this is not proper/correct way to achieve UoW or transaction as a business. I read many articles and this is the conclusion I made using all knowledges I have.

type Tx

type Tx interface {
	DB
	// Commit commits the transaction.
	Commit(ctx context.Context) error
	// Rollback rolls back the transaction.
	Rollback(ctx context.Context) error
}

Tx defines an interface for business transaction functionality. This interface exists for the sake of Unit of Work (UoW) pattern. See more in the comment on DB interface.

type UnitOfWork

type UnitOfWork interface {
	// Begin begins the transaction.
	Begin(ctx context.Context) (Tx, error)
	// Finish finishes the transaction.
	Finish(ctx context.Context, tx Tx, err error) error
}

UnitOfWork defines the high-level use case of Unit of Work pattern.

type UnitWorker

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

UnitWorker represents a unit of work. It actually just to begin and finish the transaction.

func NewUnitWorker

func NewUnitWorker(db DB) *UnitWorker

NewUnitWorker creates an instance of UnitWorker.

func (*UnitWorker) Begin

func (u *UnitWorker) Begin(ctx context.Context) (Tx, error)

Begin begins the transaction.

func (*UnitWorker) Finish

func (u *UnitWorker) Finish(ctx context.Context, tx Tx, err error) error

Finish finishes the transaction.

Jump to

Keyboard shortcuts

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