DDD:领域模型命名空间约定
我正在用 PHP 编写一个带有域模型的应用程序,并且想知道应该采用哪种命名约定。
假设我有一个客户
,在其聚合根中有一个地址
。
我还有一个Product
,在其聚合根中有一个Option
。
我有两种选择:
将聚合根保留在域模型的根处:
<前><代码>客户 客户\地址 产品 产品\选项专业版:我可以在同一命名空间中使用
Customer
和Product
缺点:客户
必须将自己的地址
引用为客户\地址
对所有聚合进行分组同一命名空间中的类,包括聚合根:
客户\客户 客户\地址 产品\产品 产品\选项
专业版:
客户
可以将其地址引用为地址
缺点:从我的根域命名空间,我必须引用:客户
为客户\客户
产品
作为产品\产品
I'm writing an application with a domain model in PHP, and am wondering which naming convention I should adopt.
Let's say I have a Customer
, having an Address
within its aggregate root.
I also have a Product
, having an Option
within its aggregate root.
I have two alternatives:
Keep aggregate roots at the root of the domain model:
Customer Customer\Address Product Product\Option
Pro: I can use both
Customer
andProduct
in the same namespace
Con:Customer
has to reference its ownAddress
asCustomer\Address
Group all aggregate classes in the same namespace, including the aggregate root:
Customer\Customer Customer\Address Product\Product Product\Option
Pro:
Customer
can reference its address asAddress
Con: from my root domain namespace, I have to reference:Customer
asCustomer\Customer
Product
asProduct\Product
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不久前写了一个小框架,我选择使用您提出的第一个解决方案。
为什么?
事实上,我问了自己和你今天问的同样的问题,在与我的团队成员进行了一些讨论之后,我们一致认为,在命名空间中不重复类名感觉更合乎逻辑。
让我们看看如何使用解决方案 n°2 实例化您的类
您必须编写:
您可以看到重复吗?我感觉不太对劲。
在我看来,这就像
在方法名称中写“为什么重复客户”?我们知道这是客户的 ID,因为我们使用的是 Customer 对象。
该模型的另一个“坏事”是无法使用保留关键字作为类名。
例如,使用 pear 约定,您可以拥有
位于 Customer/Abstract.php 的类,这对我来说没问题,但如果您尝试使用名称空间对其进行翻译,则会
导致致命错误。因此,您必须再次重复类名中的域:
现在让我们看看如何使用解决方案 n°1 实例化您的类
您将编写:
我们不必再重复 Customer 两次来实例化客户类。然而,地址与客户相关仍然很明显。
这就是我选择使用这个模型的原因。
编辑: Zend Framework 2 也使用此约定
I wrote a little framework a while ago and i choose to use the first solution you propose.
Why?
Actually I asked myself the same question you're asking today and after a bit of discussion with my team mates we agreed that it felt more logical not to repeat the class name in the namespace.
Let's see how to instanciate your classes with solution n°2
You will have to write :
You can see the repetition right? It kind of doesn't feel right to me.
In my opinion it's like writing
Why repeat Customer in the method name? We know it's the customer's id since we're using a Customer object.
Another "bad thing" with this model is the impossibility to use reserved keyword as class name.
For exemple, using the pear convention you could have had the class
Located at Customer/Abstract.php which is ok to me but if you try to translate it using namespace you will have
which results in a fatal error. So again you will have to repeat the domain in the class name :
Now let's see how to instanciate your classes with solution n°1
You will write :
We don't have to repeat Customer twice anymore to instanciate the Customer class. However it is still clear that Address is related to Customer.
That's why I choose to use this model.
EDIT : Zend Framework 2 use this convention too