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.
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\OptionPro: I can use both
Con:CustomerandProductin the same namespaceCustomerhas to reference its ownAddressasCustomer\AddressGroup all aggregate classes in the same namespace, including the aggregate root:
Customer\Customer Customer\Address Product\Product Product\OptionPro:
Con: from my root domain namespace, I have to reference:Customercan reference its address asAddressCustomerasCustomer\CustomerProductasProduct\Product
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
加载中,请稍侯......
精彩评论