Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Created February 22, 2018 10:18
Show Gist options
  • Select an option

  • Save gemmadlou/ab015ee80a31752cd93fea8426f19ccb to your computer and use it in GitHub Desktop.

Select an option

Save gemmadlou/ab015ee80a31752cd93fea8426f19ccb to your computer and use it in GitHub Desktop.
Tech Talk - Intro To Domain Driven Design
<?php
function read($whatever) {
if (is_string($whatever)) echo $whatever;
else print_r($whatever);
echo "\n";
};
// -------------------------------------
// Introduction to Domain Driven Design
// -------------------------------------
// The domain is an area of the business of which there is an expert on how it works
// -------------------------------------
// What does this example include?
// -------------------------------------
// No defensive programming principles
// No type hinting - as you don't have this in javascript (unless you use flow or typescript)
// Just domain modelling, no persistence, no factories, no aggregates or value objects
// Bad model structure, but simple principles
// -------------------------------------
// -------------------------------------
// A basic brief
// -------------------------------------
// Say you're making a publishing platform for journalists
// ...why recreate Wordpress?!!!!!!!!
// Your domain logic might be like this:
// 1. Without DDD
// -------------------------------------
// class Post
// {
// public $name;
// public $content;
// public function __construct($name, $content)
// {
// $this->name = $name;
// $this->content = $content;
// }
// }
// class Blog
// {
// public $name;
// public $posts = [];
// public function __construct($name)
// {
// $this->name = $name;
// }
// public function addPost($post)
// {
// $this->posts[] = $post;
// }
// }
// $blog = new Blog('Some Blogging/Journalists Platform');
// $post = new Post('Hello world', 'My first article');
// $blog->addPost($post);
// read($blog->name);
// read($blog->posts[0]->name);
// read($blog->posts[0]->content);
// -------------------------------------
// 2. Use the same language as the user or the domain expert
// In our case - this is a journalist
// -------------------------------------
// class Article
// {
// public $headline;
// public $copy;
// public function __construct($headline, $copy)
// {
// $this->headline = $headline;
// $this->copy = $copy;
// }
// }
// class News
// {
// public $articles = [];
// public function addArticle($article)
// {
// $this->articles[] = $article;
// }
// }
// $article = new Article('Jack Gets Dog!', 'copy copy copy');
// $news = new News;
// $news->addArticle($article);
// read($news->articles[0]->headline);
// read($news->articles[0]->copy);
// -------------------------------------
// 3. Modelling behaviour - not structure
// We'll change our News object
// -------------------------------------
// class Article
// {
// public $headline;
// public $copy;
// public function __construct($headline, $copy)
// {
// $this->headline = $headline;
// $this->copy = $copy;
// }
// public static function draft($headline, $copy)
// {
// return new self($headline, $copy);
// }
// }
// $article = Article::draft('Jack Gets Dog!', 'copy copy copy');
// read($article->headline);
// read($article->copy);
// -------------------------------------
// 4. Protect invariants = business rules that must be true
// Do this on behaviours only!
// -------------------------------------
// class Article
// {
// public $headline;
// public $copy;
// public function __construct($headline, $copy)
// {
// $this->headline = $headline;
// $this->copy = $copy;
// }
// public static function draft($headline, $copy)
// {
// if (empty($copy)) {
// throw new Exception('An article must have some copy, even if the headline has not been decided yet');
// }
// return new self($headline, $copy);
// }
// }
// $article = Article::draft('Jack Gets Dog!', '');
// -------------------------------------
// 5A. What happens if you put business rules in the constructor?
// having a new Something is not a behaviour, it is a result of something
// -------------------------------------
// class MacBook
// {
// private $hasPower = false;
// public static function buy()
// {
// return new self;
// }
// public function turnOn($hasPower = false)
// {
// if (!$hasPower) {
// throw new Exception('Macbook needs to charge first');
// }
// $this->hasPower = true;
// }
// }
// $computer = MacBook::buy();
// $computer->turnOn();
// -------------------------------------
// When do you use DDD?
// -------------------------------------
// * To maintain a common language between developers and clients
// * To manage domain rules
// * When there are behaviours
// -------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment