domain-driven-designcqrsrequesthandler

DDD / CQRS - Does a request handler (or controller) can throw exceptions defined at the domain level?


Good morning,

Let's say, we've a domain defining an exception such as ObjectNotFoundException which expect an identifier (VO), defined at the domain model.

Question

Can we throw domain exceptions from the request handlers directly, for instance:

    class ObjectRequestHandler implements RequestHandler
    {
        ...

        public function __invoke(Request $request, Response $response)
        {
            // Will self-validate and throw an exception if not a valid UUID
            $objectId = ObjectId::fromString(strval($request->param('object_id'])));

            $object = $this->repository->find((string)$objectId);

            if (NULL === $object) {
                // Exception defined at the domain level...
                throw new ObjectNotFoundException($objectId);
            }

            ...
        }
}

Doing this also lead to usage of the identifier VO in the request handler... It MUST be also noted that the throwed exception will be catched by the default exception handler which in turn, will prepare and send a JSON response.

Finally, note that the request handler here, is an implementation detail, not part of the question. Please don't comment about it.

Thank you.


Solution

  • Your example shows the correct usage of the repository to fetch an object from the data store based on an identifier.

    Let's unpack and expand the workflow a little more to fit the paradigms of DDD, to help answer the question:

    So,

    Ignoring Application Services for a moment, you are spot on in your usage of the concept. The code example is correct in structure. It's just the terms that need to be clarified.