Skip to content

Commit

Permalink
Stop using Gorm, add persistence layer components and new mechanic of…
Browse files Browse the repository at this point in the history
… domain event publishing
  • Loading branch information
tonybka committed Apr 13, 2023
1 parent a754df0 commit 6c70569
Show file tree
Hide file tree
Showing 28 changed files with 743 additions and 134 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Duy Phung

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'
services:
db:
image: postgres:14.1-alpine
container_name: postgresdb
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '54320:5432'
16 changes: 14 additions & 2 deletions domain/entity/base_entity.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package entity

import (
"time"

"github.com/tonybka/go-base-ddd/domain/event"
)

type BaseEntity struct {
ID uint
ID uint
CreatedAt time.Time
UpdatedAt time.Time
}

func NewBaseEntity(id uint) BaseEntity {
return BaseEntity{ID: id}
return BaseEntity{ID: id, CreatedAt: time.Now()}
}

func (base *BaseEntity) AddEvent(tableName string, domainEvent event.IBaseDomainEvent) {
event.EventSource.AddEvent(tableName, domainEvent)
}
66 changes: 64 additions & 2 deletions domain/event/base_domain_event.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,72 @@
package event

import "time"
import (
"encoding/json"
"time"

"github.com/google/uuid"
)

type IBaseDomainEvent interface {
ID() uuid.UUID
AggregateID() uint
Aggregate() string
Name() string
ToJson() (string, error)
ID() string
OccurredAt() time.Time
Data() interface{}
}

type domainEventProps struct {
ID uuid.UUID
AggregateID uint
Aggregate string
Name string
OccuredAt time.Time
Data interface{}
}

type BaseDomainEvent struct {
props domainEventProps
}

func NewBaseDomainEvent(aggregate string, aggregateID uint, name string, data interface{}) *BaseDomainEvent {
return &BaseDomainEvent{
props: domainEventProps{
ID: uuid.New(),
Aggregate: aggregate,
AggregateID: aggregateID,
Name: name,
OccuredAt: time.Now(),
Data: data,
},
}
}

func (event *BaseDomainEvent) ID() uuid.UUID {
return event.props.ID
}

func (event *BaseDomainEvent) AggregateID() uint {
return event.props.AggregateID
}

func (event *BaseDomainEvent) Aggregate() string {
return event.props.Aggregate
}

func (event *BaseDomainEvent) ToJson() (string, error) {
b, err := json.Marshal(event.props)
if err != nil {
return "", nil
}
return string(b), nil
}

func (event *BaseDomainEvent) OccurredAt() time.Time {
return event.props.OccuredAt
}

func (event *BaseDomainEvent) Data() interface{} {
return event.props.Data
}
6 changes: 1 addition & 5 deletions domain/event/domain_event_publisher.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package event

import (
"gorm.io/gorm"
)

// =----------------------
// DomainEventPublisher
// =----------------------
Expand All @@ -25,7 +21,7 @@ func (publisher *DomainEventPublisher) RegisterSubscriber(event IBaseDomainEvent
}

// Publish notifies all registered subscribers about the given events
func (publisher *DomainEventPublisher) Publish(tx *gorm.DB, events ...IBaseDomainEvent) error {
func (publisher *DomainEventPublisher) Publish(events ...IBaseDomainEvent) error {
for _, event := range events {
eventName := event.Name()

Expand Down
37 changes: 0 additions & 37 deletions domain/event/domain_event_publisher_test.go

This file was deleted.

27 changes: 27 additions & 0 deletions domain/event/event_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package event

var EventSource *EventStorage

func init() {
if EventSource == nil {
EventSource = &EventStorage{
pendingEvents: make(map[string][]IBaseDomainEvent),
}
}
}

type EventStorage struct {
pendingEvents map[string][]IBaseDomainEvent
}

func (storage *EventStorage) AddEvent(dataModel string, domainEvent IBaseDomainEvent) {
if storage.pendingEvents[dataModel] == nil {
storage.pendingEvents[dataModel] = make([]IBaseDomainEvent, 0)
}

storage.pendingEvents[dataModel] = append(storage.pendingEvents[dataModel], domainEvent)
}

func (storage *EventStorage) GetPendingEvents(dataModel string) []IBaseDomainEvent {
return storage.pendingEvents[dataModel]
}
33 changes: 0 additions & 33 deletions domain/event/mock_domain_event.go

This file was deleted.

10 changes: 0 additions & 10 deletions domain/event/mock_event_handler.go

This file was deleted.

16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ go 1.18

require (
github.com/google/uuid v1.3.0
gorm.io/gorm v1.24.2
github.com/jackc/pgx/v5 v5.3.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/stretchr/testify v1.8.1
gorm.io/driver/sqlite v1.4.3
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.0 // indirect
github.com/stretchr/testify v1.8.2
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
40 changes: 25 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU=
github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8=
github.com/jackc/puddle/v2 v2.2.0 h1:RdcDk92EJBuBS55nQMMYFXTxwstHug4jkhT5pq8VxPk=
github.com/jackc/puddle/v2 v2.2.0/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0=
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
Loading

0 comments on commit 6c70569

Please sign in to comment.