example_repository

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 5 Imported by: 0

README

Repository Package Examples

This directory contains examples demonstrating how to use the repository package, which provides utilities for implementing the repository pattern in Go applications. The repository pattern abstracts data access logic, making it easier to switch between different data storage implementations and improving testability.

Examples

1. Basic Repository Example

basic_repository_example.go

Demonstrates the basic implementation and usage of a repository.

Key concepts:

  • Creating a domain entity
  • Implementing a repository interface
  • Using an in-memory repository implementation
  • Performing basic CRUD operations
  • Using generics for type-safe repository operations
2. Dependency Injection Example

dependency_injection_example.go

Shows how to use dependency injection with repositories.

Key concepts:

  • Injecting repository dependencies
  • Decoupling business logic from data access
  • Using interfaces for loose coupling
  • Testing with mock repositories
  • Configuring repositories at runtime
3. Repository Factory Example

repository_factory_example.go

Demonstrates how to use a repository factory to create repositories.

Key concepts:

  • Creating a repository factory
  • Managing multiple repository types
  • Centralizing repository creation logic
  • Configuring repositories based on application settings
  • Handling repository initialization

Running the Examples

To run any of the examples, use the go run command:

go run examples/repository/basic_repository_example.go

Additional Resources

For more information about the repository package, see the repository package documentation.

Documentation

Overview

Example of basic repository implementation

Example of integration with dependency injection

Example of using repository factory

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DIInMemoryUserRepository

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

DIInMemoryUserRepository implements the Repository interface for DIUser entities

func NewDIInMemoryUserRepository

func NewDIInMemoryUserRepository() *DIInMemoryUserRepository

NewDIInMemoryUserRepository creates a new in-memory user repository

func (*DIInMemoryUserRepository) GetAll

func (r *DIInMemoryUserRepository) GetAll(ctx context.Context) ([]DIUser, error)

GetAll retrieves all users

func (*DIInMemoryUserRepository) GetByID

func (r *DIInMemoryUserRepository) GetByID(ctx context.Context, id string) (DIUser, error)

GetByID retrieves a user by ID

func (*DIInMemoryUserRepository) Save

Save persists a user

type DIUser

type DIUser struct {
	ID       string
	Username string
	Email    string
}

DIUser is a domain entity

type FactoryInMemoryUserRepository

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

FactoryInMemoryUserRepository implements the Repository interface for FactoryUser entities

func NewFactoryInMemoryUserRepository

func NewFactoryInMemoryUserRepository() *FactoryInMemoryUserRepository

NewFactoryInMemoryUserRepository creates a new in-memory user repository

func (*FactoryInMemoryUserRepository) GetAll

GetAll retrieves all users

func (*FactoryInMemoryUserRepository) GetByID

GetByID retrieves a user by ID

func (*FactoryInMemoryUserRepository) Save

Save persists a user

type FactoryUser

type FactoryUser struct {
	ID       string
	Username string
	Email    string
}

FactoryUser is a domain entity

type InMemoryOrderRepository

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

InMemoryOrderRepository implements the Repository interface for Order entities

func NewInMemoryOrderRepository

func NewInMemoryOrderRepository() *InMemoryOrderRepository

NewInMemoryOrderRepository creates a new in-memory order repository

func (*InMemoryOrderRepository) GetAll

func (r *InMemoryOrderRepository) GetAll(ctx context.Context) ([]Order, error)

GetAll retrieves all orders

func (*InMemoryOrderRepository) GetByID

func (r *InMemoryOrderRepository) GetByID(ctx context.Context, id string) (Order, error)

GetByID retrieves an order by ID

func (*InMemoryOrderRepository) Save

func (r *InMemoryOrderRepository) Save(ctx context.Context, order Order) error

Save persists an order

type InMemoryUserRepository

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

InMemoryUserRepository implements the Repository interface for User entities

func NewInMemoryUserRepository

func NewInMemoryUserRepository() *InMemoryUserRepository

NewInMemoryUserRepository creates a new in-memory user repository

func (*InMemoryUserRepository) GetAll

func (r *InMemoryUserRepository) GetAll(ctx context.Context) ([]User, error)

GetAll retrieves all users

func (*InMemoryUserRepository) GetByID

func (r *InMemoryUserRepository) GetByID(ctx context.Context, id string) (User, error)

GetByID retrieves a user by ID

func (*InMemoryUserRepository) Save

func (r *InMemoryUserRepository) Save(ctx context.Context, user User) error

Save persists a user

type Order

type Order struct {
	ID     string
	UserID string
	Amount float64
}

Order is another domain entity

type RepositoryFactoryImpl

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

RepositoryFactoryImpl implements the RepositoryFactory interface

func NewRepositoryFactory

func NewRepositoryFactory(
	userRepo repository.Repository[FactoryUser],
	orderRepo repository.Repository[Order],
) *RepositoryFactoryImpl

NewRepositoryFactory creates a new repository factory

func (*RepositoryFactoryImpl) GetOrderRepository

func (f *RepositoryFactoryImpl) GetOrderRepository() repository.Repository[Order]

GetOrderRepository returns the order repository

func (*RepositoryFactoryImpl) GetRepository

func (f *RepositoryFactoryImpl) GetRepository() any

GetRepository returns a repository for the given entity type

func (*RepositoryFactoryImpl) GetUserRepository

func (f *RepositoryFactoryImpl) GetUserRepository() repository.Repository[FactoryUser]

GetUserRepository returns the user repository

type User

type User struct {
	ID       string
	Username string
	Email    string
	Active   bool
}

User is a domain entity

type UserService

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

UserService is a domain service that uses a repository

func NewUserService

func NewUserService(userRepo repository.Repository[DIUser]) *UserService

NewUserService creates a new user service

func (*UserService) CreateUser

func (s *UserService) CreateUser(ctx context.Context, username, email string) (DIUser, error)

CreateUser creates a new user

func (*UserService) GetUser

func (s *UserService) GetUser(ctx context.Context, id string) (DIUser, error)

GetUser retrieves a user by ID

Jump to

Keyboard shortcuts

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