Documentation
¶
Overview ¶
Package repository provides generic repository interfaces for data persistence operations.
This package implements the Repository pattern, which abstracts the data access layer and provides a consistent interface for working with different data sources. It aligns with the Hexagonal Architecture (Ports and Adapters) pattern by defining ports (interfaces) that can be implemented by various adapters.
The package is designed to be used with any entity type through Go generics, allowing for type-safe repository operations without code duplication.
Key components:
- Repository: A generic interface for CRUD operations on entities
- RepositoryFactory: An interface for creating repositories
The Repository pattern provides several benefits:
- Decouples business logic from data access implementation
- Simplifies testing through mocking
- Enables switching between different data sources with minimal code changes
- Centralizes data access logic
Example usage:
// Define an entity
type User struct {
ID string
Name string
Age int
}
// Use the repository interface
func ProcessUser(ctx context.Context, repo repository.Repository[User], userID string) error {
// Get user from repository
user, err := repo.GetByID(ctx, userID)
if err != nil {
return err
}
// Process user...
user.Age++
// Save changes
return repo.Save(ctx, user)
}
// Implementation would be provided by an adapter in the infrastructure layer
type MongoUserRepository struct {
// MongoDB client and collection
}
func (r *MongoUserRepository) GetByID(ctx context.Context, id string) (User, error) {
// Implementation using MongoDB
}
func (r *MongoUserRepository) GetAll(ctx context.Context) ([]User, error) {
// Implementation using MongoDB
}
func (r *MongoUserRepository) Save(ctx context.Context, user User) error {
// Implementation using MongoDB
}
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. // It returns the entity if found, or an error if the entity doesn't exist // or if there was a problem accessing the data store. // // The context parameter can be used to control cancellation and timeouts. GetByID(ctx context.Context, id string) (T, error) // GetAll retrieves all entities of type T. // It returns a slice of entities, which may be empty if no entities exist, // or an error if there was a problem accessing the data store. // // The context parameter can be used to control cancellation and timeouts. GetAll(ctx context.Context) ([]T, error) // Save persists an entity to the data store. // For new entities, this typically creates a new record. // For existing entities, this updates the existing record. // // It returns an error if there was a problem saving the entity. // The context parameter can be used to control cancellation and timeouts. 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 domain layer but implemented in the infrastructure layer.
The generic type parameter T represents the entity type that the repository manages. This allows for type-safe repository operations without code duplication.
type RepositoryFactory ¶
type RepositoryFactory interface {
// GetRepository returns a repository instance.
// The returned repository should be cast to the appropriate type
// by the caller, typically Repository[T] for some entity type T.
//
// Example:
// factory := NewMyRepositoryFactory()
// repo, ok := factory.GetRepository().(Repository[User])
// if !ok {
// return errors.New("invalid repository type")
// }
GetRepository() any
}
RepositoryFactory is an interface for creating repositories. This interface follows the Factory pattern and is used to abstract the creation of repository instances, allowing for dependency injection and easier testing.