DDD:领域模型命名空间约定

发布于 2024-11-28 18:09:04 字数 747 浏览 7 评论 0原文

我正在用 PHP 编写一个带有域模型的应用程序,并且想知道应该采用哪种命名约定。

假设我有一个客户,在其聚合根中有一个地址
我还有一个Product,在其聚合根中有一个Option

我有两种选择:

  1. 将聚合根保留在域模型的根处:

    <前><代码>客户 客户\地址 产品 产品\选项

    专业版:我可以在同一命名空间中使用 CustomerProduct
    缺点客户必须将自己的地址引用为客户\地址

  2. 对所有聚合进行分组同一命名空间中的类,包括聚合根:

    客户\客户
    客户\地址
    产品\产品
    产品\选项
    

    专业版客户可以将其地址引用为地址
    缺点:从我的根域命名空间,我必须引用:

    • 客户客户\客户
    • 产品作为产品\产品

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:

  1. Keep aggregate roots at the root of the domain model:

    Customer
    Customer\Address
    Product
    Product\Option
    

    Pro: I can use both Customer and Product in the same namespace

    Con: Customer has to reference its own Address as Customer\Address

  2. 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 as Address

    Con: from my root domain namespace, I have to reference:

    • Customer as Customer\Customer
    • Product as Product\Product

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-12-05 18:09:04

我不久前写了一个小框架,我选择使用您提出的第一个解决方案。

将聚合根保留在域模型的根处:

为什么?

事实上,我问了自己和你今天问的同样的问题,在与我的团队成员进行了一些讨论之后,我们一致认为,在命名空间中不重复类名感觉更合乎逻辑。


让我们看看如何使用解决方案 n°2 实例化您的类

Customer\Customer
Customer\Address

您必须编写:

$customer = new Customer\Customer();
$address = new Customer\Address();

您可以看到重复吗?我感觉不太对劲。
在我看来,这就像

$customer->getCustomerId();

在方法名称中写“为什么重复客户”?我们知道这是客户的 ID,因为我们使用的是 Customer 对象。

该模型的另一个“坏事”是无法使用保留关键字作为类名。

例如,使用 pear 约定,您可以拥有

Customer_Abstract

位于 Customer/Abstract.php 的类,这对我来说没问题,但如果您尝试使用名称空间对其进行翻译,则会

namespace Customer;

class Abstract {}

导致致命错误。因此,您必须再次重复类名中的域:

namespace Customer;

class AbstractCustomer {}

$customer = new Customer\AbstractCustomer();

现在让我们看看如何使用解决方案 n°1 实例化您的类

Customer
Customer\Address

您将编写:

$customer = new Customer();
$address = new Customer\Address();

我们不必再重复 Customer 两次来实例化客户类。然而,地址与客户相关仍然很明显。

这就是我选择使用这个模型的原因。

编辑: Zend Framework 2 也使用此约定

I wrote a little framework a while ago and i choose to use the first solution you propose.

Keep aggregate roots at the root of the domain model:

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

Customer\Customer
Customer\Address

You will have to write :

$customer = new Customer\Customer();
$address = new Customer\Address();

You can see the repetition right? It kind of doesn't feel right to me.
In my opinion it's like writing

$customer->getCustomerId();

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

Customer_Abstract

Located at Customer/Abstract.php which is ok to me but if you try to translate it using namespace you will have

namespace Customer;

class Abstract {}

which results in a fatal error. So again you will have to repeat the domain in the class name :

namespace Customer;

class AbstractCustomer {}

$customer = new Customer\AbstractCustomer();

Now let's see how to instanciate your classes with solution n°1

Customer
Customer\Address

You will write :

$customer = new Customer();
$address = new Customer\Address();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文