domain-driven-designdtodomain-model

How to share business logic between DTO & domain model?


I'm trying to refactor a codebase that was passing domain models to the views, to use DTOs instead.

Overall everything looks cleaner now, except one point: I have some business logic that belongs to the domain but needs to be executed in the view as well.

Example: I have an Activity class, that has a method to check whether a given User can edit it:

class Activity {
    public function isEditableBy(User $user): bool {
        ...
    }
}

Question: How to avoid duplicating this logic in the DTO and the model?


Solution

  • I thing you should not put logic in the DTO at all due to its definition "Data Transfer Object". So, "data" not "logic". Instead you can calculate result of the isEditableBy() method in your application service layer, put this result into a DTO and use the DTO in the view layer.