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
1 parent
281e250
commit e9cd36c
Showing
17 changed files
with
387 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,21 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/service" | ||
) | ||
|
||
func CreateAccount(accountId string, amount uint, phoneNumber string) { | ||
service.GetAccountService().Create(accountId, amount, phoneNumber) | ||
} | ||
|
||
func TransferMoney(srcAccountId, dstAccountId string, amount uint) { | ||
service.GetAccountService().TransferMoney(srcAccountId, dstAccountId, amount) | ||
} | ||
|
||
func GetAmount(accountId string) (uint, error) { | ||
return service.GetAccountService().GetAmount(accountId) | ||
} | ||
|
||
func DestroyAccount(accountId string) { | ||
service.GetAccountService().Destroy(accountId) | ||
} |
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,22 @@ | ||
package model | ||
|
||
import "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base" | ||
|
||
type Account struct { | ||
base.AggregateRoot | ||
balance *balance | ||
phone *phone | ||
MoneySource *moneySource | ||
MoneyDestination *moneyDestination | ||
} | ||
|
||
func newAccount(accountId string, balance *balance, phone *phone) *Account { | ||
account := &Account{ | ||
AggregateRoot: base.NewAggregateRoot(accountId), | ||
balance: balance, | ||
phone: phone, | ||
} | ||
account.MoneySource = newMoneySource(accountId, balance, phone) | ||
account.MoneyDestination = newMoneyDestination(accountId, balance, phone) | ||
return account | ||
} |
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 AccountFactory struct { | ||
} | ||
|
||
func (this AccountFactory) Create(accountId string, amount uint, phoneNumber string) *Account { | ||
b := newBalance(amount) | ||
p := newPhone(accountId, phoneNumber) | ||
return newAccount(accountId, b, 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,17 @@ | ||
package model | ||
|
||
var repoInst AccountRepo | ||
|
||
type AccountRepo interface { | ||
Add(account *Account) | ||
Get(accountId string) *Account | ||
Remove(accountId string) | ||
} | ||
|
||
func SetAccountRepo(repo AccountRepo) { | ||
repoInst = repo | ||
} | ||
|
||
func GetAccountRepo() AccountRepo { | ||
return repoInst | ||
} |
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,24 @@ | ||
package model | ||
|
||
import "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base" | ||
|
||
type balance struct { | ||
base.ValueObject | ||
amount uint | ||
} | ||
|
||
func newBalance(amount uint) *balance { | ||
return &balance{amount: amount} | ||
} | ||
|
||
func (this *balance) increase(amount uint) { | ||
this.amount += amount | ||
} | ||
|
||
func (this *balance) decrease(amount uint) { | ||
this.amount -= amount | ||
} | ||
|
||
func (this *balance) get() uint { | ||
return this.amount | ||
} |
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) Id() 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,4 @@ | ||
package base | ||
|
||
type Role 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,4 @@ | ||
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,25 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base" | ||
) | ||
|
||
type moneyDestination struct { | ||
base.Role | ||
accountId string | ||
balance *balance | ||
phone *phone | ||
} | ||
|
||
func newMoneyDestination(accountId string, balance *balance, phone *phone) *moneyDestination { | ||
return &moneyDestination{accountId: accountId, balance: balance, phone: phone} | ||
} | ||
|
||
func (this *moneyDestination) getAccountId() string { | ||
return this.accountId | ||
} | ||
|
||
func (this *moneyDestination) transferMoneyFrom(srcAccountId string, amount uint) { | ||
this.balance.increase(amount) | ||
this.phone.sendTransferFromMsg(srcAccountId, amount) | ||
} |
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,29 @@ | ||
package model | ||
|
||
import ( | ||
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base" | ||
) | ||
|
||
type moneySource struct { | ||
base.Role | ||
accountId string | ||
balance *balance | ||
phone *phone | ||
} | ||
|
||
func newMoneySource(accountId string, balance *balance, phone *phone) *moneySource { | ||
return &moneySource{accountId: accountId, balance: balance, phone: phone} | ||
} | ||
|
||
func (this *moneySource) TransferMoneyTo(dst *moneyDestination, amount uint) { | ||
if this.balance.get() < amount { | ||
panic("insufficient money!") | ||
} | ||
dst.transferMoneyFrom(this.accountId, amount) | ||
this.balance.decrease(amount) | ||
this.phone.sendTransferToMsg(dst.getAccountId(), amount) | ||
} | ||
|
||
func (this *moneySource) GetAmount() uint { | ||
return this.balance.get() | ||
} |
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,26 @@ | ||
package model | ||
|
||
import ( | ||
"fmt" | ||
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model/base" | ||
) | ||
|
||
type phone struct { | ||
base.ValueObject | ||
accountId string | ||
phoneNumber string | ||
} | ||
|
||
func newPhone(accountId, phoneNumber string) *phone { | ||
return &phone{accountId: accountId, phoneNumber: phoneNumber} | ||
} | ||
|
||
func (this *phone) sendTransferToMsg(dstAccountId string, amount uint) { | ||
msg := fmt.Sprintf("%s send %d money to %s", this.accountId, amount, dstAccountId) | ||
GetPhoneProvider().Send(this.phoneNumber, msg) | ||
} | ||
|
||
func (this *phone) sendTransferFromMsg(srcAccountId string, amount uint) { | ||
msg := fmt.Sprintf("%s receive %d money from %s", this.accountId, amount, srcAccountId) | ||
GetPhoneProvider().Send(this.phoneNumber, msg) | ||
} |
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 PhoneProvider interface { | ||
Send(phoneNumber, msg string) | ||
} | ||
|
||
var providerInst PhoneProvider | ||
|
||
func SetPhoneProvider(provider PhoneProvider) { | ||
providerInst = provider | ||
} | ||
|
||
func GetPhoneProvider() PhoneProvider { | ||
return providerInst | ||
} |
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 ( | ||
"errors" | ||
"github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model" | ||
"sync" | ||
) | ||
|
||
type AccountService struct { | ||
repo model.AccountRepo | ||
} | ||
|
||
func (this *AccountService) Create(accountId string, amount uint, phoneNumber string) { | ||
account := model.AccountFactory{}.Create(accountId, amount, phoneNumber) | ||
this.repo.Add(account) | ||
} | ||
|
||
func (this *AccountService) TransferMoney(srcAccountId, dstAccountId string, amount uint) { | ||
from := this.repo.Get(srcAccountId) | ||
to := this.repo.Get(dstAccountId) | ||
from.MoneySource.TransferMoneyTo(to.MoneyDestination, amount) | ||
} | ||
|
||
func (this *AccountService) GetAmount(accountId string) (uint, error) { | ||
if account := this.repo.Get(accountId); account != nil { | ||
return account.MoneySource.GetAmount(), nil | ||
} | ||
return 0, errors.New("accountId is not existed") | ||
|
||
} | ||
|
||
func (this *AccountService) Destroy(accountId string) { | ||
this.repo.Remove(accountId) | ||
} | ||
|
||
var once sync.Once | ||
var serviceInst *AccountService = &AccountService{} | ||
|
||
func GetAccountService() *AccountService { | ||
once.Do(func() { | ||
serviceInst.repo = model.GetAccountRepo() | ||
}) | ||
return serviceInst | ||
} |
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 infra | ||
|
||
import "github.com/agiledragon/ddd-dci-sample-in-golang/transfer-money/domain/model" | ||
|
||
type AccountRepoImpl struct { | ||
} | ||
|
||
func (this *AccountRepoImpl) Add(account *model.Account) { | ||
|
||
} | ||
|
||
func (this *AccountRepoImpl) Get(accountId string) *model.Account { | ||
return nil | ||
} | ||
|
||
func (this *AccountRepoImpl) Remove(accountId string) { | ||
|
||
} |
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,8 @@ | ||
package infra | ||
|
||
type PhoneProviderImpl struct { | ||
} | ||
|
||
func (this PhoneProviderImpl) Send(phoneNumber, msg string) { | ||
|
||
} |
Oops, something went wrong.