Search code examples
phpdomain-driven-designdto

PHP DDD can I return null from DTO getter?


Is that allowed or not? For example I have a method getName() that returns string, but what should I return if name is not set?


Solution

  • As far as I am aware there isn't any rule in DDD, preventing a DTO of returning Null value from a getter.

    Personally I handle the Null values sourcing in DTO in a level closer to the Client/View.

    So for example, if I build an MVC web app using DDD, in my interface I will have:

    • My DTO - with the getter
    • A DtoAssembler for my DTO which will use the getter.
    • A Controller which will call the DtoAssembler. Here I will make sure that the method which calls the DtoAssembler will throw an Exception in the case of a Null value from the getter.

    This way you ensure that the client will need to handle the Null value.