What is Domain-Driven Design
It’s about modeling the domain to reflect the intent of the business without the complexity of technical solutions.
DDD is not all about how to write or implement and use design patterns in your code, it’s more on solving domain problem in an explicit sense that all stakeholders can understand and discuss.
Example: Both of these code comes from a project that I am working on. They are the same project only the former is using PHP while the latter is TypeScript/JavaScript. (Ignore syntax difference)
The project exposes an API where clients can send an arbitrary code to be compiled in an isolated environment.
Looking between these two, which of them, do you think made more sense?
Version 1 in PHP
<?php
$courseRepo = new CourseRepository($this->getParameter('courses'));
$course = $courseRepo->find($repoName);
$sandbox = $this->get('sandbox_repository')->create($repoName, $data->code, $data->proof);
$runner = new SandboxRunner($sandbox, $this->verified_answer);
$runner->execute();
?>
Version 2 in TypeScript
const requestId = CoreDomain.Model.RequestId.create();
const environment = CoreDomain.Model.IsolatedEnvironment.create(requestId);
const compiler = new CoreDomain.Model.Compiler(environment);
const code = new CoreDomain.Model.Code(req.code, new CoreDomain.Model.Engine(req.engine));
const result = compiler.compile(code);
The latter uses DDD to clarify domain solution.
It nearly took me a day to figure out the right API interface/naming for the problem but it was worth it, as I have now a model that can be easily understood by all stakeholders.
I recommend that you invest this kind of attention and energy on important sub-domain(s) only, as modeling can be really tough and time-consuming to distilled.
In the end, this code can easily be discussed with or understand by other stakeholders of the project - your team, new programmers in the team, QA, technical writer, your boss, etc.
It’s much easier to discuss that, “The compiler needs to run in an isolated environment” than the, “The sandbox runner needs a sandbox.”
That’s all for now.