forked from agiledragon/ddd-sample-in-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cb4ec30
Showing
16 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# temp | ||
tmp/ | ||
output/ | ||
build/ | ||
|
||
# mac | ||
.DS_Store | ||
|
||
# python | ||
*.pyc | ||
|
||
# clion | ||
.idea/ | ||
*.xml | ||
|
||
# eclipse | ||
.settings/ | ||
.project | ||
.cproject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# ddd-sample-in-golang | ||
|
||
ddd sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/domain/service" | ||
) | ||
|
||
func CreateCargo(cargoId string, afterDays uint) { | ||
service.GetCargoServiceInstance().Create(cargoId, afterDays) | ||
} | ||
|
||
func DelayCargo(cargoId string, days uint) { | ||
service.GetCargoServiceInstance().Delay(cargoId, days) | ||
} | ||
|
||
func GetCargoAfterDays(cargoId string) uint { | ||
return service.GetCargoServiceInstance().GetAfterDays(cargoId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package base | ||
|
||
type AggregateRoot struct { | ||
Entity | ||
} | ||
|
||
func NewAggregateRoot(id string) AggregateRoot { | ||
return AggregateRoot{NewEntity(id)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package base | ||
|
||
type Entity struct { | ||
Id string | ||
} | ||
|
||
func NewEntity(id string) Entity { | ||
return Entity{Id: id} | ||
} | ||
|
||
func (t *Entity) GetId() string { | ||
return t.Id | ||
} | ||
|
||
func (t *Entity) Equal(other *Entity) bool { | ||
return t.Id == other.Id | ||
} | ||
|
||
func (t *Entity) NotEqual(other *Entity) bool { | ||
return !t.Equal(other) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package base | ||
|
||
type ValueObject struct { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/domain/model/base" | ||
) | ||
|
||
type Cargo struct { | ||
base.AggregateRoot | ||
Delivery Delivery | ||
} | ||
|
||
func newCargo(cargoId string, delivery Delivery) *Cargo { | ||
return &Cargo{AggregateRoot: base.NewAggregateRoot(cargoId), Delivery: delivery} | ||
} | ||
|
||
func (t *Cargo) Delay(days uint) { | ||
afterDays := t.GetAfterDays() + days | ||
t.Delivery = Delivery{AfterDays: afterDays} | ||
} | ||
|
||
func (t *Cargo) GetAfterDays() uint { | ||
return t.Delivery.AfterDays | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
type CargoFactory struct { | ||
|
||
} | ||
|
||
func (t CargoFactory) Create(cargoId string, afterDays uint) *Cargo { | ||
delivery := Delivery{AfterDays: afterDays} | ||
return newCargo(cargoId, delivery) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package model | ||
|
||
type CargoProvider interface { | ||
Confirm(cargo *Cargo) | ||
} | ||
|
||
var p CargoProvider = nil | ||
|
||
func SetCargoProvider(provider CargoProvider) { | ||
p = provider | ||
} | ||
|
||
func GetCargoProvider() CargoProvider { | ||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package model | ||
|
||
type CargoRepo interface { | ||
Add(cargo *Cargo) | ||
Get(cargoId string) *Cargo | ||
Update(cargo *Cargo) | ||
Remove(cargoId string) | ||
} | ||
|
||
var r CargoRepo = nil | ||
|
||
func SetCargoRepo(repo CargoRepo) { | ||
r = repo | ||
} | ||
|
||
func GetCargoRepo() CargoRepo { | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/domain/model/base" | ||
) | ||
|
||
type Delivery struct { | ||
base.ValueObject | ||
AfterDays uint | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-sample-in-golang/domain/model" | ||
"sync" | ||
) | ||
|
||
type CargoService struct { | ||
repo model.CargoRepo | ||
provider model.CargoProvider | ||
} | ||
|
||
var cs = &CargoService{} | ||
var once sync.Once | ||
func GetCargoServiceInstance() *CargoService { | ||
once.Do(func() { | ||
cs.repo = model.GetCargoRepo() | ||
cs.provider = model.GetCargoProvider() | ||
}) | ||
return cs | ||
} | ||
|
||
func (t *CargoService) Create(cargoId string, afterDays uint) { | ||
cargo := model.CargoFactory{}.Create(cargoId, afterDays) | ||
t.repo.Add(cargo) | ||
t.provider.Confirm(cargo) | ||
} | ||
|
||
func (t *CargoService) Delay(cargoId string, days uint) { | ||
cargo := t.repo.Get(cargoId) | ||
if cargo != nil { | ||
cargo.Delay(days) | ||
t.repo.Update(cargo) | ||
t.provider.Confirm(cargo) | ||
} | ||
} | ||
|
||
func (t *CargoService) GetAfterDays(cargoId string) uint { | ||
cargo := t.repo.Get(cargoId) | ||
if cargo != nil { | ||
return cargo.GetAfterDays() | ||
} | ||
panic("invalid cargoId") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ft | ||
|
||
import ( | ||
"testing" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"github.com/agiledragon/ddd-sample-in-golang/domain/model" | ||
"github.com/agiledragon/ddd-sample-in-golang/app/service" | ||
) | ||
|
||
type SpyCargoProvider struct { | ||
cargoId string | ||
afterDays uint | ||
} | ||
|
||
func (t *SpyCargoProvider) Confirm(cargo *model.Cargo) { | ||
t.cargoId = cargo.GetId() | ||
t.afterDays = cargo.GetAfterDays() | ||
} | ||
|
||
type FakeCargoRepo struct { | ||
cargoes map[string]*model.Cargo | ||
} | ||
|
||
func (t *FakeCargoRepo) Add(cargo *model.Cargo) { | ||
t.cargoes[cargo.GetId()] = cargo | ||
} | ||
|
||
func (t *FakeCargoRepo) Get(cargoId string) *model.Cargo { | ||
return t.cargoes[cargoId] | ||
} | ||
|
||
func (t *FakeCargoRepo) Update(cargo *model.Cargo) { | ||
t.cargoes[cargo.GetId()] = cargo | ||
} | ||
|
||
func (t *FakeCargoRepo) Remove(cargoId string) { | ||
delete(t.cargoes, cargoId) | ||
} | ||
|
||
func TestCargo(t *testing.T) { | ||
provider := &SpyCargoProvider{} | ||
model.SetCargoProvider(provider) | ||
repo := &FakeCargoRepo{make(map[string]*model.Cargo)} | ||
model.SetCargoRepo(repo) | ||
const cargoId = "1" | ||
|
||
Convey("TestCargo", t, func() { | ||
Convey("create cargo", func() { | ||
const afterDays = 10 | ||
service.CreateCargo(cargoId, afterDays) | ||
So(provider.cargoId, ShouldEqual, cargoId) | ||
So(provider.afterDays, ShouldEqual, afterDays) | ||
So(service.GetCargoAfterDays(cargoId), ShouldEqual, afterDays) | ||
}) | ||
|
||
Convey("delay cargo", func() { | ||
const afterDays = 20 | ||
const days = 5 | ||
service.CreateCargo(cargoId, afterDays) | ||
service.DelayCargo(cargoId, days) | ||
So(provider.cargoId, ShouldEqual, cargoId) | ||
So(provider.afterDays, ShouldEqual, afterDays + days) | ||
So(service.GetCargoAfterDays(cargoId), ShouldEqual, afterDays + days) | ||
|
||
}) | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package infra | ||
|
||
import "github.com/agiledragon/ddd-sample-in-golang/domain/model" | ||
|
||
type CargoProviderImpl struct { | ||
|
||
} | ||
|
||
func (t *CargoProviderImpl) Confirm(cargo *model.Cargo) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package infra | ||
|
||
import "github.com/agiledragon/ddd-sample-in-golang/domain/model" | ||
|
||
type CargoRepoImpl struct { | ||
|
||
} | ||
|
||
func (t *CargoRepoImpl) Add(cargo *model.Cargo) { | ||
|
||
} | ||
|
||
func (t *CargoRepoImpl) Get(cargoId string) *model.Cargo { | ||
return nil | ||
} | ||
|
||
func (t *CargoRepoImpl) Update(cargo *model.Cargo) { | ||
|
||
} | ||
|
||
func (t *CargoRepoImpl) Remove(CargoId string) { | ||
|
||
} |