From 0c4440d6172cc415e1189554986437e3e0a7a940 Mon Sep 17 00:00:00 2001 From: xiandong Date: Mon, 26 Sep 2022 18:41:25 +0800 Subject: [PATCH] update Context --- .gitignore | 4 +++- aggregate.go | 1 + context.go | 21 ++++++++++++++++----- repository.go | 5 +++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index db67165..f0d6180 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,6 @@ *.out # goland -.idea \ No newline at end of file +.idea + +internal diff --git a/aggregate.go b/aggregate.go index 1655c66..f258a77 100644 --- a/aggregate.go +++ b/aggregate.go @@ -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 { diff --git a/context.go b/context.go index 29d590c..25a38bb 100644 --- a/context.go +++ b/context.go @@ -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 } diff --git a/repository.go b/repository.go index 34d08a0..0312749 100644 --- a/repository.go +++ b/repository.go @@ -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)