Question

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.

Was it helpful?

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.

OTHER TIPS

Firstly, I think this is not DDD related question. To use or not static methods is more like OOP or designing question.

Basically using class static methods is not really OOP but procedural programming because you cannot use any of the OOP paradigms like encapsulation or inheritance. It points at possible design flaws/code smell.

It also makes the unit testing hard, because mocking of static methods is more complicated and AFAIK it has been added just to allow covering the legacy code by unit tests.

It would be easier though to answer your question if posted some code examples.

Similar questions were answered here, here or here.

In Domain Driven Design, a persistent collection of users should be represented as a UserRepository interface. The concrete implementation of that interface has a state, including for example the state of the database connection. So it follows that you need an instance of the repository implementation, and its methods would be instance methods rather than static methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top