DDD: Domain model namespaces convention DDD: Domain model namespaces convention php php

DDD: Domain model namespaces convention


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\CustomerCustomer\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

CustomerCustomer\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