collectionsdomain-driven-designobject-model

Methods for retrieving object-collection in Domain Driven Design


I am using Zend Framework and also try to move towards DDD approach (Domain-Driven Design). I have models, mappers and DbTables for domain objects.

There are many situations when I need to fetch multiple entities of same time, for example -list of all users within the system-, so my user model will have a method 'getAllUsers' which will return all the users (right now its returning an array of all the users, but I am thinking of making a collection class). So far I am using a normal method (non-static) to fetch the collection, and for this purpose, I need to create an 'empty' object. The other option is to convert it into a static method.

I am not sure, which approach is better, keep such methods as non-static or convert them into static methods. And what is the better approach/practice and why? Also which approach closely follow the DDD methodology.

PS: Kindly let me know, if you can think of a better title. And NO its not a course question.


Solution

  • Static method means that there is no need for instantiated object to call it. Usually static methods are used to group methods that are related to whole class not just particular instance of class. In contrast - non static methods are used to group methods that are related to particular individual object.

    So, if you are marking getAllUsers() non-static and put it underneath user, basically you are asking for one particular user to know about every other user. Using analogy - that would be like asking full information about all citizens in country from one individual citizen (do you know them all in your country?).

    Marking it static would be like asking for information about all citizens from citizen definition in encyclopedia. It's better than marking it non-static, but still a bit strange and awkward.

    Normally countries have population registers that are responsible for dealing out information about citizens. Converting analogy back - you would have "something else", collection-like register that is responsible for this.