Cookie Consent by Free Privacy Policy Generator 📌 Implementing the repository pattern in Go with both in-memory and MySQL repositories


✅ Implementing the repository pattern in Go with both in-memory and MySQL repositories


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

  1. - We define the User struct representing a user entity.
  2. - We define the UserRepository interface with methods for managing users.
  3. - We implement InMemoryUserRepository and MySQLUserRepository to provide in-memory and MySQL-based repositories, respectively.
  4. - NewInMemoryUserRepository and NewMySQLUserRepository are constructor functions for creating instances of the respective repositories.
  5. - We demonstrate how to use both repositories in the main function by inserting users, getting a user by ID, and getting all users.
package main

import (
    "database/sql"
    "errors"
    "fmt"

    _ "github.com/go-sql-driver/mysql"
)

// User represents a user entity
type User struct {
    ID       int
    Username string
    Email    string
}

// UserRepository defines the methods a user repository must implement
type UserRepository interface {
    Insert(user *User) error
    GetByID(id int) (*User, error)
    GetAll() ([]*User, error)
}

// InMemoryUserRepository is an in-memory implementation of UserRepository
type InMemoryUserRepository struct {
    users []*User
}

// NewInMemoryUserRepository creates a new instance of InMemoryUserRepository
func NewInMemoryUserRepository() *InMemoryUserRepository {
    return &InMemoryUserRepository{
        users: make([]*User, 0),
    }
}

// Insert inserts a new user into the repository
func (repo *InMemoryUserRepository) Insert(user *User) error {
    repo.users = append(repo.users, user)
    return nil
}

// GetByID retrieves a user by its ID from the repository
func (repo *InMemoryUserRepository) GetByID(id int) (*User, error) {
    for _, user := range repo.users {
        if user.ID == id {
            return user, nil
        }
    }
    return nil, errors.New("user not found")
}

// GetAll retrieves all users from the repository
func (repo *InMemoryUserRepository) GetAll() ([]*User, error) {
    return repo.users, nil
}

// MySQLUserRepository is a MySQL implementation of UserRepository
type MySQLUserRepository struct {
    db *sql.DB
}

// NewMySQLUserRepository creates a new instance of MySQLUserRepository
func NewMySQLUserRepository(dataSourceName string) (*MySQLUserRepository, error) {
    db, err := sql.Open("mysql", dataSourceName)
    if err != nil {
        return nil, err
    }
    return &MySQLUserRepository{
        db: db,
    }, nil
}

// Insert inserts a new user into the MySQL repository
func (repo *MySQLUserRepository) Insert(user *User) error {
    _, err := repo.db.Exec("INSERT INTO users (id, username, email) VALUES (?, ?, ?)", user.ID, user.Username, user.Email)
    if err != nil {
        return err
    }
    return nil
}

// GetByID retrieves a user by its ID from the MySQL repository
func (repo *MySQLUserRepository) GetByID(id int) (*User, error) {
    row := repo.db.QueryRow("SELECT id, username, email FROM users WHERE id = ?", id)
    user := &User{}
    err := row.Scan(&user.ID, &user.Username, &user.Email)
    if err != nil {
        return nil, err
    }
    return user, nil
}

// GetAll retrieves all users from the MySQL repository
func (repo *MySQLUserRepository) GetAll() ([]*User, error) {
    rows, err := repo.db.Query("SELECT id, username, email FROM users")
    if err != nil {
        return nil, err
    }
    defer rows.Close()

    var users []*User
    for rows.Next() {
        user := &User{}
        err := rows.Scan(&user.ID, &user.Username, &user.Email)
        if err != nil {
            return nil, err
        }
        users = append(users, user)
    }
    return users, nil
}

func main() {
    // Example usage of in-memory repository
    memRepo := NewInMemoryUserRepository()
    memRepo.Insert(&User{ID: 1, Username: "user1", Email: "[email protected]"})
    memRepo.Insert(&User{ID: 2, Username: "user2", Email: "[email protected]"})

    user, err := memRepo.GetByID(1)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("In-Memory User:", user)
    }

    allUsers, err := memRepo.GetAll()
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("In-Memory All Users:", allUsers)
    }

    // Example usage of MySQL repository
    mysqlRepo, err := NewMySQLUserRepository("user:password@tcp(127.0.0.1:3306)/database_name")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    mysqlRepo.Insert(&User{ID: 3, Username: "user3", Email: "[email protected]"})
    mysqlRepo.Insert(&User{ID: 4, Username: "user4", Email: "[email protected]"})

    user, err = mysqlRepo.GetByID(3)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("MySQL User:", user)
    }

    allUsers, err = mysqlRepo.GetAll()
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("MySQL All Users:", allUsers)
    }
}

...

✅ Implementing the Cached Repository Pattern in C#


📈 34.89 Punkte

✅ Complete Guide to the Django Services and Repositories Design Pattern with the Django REST Framework


📈 27.71 Punkte

✅ Safest Kodi Repositories: Best Add-on Repositories Ranked High for Trust


📈 26.97 Punkte

✅ Implementing Horizontally Scalable Transactional Outbox Pattern with .NET 8 and Kafka: A Practical Guide


📈 26.68 Punkte

✅ Go program pattern 02: Implementing Class Inheritance and Method Overriding through Composition


📈 26.68 Punkte

✅ [dos] Pdfium - Out-of-Bounds Read with Shading Pattern Backed by Pattern Colorspace


📈 25.39 Punkte

✅ Observer Pattern: Was steckt hinter dem Observer Design Pattern?


📈 25.39 Punkte

✅ Factory Pattern: Alle Informationen zum Factory Method Pattern


📈 25.39 Punkte

✅ C# Pattern Matching Inside Out: Kompakter und prägnanter C#-Code durch Pattern Matching


📈 25.39 Punkte

✅ Neu in .NET 7 [5]: List Pattern und Slice Pattern mit C# 11


📈 25.39 Punkte

✅ A transição do Higher-Order Component pattern para o React Hooks pattern


📈 25.39 Punkte

✅ Go program pattern 01: Functional Options Pattern


📈 25.39 Punkte

✅ Design Pattern #2 - Facade Pattern


📈 25.39 Punkte

✅ Design Pattern #3 - Observer Pattern


📈 25.39 Punkte

✅ Design Pattern #4 - Publisher/Subscriber Pattern


📈 25.39 Punkte

✅ Singleton-Pattern | Javascript Design Pattern Simplified | Part 1


📈 25.39 Punkte

✅ Observer-Pattern | Javascript Design Pattern Simplified | Part 3


📈 25.39 Punkte

✅ Factory-Pattern | Javascript Design Pattern Simplified | Part 2


📈 25.39 Punkte

✅ Decorator-Pattern | Javascript Design Pattern Simplified | Part 5


📈 25.39 Punkte

✅ Module-Pattern | Javascript Design Pattern Simplified | Part 4


📈 25.39 Punkte

✅ Pub/Sub pattern vs Observer Pattern: what's the difference?


📈 25.39 Punkte

✅ Design Pattern #5 - Adapter Pattern


📈 25.39 Punkte

✅ Enhancing Resiliency: Implementing the Circuit Breaker Pattern for Strong Serverless Architecture on AWS


📈 25.15 Punkte

✅ Implementing Saga Pattern in Microservices with .NET Core: A Practical Guide


📈 25.15 Punkte

✅ Implementing the Viewer Pattern in Federated GraphQL APIs


📈 25.15 Punkte

✅ C# Delegates In Practice — Implementing Observer Pattern With Delegates


📈 25.15 Punkte

✅ Implementing Reversible Actions using the Command Pattern


📈 25.15 Punkte

✅ Implementing Composition Pattern in React Applications with RadixSlot


📈 25.15 Punkte

✅ Interesting discussion thread on ycombinator about both docker including devs from both


📈 24.81 Punkte

✅ Interesting discussion thread on ycombinator about both Docker & LXD including devs from both


📈 24.81 Punkte











matomo

Datei nicht gefunden!