repository

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: MIT Imports: 1 Imported by: 0

README

Repository Module

The Repository Module provides generic repository interfaces for entity persistence operations in Go applications. It implements the Repository Pattern, which is a key component of Domain-Driven Design (DDD) and Hexagonal Architecture.

Features

  • Generic Interfaces: Type-safe repository interfaces using Go generics
  • Hexagonal Architecture: Supports ports and adapters pattern
  • Domain-Driven Design: Facilitates separation of domain and infrastructure concerns
  • Repository Pattern: Standardized approach to data access
  • Repository Factory: Interface for creating repositories

Installation

go get github.com/abitofhelp/servicelib/repository

Quick Start

See the Basic Repository example for a complete, runnable example of how to use the Repository module.

API Documentation

Basic Repository Implementation

The Repository interface provides a generic interface for entity persistence operations.

Basic Usage

See the Basic Repository example for a complete, runnable example of how to implement and use a basic repository.

Repository Factory

The RepositoryFactory interface provides a way to create and manage repositories for different entity types.

Using Repository Factory

See the Repository Factory example for a complete, runnable example of how to implement and use a repository factory.

Dependency Injection

The Repository pattern works well with dependency injection, allowing for better testability and flexibility.

Integration with Dependency Injection

See the Dependency Injection example for a complete, runnable example of how to use repositories with dependency injection.

Best Practices

  1. Interface Segregation: Keep repository interfaces focused on specific entity types.

  2. Dependency Inversion: Depend on repository interfaces, not concrete implementations.

  3. Testability: Use in-memory repository implementations for testing.

  4. Transaction Management: Consider adding transaction support for operations that span multiple repositories.

  5. Error Handling: Use domain-specific errors for repository operations.

  6. Context Usage: Always pass a context to repository methods for cancellation and timeout support.

  7. Repository Factory: Use a factory to create and manage repositories when dealing with multiple entity types.

  8. Concurrency: Ensure thread safety in repository implementations.

License

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

Documentation

Overview

Package repository provides generic repository interfaces that can be used across different applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Repository

type Repository[T any] interface {
	// GetByID retrieves an entity by its ID
	GetByID(ctx context.Context, id string) (T, error)

	// GetAll retrieves all entities
	GetAll(ctx context.Context) ([]T, error)

	// Save persists an entity
	Save(ctx context.Context, entity T) error
}

Repository is a generic repository interface for entity persistence operations. This interface represents a port in the Hexagonal Architecture pattern. It's defined in the pkg layer but implemented in the infrastructure layer.

type RepositoryFactory

type RepositoryFactory interface {
	// GetRepository returns a repository for the given entity type
	GetRepository() any
}

RepositoryFactory is an interface for creating repositories

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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