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
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"););
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?
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
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?
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!
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
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
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!