Golang DDD(领域驱动设计)的依赖模块实现

huangapple go评论91阅读模式
英文:

Golang DDD implementation of dependent modules

问题

我正在尝试将DDD架构应用到我的应用程序模块中,并在存储它们之间的相似(依赖)结构的问题上遇到了困难。

在第一个包中,我存储与实体People相关的所有内容:服务(存储库)、控制器、模型等等。

在第二个包中,我存储与另一个实体Apartments相关的所有内容:服务(存储库)、控制器、模型等等。

每个Apartment可以包含多个以与People相同结构存储在数据库中的Tenants

一些Apartment包的服务应该能够调用像获取/更新等Tenants的方法。我不想重新实现这些操作,而是希望从People包中的实现PeopleService中使用它们,但该服务返回的是People实体,而不是Tenant

我应该在Apartment包中创建另一个结构(复制整个源代码)People,将其称为Tenant,并将PeopleService的返回类型转换为它吗?

还是有其他方法可以做到这一点?

此外,我应该将具体实体的服务/存储库接口(具有CRUD类似方法的接口)存储在哪里?PeopleService接口应该同时存在于PeopleApartment包中吗?

谢谢。

英文:

I'm trying to implement DDD architecture to my application modules and get stuck with a problem of storing similar(depending) structures between them.

In the first package I store everything that is related to entity People: Services(Repositories), Controllers, Models, etc...

In the second package I store everything that is related to another entity Apartments: Services(Repositories), Controllers, Models, etc...

Each Apartment can contain multiple Tenants which are stored in database as the same structure as People.

Some Apartment package services should be able to call methods like get/update/... Tenants. I would gladly not re-implement these actions and use them from the implementations in the People package PeopleService, but that service returns People entity, not the Tenant.

Should I create additional structure (copy-paste whole source code) of People into the Apartment package, call it Tenant and convert return types of PeopleService to it?

Or is there another way to do it?

Also, where should I store interfaces of services/repositories(interfaces with CRUD-like methods) of concrete entity? Should interface PeopleService be both in People and Apartment packages?

Thanks.

答案1

得分: 6

可能有不同的方法,坦率地说,这个问题可能应该被关闭,因为它过于宽泛和基于个人观点。

我可能会这样做:

  1. 将所有相关的模型解耦,放在一个名为models的独立包中。

  2. 对于People->Tenants,使用组合关系,例如:

type Person { Name string }
type Tenant struct { Person }
  1. 关于People<->Apartments,创建一个名为tenants的包,该包将处理与people和apartments的模型和服务相关的操作。
英文:

There could be different approaches and frankly the question probably should be closed as too broad & opinion based.

I probably would do it this way:

  1. Decouple all linked models in a separate packages models.

  2. Use composition for People->Tenants, e.g.

type Person { Name string }
type Tenant struct { Person }
  1. Regards People<->Apartments - create a package tenants that will operate with models & services of people & apartments.

huangapple
  • 本文由 发表于 2016年9月15日 09:28:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/39501923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定