dongying9756 2017-02-28 14:06
浏览 72

在GO中设计DDD Web应用

I'm building a go web service and trying to follow the "clean code" or "domain driven design" principles. My stripped down folder structure looks something like this:

- api
    - postgre
        employee_service.go
    employee.go

The basic layout is my root(api) folder is all of my domain code. And all the packages are adapters to make my domain work but are easy to swap out if needed. My employee.go file might looks something like this:

type EmployeeService interface{
    CreateEmployee(e *Employee) error
}

type Employee struct {
    ID string
    Name string
}

The question I have is where to place certain things on my app because they feel to me like they are part of my domain logic; however the domain logic should not have any external dependencies so it leaves me stuck. Here is an example I want to create a new employee:

postgre/employee_service.go:

type EmployeeService struct {
    DB *pg.DB
}

func (s *EmployeeService) CreateEmployee(e *api.Employee) error {
    e.ID = uuid.NewV4().String()

    // insert to db
}

Now you can imagine the Employee struct has a lot more fields than just an ID or name I need to set before inserting it to the DB. So my thought was to add a method to the User struct in the root of my app something like this:

/employee.go    

func (e *Employee) New() {
    e.ID = uuid.NewV4()
    // add any other fields I need
}

And the postgre user service would look like this:

postgre/employee_service.go:

func (s *EmployeeService) CreateEmployee(e *api.Employee) error {
    e.New()

    // insert to db
}

Now the issue with this is that the domain of my app is dependent on the uuid package. In my mind that is fine because if I were to swap out postgre for mysql or mongo or any other DB that New() method would not change. Another more "complicated" example is password hashing no matter what the DB is my password hashes will be generated the same so I believe this belongs in my root folder where my domain logic is. Yet again to hash a password I would the crypto/bcrypt dependency breaking the DDD rule that your domain should have no external dependencies.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?