Skip to content

Commit

Permalink
update Context
Browse files Browse the repository at this point in the history
  • Loading branch information
xiandong-italki committed Sep 26, 2022
1 parent 386979a commit 0c4440d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
*.out

# goland
.idea
.idea

internal
1 change: 1 addition & 0 deletions aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func NewAggregateId() (id int64) {

func ApplyAggregateChange(ctx context.Context, aggregate Aggregate, change aggregateChange) {
aggregate.Apply(aggregate, change)
return
}

type Aggregate interface {
Expand Down
21 changes: 16 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ package gddd

import (
"context"
"database/sql"
)

type Context struct {
ctx context.Context
tx *sql.Tx
ctx context.Context
repository *Repository
}

func (c *Context) Apply(aggregate Aggregate, change aggregateChange) error {
func NewContext(ctx context.Context, repository *Repository) Context {
return Context{ctx: ctx, repository: repository}
}

func (c *Context) Load(aggregate Aggregate) (has bool, err error) {
return c.repository.Load(c.ctx, aggregate)
}

func (c *Context) Save(aggregates ...Aggregate) (ok bool, err error) {
return c.repository.Save(c.ctx, aggregates...)
}

func (c *Context) Apply(aggregate Aggregate, change aggregateChange) {
ApplyAggregateChange(c.ctx, aggregate, change)
return nil
return
}
5 changes: 5 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ type Repository struct {
saveListener RepositorySaveListener
}

func (r *Repository) SetSaveListener(ctx context.Context, saveListener RepositorySaveListener) (err error) {
r.saveListener = saveListener
return
}

func (r *Repository) RegisterAggregates(ctx context.Context, aggregates ...Aggregate) (err error) {
for _, aggregate := range aggregates {
aggregateName := getAggregateName(aggregate)
Expand Down

0 comments on commit 0c4440d

Please sign in to comment.