SlideShare a Scribd company logo
1 of 68
Hello!Hello!
Who am I?Who am I?
Yoan-Alexander GrigorovYoan-Alexander Grigorov
Software EngineerSoftware Engineer
Design Patterns adventurerDesign Patterns adventurer
GNU/Linux userGNU/Linux user
What is a Domain Model?What is a Domain Model?
Business Domain ModelBusiness Domain Model
<?php<?php
// ...// ...
$insurance$insurance->->addCoveredPerson(addCoveredPerson(
newnew CoveredPersonCoveredPerson(('Yoan-Alexander''Yoan-Alexander',, newnew AgeAge((2323))))
););
// ...// ...
// ...// ...
ifif (($ticket$ticket->->isExpired(isExpired(newnew DateTimeDateTime(('now''now'))) {))) {
// alert user that ticket has expired// alert user that ticket has expired
}}
Ubiquitous languageUbiquitous language
Application ModelApplication Model
// The connection URL// The connection URL
StringString urlurl == "https://ajax.googleapis.com/ajax/""https://ajax.googleapis.com/ajax/" ++
"services/search/web?v=1.0&q={query}""services/search/web?v=1.0&q={query}";;
// Create a new RestTemplate instance// Create a new RestTemplate instance
RestTemplateRestTemplate restTemplaterestTemplate == newnew RestTemplateRestTemplate();();
// Add the String message converter// Add the String message converter
restTemplaterestTemplate..getMessageConverters()getMessageConverters()..add(add(newnew
StringHttpMessageConverterStringHttpMessageConverter());());
// Make the HTTP GET request, marshaling the response to a String// Make the HTTP GET request, marshaling the response to a String
StringString resultresult == restTemplaterestTemplate..getForObject(url,getForObject(url, StringString..class,class,
"Android""Android"););
MVCMVC
MVC starts with the word “Model”MVC starts with the word “Model”
●
Model != ClassModel != Class
●
A model would express logic implementationA model would express logic implementation
●
A model is not always required to be CRUDA model is not always required to be CRUD
implementationimplementation
Widely used bad practicesWidely used bad practices
●
One controller – one “model”One controller – one “model”
●
Widely inheriting a common abstract class,Widely inheriting a common abstract class,
which contains helper methodswhich contains helper methods
●
Logic in view scriptsLogic in view scripts
●
Messy controllersMessy controllers
Why we should never keep anyWhy we should never keep any
logic in controllers?logic in controllers?
Compulsive hoardingCompulsive hoarding
Domain-Driven Design?Domain-Driven Design?
What Domain-Driven Design CAN'T do?What Domain-Driven Design CAN'T do?
●
Stop a huge asteroid heading to earthStop a huge asteroid heading to earth
●
Cure plagueCure plague
●
Can't beat node.js (because node.js is the bestCan't beat node.js (because node.js is the best
thing happened to the universe)thing happened to the universe)
●
Answer the question “Why are we here?!”Answer the question “Why are we here?!”
●
Bring dinosaurs back to lifeBring dinosaurs back to life
●
Prove the events from the Holy BibleProve the events from the Holy Bible
With what can Domain-Driven DesignWith what can Domain-Driven Design
help you?help you?
●
Escape from the code messEscape from the code mess
●
Provide loosely coupled classes to yourProvide loosely coupled classes to your
projectproject
●
Help you in decisions about data structuringHelp you in decisions about data structuring
●
Give you some tips about where to keep stateGive you some tips about where to keep state
and behaviorand behavior
Building BlocksBuilding Blocks
Value ObjectsValue Objects
Example:Example:
Saving IP address with a validationSaving IP address with a validation
<?php<?php
// ...// ...
public functionpublic function insertinsert((stringstring $ipAddress)$ipAddress)
{{
$ipAddressPattern$ipAddressPattern == '/'/^^(?:(?:2[0-4]...........-5]))?(?:(?:2[0-4]...........-5]))?$$/'/';;
ifif ((!!preg_matchpreg_match($ipAddressPattern, $ipAddress)) {($ipAddressPattern, $ipAddress)) {
throwthrow newnew WrongIpAddressExceptionWrongIpAddressException();();
}}
$this$this->->dbdb->->insert([insert([
'ipAddress''ipAddress' =>=> $ipAddress$ipAddress
]);]);
}}
// ...// ...
<?php<?php
// ...// ...
private functionprivate function isIpAddressValidisIpAddressValid((stringstring $ipAddress)$ipAddress)
{{
$ipAddressPattern$ipAddressPattern == '/'/^^(?:(?:2[0-4]...........-5]))?(?:(?:2[0-4]...........-5]))?$$/'/';;
ifif ((!!preg_matchpreg_match($ipAddressPattern, $ipAddress)) {($ipAddressPattern, $ipAddress)) {
returnreturn falsefalse;;
}}
returnreturn truetrue;;
}}
public functionpublic function insertinsert((stringstring $ipAddress)$ipAddress)
{{
ifif ((!!$this$this->->isIpAddressValid($ipAddress)) {isIpAddressValid($ipAddress)) {
throwthrow newnew WrongIpAddressExceptionWrongIpAddressException();();
}}
$this$this->->dbdb->->insert([insert([
'ipAddress''ipAddress' =>=> $ipAddress$ipAddress
]);]);
}}
// ...// ...
There, I fixed it!There, I fixed it!
Can we do it better?Can we do it better?
<?php<?php
classclass IpAddressIpAddress
{{
constconst stringstring VALIDATION_REGEXVALIDATION_REGEX == '/'/^^(?:(?:2[0-4]........-5]))?(?:(?:2[0-4]........-5]))?$$/'/';;
privateprivate stringstring $ipAddress;$ipAddress;
public functionpublic function __construct__construct((stringstring $ipAddress)$ipAddress)
{{
ifif ((!!preg_matchpreg_match((self::self::VALIDATION_REGEXVALIDATION_REGEX, $ipAddress)) {, $ipAddress)) {
throwthrow newnew WrongIpAddressExceptionWrongIpAddressException();();
}}
$this$this->->ipAddressipAddress == $ipAddress;$ipAddress;
}}
public functionpublic function toStringtoString()()
{{
returnreturn $this$this->->ipAddress;ipAddress;
}}
}}
<?php<?php
// ...// ...
public functionpublic function insertinsert((IpAddressIpAddress $ipAddress)$ipAddress)
{{
$this$this->->dbdb->->insert([insert([
'ipAddress''ipAddress' =>=> $ipAddress$ipAddress->->toString()toString()
]);]);
}}
// ...// ...
RememberRemember
●
Value ObjectsValue Objects shouldshould be immutablebe immutable
●
Two value objects with the same attributeTwo value objects with the same attribute
values are considered the samevalues are considered the same
Examples for embedded valueExamples for embedded value
objectsobjects
●
DateTime in PHPDateTime in PHP
●
java.lang.Integer in Java (and a lot more)java.lang.Integer in Java (and a lot more)
●
Number, String, Date in JavaScriptNumber, String, Date in JavaScript
EntitiesEntities
Object-Relational Mappers?Object-Relational Mappers?
What is an Entity?What is an Entity?
●
Objects, which could be distinguished byObjects, which could be distinguished by
some identifier (Primary Key?)some identifier (Primary Key?)
●
Entity objects are mutable (unlike valueEntity objects are mutable (unlike value
objects)objects)
●
Two entities with the same attribute values areTwo entities with the same attribute values are
not the same thing (unlike value objects)not the same thing (unlike value objects)
Yoan AlexanderYoan Alexander
!=!=
Example entitiesExample entities
RepositoriesRepositories
●
Repositories support entities lifecycleRepositories support entities lifecycle
●
From repositories you can fetch entitiesFrom repositories you can fetch entities
●
You can save entities to a data-source using aYou can save entities to a data-source using a
repository objectrepository object
Can we place a “seriousCan we place a “serious”” businessbusiness
logic inside entity classes?logic inside entity classes?
NO.NO.
●
Structure of entitiesStructure of entities
●
There is one main entity calledThere is one main entity called Aggregate RootAggregate Root
●
In the Orders aggregate, a User is part of an OrderIn the Orders aggregate, a User is part of an Order
●
In other scenario we can have another aggregate in which User isIn other scenario we can have another aggregate in which User is
the main entity (aggregate root)the main entity (aggregate root)
●
Each aggregate has it's corresponding repositoryEach aggregate has it's corresponding repository
AggregateAggregate
Repositories and aggregatesRepositories and aggregates
●
When we want to fetch something from theWhen we want to fetch something from the
OrdersRepository, we get the whole aggregateOrdersRepository, we get the whole aggregate
(Order + User)(Order + User)
What about the logic-leaders?What about the logic-leaders?
ServicesServices
Service Layer != Web ServicesService Layer != Web Services
Service classesService classes
●
They manage tasks from a higher (moreThey manage tasks from a higher (more
general) levelgeneral) level
●
They should do exactly what they name tellsThey should do exactly what they name tells
usus
●
Single Responsibility PrincipleSingle Responsibility Principle
●
They need to be SIMPLE!They need to be SIMPLE!
<?php<?php
//... Users registration controller ....//... Users registration controller ....
public functionpublic function signUpActionsignUpAction()()
{{
// ...// ...
$userCreator$userCreator == newnew UserCreatorUserCreator();();
$userCreator$userCreator->->registerNewUser(registerNewUser(
$email, $password, $fullName, $phone$email, $password, $fullName, $phone
););
// ...// ...
}}
<?php<?php
classclass UserCreatorUserCreator {{
privateprivate $usersRepository;$usersRepository;
privateprivate $emailNotifier;$emailNotifier;
public functionpublic function __construct__construct((
UsersRepositoryUsersRepository $usersRepository,$usersRepository,
NewAccountNotifierNewAccountNotifier $emailNotifier)$emailNotifier)
{{
$this$this->->usersRepositoryusersRepository == $usersRepository;$usersRepository;
$this$this->->emailNotifieremailNotifier == $emailNotifier;$emailNotifier;
}}
public functionpublic function registerNewUserregisterNewUser((
EmailEmail $email,$email, PasswordPassword $password,$password,
stringstring $fullName,$fullName, PhoneNumberPhoneNumber $phone$phone == nullnull))
{{
$user$user == newnew UserUser($email, $password, $fullName, $phone);($email, $password, $fullName, $phone);
$this$this->->usersRepositoryusersRepository->->insert($user);insert($user);
$this$this->->emailNotifieremailNotifier->->sendNotification($user);sendNotification($user);
}}
}}
<?php<?php
// ...// ...
public functionpublic function registerNewUserregisterNewUser((
EmailEmail $email,$email, PasswordPassword $password,$password,
stringstring $fullName,$fullName, PhoneNumberPhoneNumber $phone$phone == nullnull))
{{
$user$user == newnew UserUser($email, $password, $fullName, $phone);($email, $password, $fullName, $phone);
$this$this->->usersRepositoryusersRepository->->insert($user);insert($user);
$this$this->->eventManagereventManager->->trigger(trigger('NewAccountCreated''NewAccountCreated', $user);, $user);
}}
<?php<?php
// ... Confirguration// ... Confirguration
$eventManager$eventManager->->on(on('NewAccountCreated''NewAccountCreated',, functionfunction ((UserUser $user) {$user) {
$emailNotifier$emailNotifier == newnew NewAccountNotifierNewAccountNotifier();();
$emailNotifier$emailNotifier->->sendNotification($user);sendNotification($user);
});});
Some good advicesSome good advices
●
Do not put “Entity” in your entities class namesDo not put “Entity” in your entities class names
●
Avoid placing “Manager” or “Service” in theAvoid placing “Manager” or “Service” in the
class names of your service layer classesclass names of your service layer classes
●
One service class should not contain moreOne service class should not contain more
then 3 dependenciesthen 3 dependencies
We want more examples!We want more examples!
We have this desktop application which weWe have this desktop application which we
were using for our customers. We need to bringwere using for our customers. We need to bring
all of the existing user profiles from there to theall of the existing user profiles from there to the
web systemweb system
We are braking the singleWe are braking the single
responsibility principle... badly!responsibility principle... badly!
The class is named “UserCreator”The class is named “UserCreator”
…… notnot
UserCreatorAndImporterFromSomewhereElUserCreatorAndImporterFromSomewhereEl
sese
Another example?Another example?
We want our customers to be able toWe want our customers to be able to
use Facebook for their registrationsuse Facebook for their registrations
Grouping and packagingGrouping and packaging
●
NEVER group by pattern (e.g. packageNEVER group by pattern (e.g. package
Entities or Repositories)Entities or Repositories)
●
Group by meaningGroup by meaning
Get ready for endless refactoring!Get ready for endless refactoring!
Admit your mistakes before yourself!Admit your mistakes before yourself!
More on this topicMore on this topic
More on this topicMore on this topic
Thanks for listening!Thanks for listening!
Contacts:Contacts:
●
joan.grigorov@gmail.comjoan.grigorov@gmail.com
●
@YoanDev@YoanDev
●
http://linkedin.com/in/yoangrigorovhttp://linkedin.com/in/yoangrigorov

More Related Content

What's hot

The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)Ki Sung Bae
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 
The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable CodeBaidu, Inc.
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and SitesBuilding High Performance Web Applications and Sites
Building High Performance Web Applications and Sitesgoodfriday
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsRoss Tuck
 
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY SyntaxRubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY SyntaxDr Nic Williams
 
Realm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app databaseRealm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app databaseSergi Martínez
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в MagentoMagecom Ukraine
 
Typescript - why it's awesome
Typescript - why it's awesomeTypescript - why it's awesome
Typescript - why it's awesomePiotr Miazga
 
groovy databases
groovy databasesgroovy databases
groovy databasesPaul King
 
Intro To Moose
Intro To MooseIntro To Moose
Intro To MoosecPanel
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Fabien Potencier
 
OSDC.fr 2012 :: Cascalog : progammation logique pour Hadoop
OSDC.fr 2012 :: Cascalog : progammation logique pour HadoopOSDC.fr 2012 :: Cascalog : progammation logique pour Hadoop
OSDC.fr 2012 :: Cascalog : progammation logique pour HadoopPublicis Sapient Engineering
 
Working Effectively With Legacy Tdiary Code Using Cucumber And Rspec
Working Effectively With Legacy Tdiary Code Using Cucumber And RspecWorking Effectively With Legacy Tdiary Code Using Cucumber And Rspec
Working Effectively With Legacy Tdiary Code Using Cucumber And RspecShintaro Kakutani
 

What's hot (20)

The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable Code
 
Building High Performance Web Applications and Sites
Building High Performance Web Applications and SitesBuilding High Performance Web Applications and Sites
Building High Performance Web Applications and Sites
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
 
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY SyntaxRubyEnRails2007 - Dr Nic Williams - DIY Syntax
RubyEnRails2007 - Dr Nic Williams - DIY Syntax
 
Terrific Frontends
Terrific FrontendsTerrific Frontends
Terrific Frontends
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Realm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app databaseRealm or: How I learned to stop worrying and love my app database
Realm or: How I learned to stop worrying and love my app database
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
 
Typescript - why it's awesome
Typescript - why it's awesomeTypescript - why it's awesome
Typescript - why it's awesome
 
groovy databases
groovy databasesgroovy databases
groovy databases
 
Intro To Moose
Intro To MooseIntro To Moose
Intro To Moose
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
OSDC.fr 2012 :: Cascalog : progammation logique pour Hadoop
OSDC.fr 2012 :: Cascalog : progammation logique pour HadoopOSDC.fr 2012 :: Cascalog : progammation logique pour Hadoop
OSDC.fr 2012 :: Cascalog : progammation logique pour Hadoop
 
Hooks WCSD12
Hooks WCSD12Hooks WCSD12
Hooks WCSD12
 
Working Effectively With Legacy Tdiary Code Using Cucumber And Rspec
Working Effectively With Legacy Tdiary Code Using Cucumber And RspecWorking Effectively With Legacy Tdiary Code Using Cucumber And Rspec
Working Effectively With Legacy Tdiary Code Using Cucumber And Rspec
 

Viewers also liked

Implementing DDD Concepts in PHP
Implementing DDD Concepts in PHPImplementing DDD Concepts in PHP
Implementing DDD Concepts in PHPSteve Rhoades
 
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!Kacper Gunia
 
DDD - модель вместо требований
DDD - модель вместо требованийDDD - модель вместо требований
DDD - модель вместо требованийSQALab
 
DDD Modeling Workshop
DDD Modeling WorkshopDDD Modeling Workshop
DDD Modeling WorkshopDennis Traub
 
Domain Driven Design (DDD)
Domain Driven Design (DDD)Domain Driven Design (DDD)
Domain Driven Design (DDD)Tom Kocjan
 
A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slidesthinkddd
 
Domain Driven Design using Laravel
Domain Driven Design using LaravelDomain Driven Design using Laravel
Domain Driven Design using Laravelwajrcs
 

Viewers also liked (11)

DDD Workshop
DDD WorkshopDDD Workshop
DDD Workshop
 
Implementing DDD Concepts in PHP
Implementing DDD Concepts in PHPImplementing DDD Concepts in PHP
Implementing DDD Concepts in PHP
 
A brief look inside UML
A brief look inside UMLA brief look inside UML
A brief look inside UML
 
SOLID design
SOLID designSOLID design
SOLID design
 
WebSockets with PHP: Mission impossible
WebSockets with PHP: Mission impossibleWebSockets with PHP: Mission impossible
WebSockets with PHP: Mission impossible
 
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
 
DDD - модель вместо требований
DDD - модель вместо требованийDDD - модель вместо требований
DDD - модель вместо требований
 
DDD Modeling Workshop
DDD Modeling WorkshopDDD Modeling Workshop
DDD Modeling Workshop
 
Domain Driven Design (DDD)
Domain Driven Design (DDD)Domain Driven Design (DDD)
Domain Driven Design (DDD)
 
A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slides
 
Domain Driven Design using Laravel
Domain Driven Design using LaravelDomain Driven Design using Laravel
Domain Driven Design using Laravel
 

Similar to Introduction to Domain-Driven Design

Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer Simsek
 
EP2016 - Moving Away From Nodejs To A Pure Python Solution For Assets
EP2016 - Moving Away From Nodejs To A Pure Python Solution For AssetsEP2016 - Moving Away From Nodejs To A Pure Python Solution For Assets
EP2016 - Moving Away From Nodejs To A Pure Python Solution For AssetsAlessandro Molina
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyDavid Padbury
 
Questioning the status quo
Questioning the status quoQuestioning the status quo
Questioning the status quoIvano Pagano
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)Oleg Zinchenko
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
JavaScript, React Native and Performance at react-europe 2016
JavaScript, React Native and Performance at react-europe 2016JavaScript, React Native and Performance at react-europe 2016
JavaScript, React Native and Performance at react-europe 2016Tadeu Zagallo
 
DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)Oleg Zinchenko
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)James Titcumb
 
So S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better CodeSo S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better CodeNeil Crookes
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)James Titcumb
 
From Android NDK To AOSP
From Android NDK To AOSPFrom Android NDK To AOSP
From Android NDK To AOSPMin-Yih Hsu
 
Bubbles & Trees with jQuery
Bubbles & Trees with jQueryBubbles & Trees with jQuery
Bubbles & Trees with jQueryBastian Feder
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)Jose Manuel Pereira Garcia
 

Similar to Introduction to Domain-Driven Design (20)

Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09
 
EP2016 - Moving Away From Nodejs To A Pure Python Solution For Assets
EP2016 - Moving Away From Nodejs To A Pure Python Solution For AssetsEP2016 - Moving Away From Nodejs To A Pure Python Solution For Assets
EP2016 - Moving Away From Nodejs To A Pure Python Solution For Assets
 
Development Principles & Philosophy
Development Principles & PhilosophyDevelopment Principles & Philosophy
Development Principles & Philosophy
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight Guy
 
Make it SOLID!
Make it SOLID!Make it SOLID!
Make it SOLID!
 
Questioning the status quo
Questioning the status quoQuestioning the status quo
Questioning the status quo
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
JavaScript, React Native and Performance at react-europe 2016
JavaScript, React Native and Performance at react-europe 2016JavaScript, React Native and Performance at react-europe 2016
JavaScript, React Native and Performance at react-europe 2016
 
DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
So S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better CodeSo S.O.L.I.D Fu - Designing Better Code
So S.O.L.I.D Fu - Designing Better Code
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
From Android NDK To AOSP
From Android NDK To AOSPFrom Android NDK To AOSP
From Android NDK To AOSP
 
Bubbles & Trees with jQuery
Bubbles & Trees with jQueryBubbles & Trees with jQuery
Bubbles & Trees with jQuery
 
Fact, Fiction, and FP
Fact, Fiction, and FPFact, Fiction, and FP
Fact, Fiction, and FP
 
Gwt.create
Gwt.createGwt.create
Gwt.create
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 

Recently uploaded

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Recently uploaded (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Introduction to Domain-Driven Design

  • 2. Who am I?Who am I? Yoan-Alexander GrigorovYoan-Alexander Grigorov Software EngineerSoftware Engineer Design Patterns adventurerDesign Patterns adventurer GNU/Linux userGNU/Linux user
  • 3. What is a Domain Model?What is a Domain Model?
  • 4.
  • 5. Business Domain ModelBusiness Domain Model <?php<?php // ...// ... $insurance$insurance->->addCoveredPerson(addCoveredPerson( newnew CoveredPersonCoveredPerson(('Yoan-Alexander''Yoan-Alexander',, newnew AgeAge((2323)))) );); // ...// ... // ...// ... ifif (($ticket$ticket->->isExpired(isExpired(newnew DateTimeDateTime(('now''now'))) {))) { // alert user that ticket has expired// alert user that ticket has expired }}
  • 7. Application ModelApplication Model // The connection URL// The connection URL StringString urlurl == "https://ajax.googleapis.com/ajax/""https://ajax.googleapis.com/ajax/" ++ "services/search/web?v=1.0&q={query}""services/search/web?v=1.0&q={query}";; // Create a new RestTemplate instance// Create a new RestTemplate instance RestTemplateRestTemplate restTemplaterestTemplate == newnew RestTemplateRestTemplate();(); // Add the String message converter// Add the String message converter restTemplaterestTemplate..getMessageConverters()getMessageConverters()..add(add(newnew StringHttpMessageConverterStringHttpMessageConverter());()); // Make the HTTP GET request, marshaling the response to a String// Make the HTTP GET request, marshaling the response to a String StringString resultresult == restTemplaterestTemplate..getForObject(url,getForObject(url, StringString..class,class, "Android""Android"););
  • 8.
  • 10.
  • 11. MVC starts with the word “Model”MVC starts with the word “Model” ● Model != ClassModel != Class ● A model would express logic implementationA model would express logic implementation ● A model is not always required to be CRUDA model is not always required to be CRUD implementationimplementation
  • 12. Widely used bad practicesWidely used bad practices ● One controller – one “model”One controller – one “model” ● Widely inheriting a common abstract class,Widely inheriting a common abstract class, which contains helper methodswhich contains helper methods ● Logic in view scriptsLogic in view scripts ● Messy controllersMessy controllers
  • 13. Why we should never keep anyWhy we should never keep any logic in controllers?logic in controllers?
  • 14.
  • 17. What Domain-Driven Design CAN'T do?What Domain-Driven Design CAN'T do? ● Stop a huge asteroid heading to earthStop a huge asteroid heading to earth ● Cure plagueCure plague ● Can't beat node.js (because node.js is the bestCan't beat node.js (because node.js is the best thing happened to the universe)thing happened to the universe) ● Answer the question “Why are we here?!”Answer the question “Why are we here?!” ● Bring dinosaurs back to lifeBring dinosaurs back to life ● Prove the events from the Holy BibleProve the events from the Holy Bible
  • 18. With what can Domain-Driven DesignWith what can Domain-Driven Design help you?help you? ● Escape from the code messEscape from the code mess ● Provide loosely coupled classes to yourProvide loosely coupled classes to your projectproject ● Help you in decisions about data structuringHelp you in decisions about data structuring ● Give you some tips about where to keep stateGive you some tips about where to keep state and behaviorand behavior
  • 21. Example:Example: Saving IP address with a validationSaving IP address with a validation
  • 22. <?php<?php // ...// ... public functionpublic function insertinsert((stringstring $ipAddress)$ipAddress) {{ $ipAddressPattern$ipAddressPattern == '/'/^^(?:(?:2[0-4]...........-5]))?(?:(?:2[0-4]...........-5]))?$$/'/';; ifif ((!!preg_matchpreg_match($ipAddressPattern, $ipAddress)) {($ipAddressPattern, $ipAddress)) { throwthrow newnew WrongIpAddressExceptionWrongIpAddressException();(); }} $this$this->->dbdb->->insert([insert([ 'ipAddress''ipAddress' =>=> $ipAddress$ipAddress ]);]); }} // ...// ...
  • 23. <?php<?php // ...// ... private functionprivate function isIpAddressValidisIpAddressValid((stringstring $ipAddress)$ipAddress) {{ $ipAddressPattern$ipAddressPattern == '/'/^^(?:(?:2[0-4]...........-5]))?(?:(?:2[0-4]...........-5]))?$$/'/';; ifif ((!!preg_matchpreg_match($ipAddressPattern, $ipAddress)) {($ipAddressPattern, $ipAddress)) { returnreturn falsefalse;; }} returnreturn truetrue;; }} public functionpublic function insertinsert((stringstring $ipAddress)$ipAddress) {{ ifif ((!!$this$this->->isIpAddressValid($ipAddress)) {isIpAddressValid($ipAddress)) { throwthrow newnew WrongIpAddressExceptionWrongIpAddressException();(); }} $this$this->->dbdb->->insert([insert([ 'ipAddress''ipAddress' =>=> $ipAddress$ipAddress ]);]); }} // ...// ...
  • 24. There, I fixed it!There, I fixed it!
  • 25. Can we do it better?Can we do it better?
  • 26. <?php<?php classclass IpAddressIpAddress {{ constconst stringstring VALIDATION_REGEXVALIDATION_REGEX == '/'/^^(?:(?:2[0-4]........-5]))?(?:(?:2[0-4]........-5]))?$$/'/';; privateprivate stringstring $ipAddress;$ipAddress; public functionpublic function __construct__construct((stringstring $ipAddress)$ipAddress) {{ ifif ((!!preg_matchpreg_match((self::self::VALIDATION_REGEXVALIDATION_REGEX, $ipAddress)) {, $ipAddress)) { throwthrow newnew WrongIpAddressExceptionWrongIpAddressException();(); }} $this$this->->ipAddressipAddress == $ipAddress;$ipAddress; }} public functionpublic function toStringtoString()() {{ returnreturn $this$this->->ipAddress;ipAddress; }} }}
  • 27. <?php<?php // ...// ... public functionpublic function insertinsert((IpAddressIpAddress $ipAddress)$ipAddress) {{ $this$this->->dbdb->->insert([insert([ 'ipAddress''ipAddress' =>=> $ipAddress$ipAddress->->toString()toString() ]);]); }} // ...// ...
  • 28.
  • 29. RememberRemember ● Value ObjectsValue Objects shouldshould be immutablebe immutable ● Two value objects with the same attributeTwo value objects with the same attribute values are considered the samevalues are considered the same
  • 30. Examples for embedded valueExamples for embedded value objectsobjects ● DateTime in PHPDateTime in PHP ● java.lang.Integer in Java (and a lot more)java.lang.Integer in Java (and a lot more) ● Number, String, Date in JavaScriptNumber, String, Date in JavaScript
  • 33. What is an Entity?What is an Entity? ● Objects, which could be distinguished byObjects, which could be distinguished by some identifier (Primary Key?)some identifier (Primary Key?) ● Entity objects are mutable (unlike valueEntity objects are mutable (unlike value objects)objects) ● Two entities with the same attribute values areTwo entities with the same attribute values are not the same thing (unlike value objects)not the same thing (unlike value objects)
  • 36. RepositoriesRepositories ● Repositories support entities lifecycleRepositories support entities lifecycle ● From repositories you can fetch entitiesFrom repositories you can fetch entities ● You can save entities to a data-source using aYou can save entities to a data-source using a repository objectrepository object
  • 37. Can we place a “seriousCan we place a “serious”” businessbusiness logic inside entity classes?logic inside entity classes?
  • 39. ● Structure of entitiesStructure of entities ● There is one main entity calledThere is one main entity called Aggregate RootAggregate Root ● In the Orders aggregate, a User is part of an OrderIn the Orders aggregate, a User is part of an Order ● In other scenario we can have another aggregate in which User isIn other scenario we can have another aggregate in which User is the main entity (aggregate root)the main entity (aggregate root) ● Each aggregate has it's corresponding repositoryEach aggregate has it's corresponding repository AggregateAggregate
  • 40.
  • 41. Repositories and aggregatesRepositories and aggregates ● When we want to fetch something from theWhen we want to fetch something from the OrdersRepository, we get the whole aggregateOrdersRepository, we get the whole aggregate (Order + User)(Order + User)
  • 42. What about the logic-leaders?What about the logic-leaders?
  • 44. Service Layer != Web ServicesService Layer != Web Services
  • 45. Service classesService classes ● They manage tasks from a higher (moreThey manage tasks from a higher (more general) levelgeneral) level ● They should do exactly what they name tellsThey should do exactly what they name tells usus ● Single Responsibility PrincipleSingle Responsibility Principle ● They need to be SIMPLE!They need to be SIMPLE!
  • 46. <?php<?php //... Users registration controller ....//... Users registration controller .... public functionpublic function signUpActionsignUpAction()() {{ // ...// ... $userCreator$userCreator == newnew UserCreatorUserCreator();(); $userCreator$userCreator->->registerNewUser(registerNewUser( $email, $password, $fullName, $phone$email, $password, $fullName, $phone );); // ...// ... }}
  • 47. <?php<?php classclass UserCreatorUserCreator {{ privateprivate $usersRepository;$usersRepository; privateprivate $emailNotifier;$emailNotifier; public functionpublic function __construct__construct(( UsersRepositoryUsersRepository $usersRepository,$usersRepository, NewAccountNotifierNewAccountNotifier $emailNotifier)$emailNotifier) {{ $this$this->->usersRepositoryusersRepository == $usersRepository;$usersRepository; $this$this->->emailNotifieremailNotifier == $emailNotifier;$emailNotifier; }} public functionpublic function registerNewUserregisterNewUser(( EmailEmail $email,$email, PasswordPassword $password,$password, stringstring $fullName,$fullName, PhoneNumberPhoneNumber $phone$phone == nullnull)) {{ $user$user == newnew UserUser($email, $password, $fullName, $phone);($email, $password, $fullName, $phone); $this$this->->usersRepositoryusersRepository->->insert($user);insert($user); $this$this->->emailNotifieremailNotifier->->sendNotification($user);sendNotification($user); }} }}
  • 48. <?php<?php // ...// ... public functionpublic function registerNewUserregisterNewUser(( EmailEmail $email,$email, PasswordPassword $password,$password, stringstring $fullName,$fullName, PhoneNumberPhoneNumber $phone$phone == nullnull)) {{ $user$user == newnew UserUser($email, $password, $fullName, $phone);($email, $password, $fullName, $phone); $this$this->->usersRepositoryusersRepository->->insert($user);insert($user); $this$this->->eventManagereventManager->->trigger(trigger('NewAccountCreated''NewAccountCreated', $user);, $user); }}
  • 49. <?php<?php // ... Confirguration// ... Confirguration $eventManager$eventManager->->on(on('NewAccountCreated''NewAccountCreated',, functionfunction ((UserUser $user) {$user) { $emailNotifier$emailNotifier == newnew NewAccountNotifierNewAccountNotifier();(); $emailNotifier$emailNotifier->->sendNotification($user);sendNotification($user); });});
  • 50. Some good advicesSome good advices ● Do not put “Entity” in your entities class namesDo not put “Entity” in your entities class names ● Avoid placing “Manager” or “Service” in theAvoid placing “Manager” or “Service” in the class names of your service layer classesclass names of your service layer classes ● One service class should not contain moreOne service class should not contain more then 3 dependenciesthen 3 dependencies
  • 51. We want more examples!We want more examples!
  • 52. We have this desktop application which weWe have this desktop application which we were using for our customers. We need to bringwere using for our customers. We need to bring all of the existing user profiles from there to theall of the existing user profiles from there to the web systemweb system
  • 53.
  • 54.
  • 55.
  • 56. We are braking the singleWe are braking the single responsibility principle... badly!responsibility principle... badly!
  • 57. The class is named “UserCreator”The class is named “UserCreator” …… notnot UserCreatorAndImporterFromSomewhereElUserCreatorAndImporterFromSomewhereEl sese
  • 58.
  • 60. We want our customers to be able toWe want our customers to be able to use Facebook for their registrationsuse Facebook for their registrations
  • 61.
  • 62.
  • 63. Grouping and packagingGrouping and packaging ● NEVER group by pattern (e.g. packageNEVER group by pattern (e.g. package Entities or Repositories)Entities or Repositories) ● Group by meaningGroup by meaning
  • 64. Get ready for endless refactoring!Get ready for endless refactoring!
  • 65. Admit your mistakes before yourself!Admit your mistakes before yourself!
  • 66. More on this topicMore on this topic
  • 67. More on this topicMore on this topic
  • 68. Thanks for listening!Thanks for listening! Contacts:Contacts: ● joan.grigorov@gmail.comjoan.grigorov@gmail.com ● @YoanDev@YoanDev ● http://linkedin.com/in/yoangrigorovhttp://linkedin.com/in/yoangrigorov