Code Monkey home page Code Monkey logo

go-unit-of-work's Introduction

Unit of Work

This project implements a Unit of Work (UOW) pattern in Go, providing a convenient way to encapsulate a set of database operations within a single transaction when the Repository pattern is used.

The Unit of Work allows to register repositories and perform operations on them, ensuring that all actions are executed within the same transaction.

Installation

To install this package, use the go get command:

go get github.com/sesaquecruz/go-unit-of-work/uow

Usage

To use the UOW, follow these steps:

  1. Import the package into the Go file:
import "github.com/sesaquecruz/go-unit-of-work/uow"
  1. Create an instance passing a db connection (*sql.DB):
UOW := uow.NewUnitOfWork(db)
  1. Register the repositories using the Register method:
UOW.Register("ProductRepository", func(tx *sql.Tx) uow.Repository {
	return NewProductRepository(tx)
})
UOW.Register("OrderRepository", func(tx *sql.Tx) uow.Repository {
	return NewOrderRepository(tx)
})
  1. Perform database operations within a single transaction by calling the Do method:
err = UOW.Do(ctx, func(ctx context.Context, tx uow.TX) error {
	// Get repositories

	productRepository, err := uow.GetAs[*ProductRepository](tx, "ProductRepository")
	if err != nil {
		return err
	}

	orderRepository, err := uow.GetAs[*OrderRepository](tx, "OrderRepository")
	if err != nil {
		return err
	}

	// Execute operations
	// ...
})

If an error occurs, the transaction is rolled back and the error is returned. Otherwise, the transaction is committed, and nil is returned.

Types and Interfaces

The uow package provides the following type and interfaces:

type RepositoryName string
type Repository any
type RepositoryFactory func(tx *sql.Tx) Repository
type TX interface {
	Get(name RepositoryName) (Repository, error)
}
type UOW interface {
	Register(name RepositoryName, factory RepositoryFactory) error
	Remove(name RepositoryName) error
	Has(name RepositoryName) bool
	Clear()
	Do(ctx context.Context, fn func(ctx context.Context, tx TX) error) error
}

For more details, see uow.go.

Contributing

Contributions to this project are welcome. If you encounter any issues or have ideas for enhancements, feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License. Please see the LICENSE file for more details.

go-unit-of-work's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.