/repository-pattern-go

The sample implementation of Repository Pattern in go with gorm

Primary LanguageGo

Repository Pattern in Go with gorm

The sample implementation of Repository Pattern in go with gorm

Let's first understand what is Repository Pattern

IMAGE ALT TEXT

Key Notes from P of EAA Catalog - Repository

  • isolates domain objects from details of the database access code
  • acting like an in-memory domain object collection

UnitOfWork usage sample

work := gorm_repository.NewGormUnitOfWork(db)
author := repository.Author{Name: "felix", Email: "abcd@gmail.com"}

err = work.AuthorRepo().Add(&author)
if err != nil {
    work.Rollback()
    return
}
course := repository.Course{
    Name:     "Database 1",
    AuthorID: author.ID,
}
err = work.CourseRepo().Add(&course)
if err != nil {
    work.Rollback()
    return
}
err = work.Complete() // commit changes
if err != nil {
    panic(err)
}