![Page 1: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/1.jpg)
Creating Models
Rob Allen, July 2014
![Page 2: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/2.jpg)
I make business websites19ft.com
![Page 3: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/3.jpg)
The business logic is thehard part
![Page 4: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/4.jpg)
MVC
![Page 5: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/5.jpg)
MVC
![Page 6: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/6.jpg)
The model is thesolution to a problem
![Page 7: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/7.jpg)
![Page 8: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/8.jpg)
![Page 9: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/9.jpg)
![Page 10: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/10.jpg)
A problemA customer wants to plan a journey between twostations.
![Page 11: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/11.jpg)
How do we model this?
![Page 12: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/12.jpg)
Identify the key objects• Customer• Journey• Station• Line
![Page 13: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/13.jpg)
E-R Diagram
![Page 14: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/14.jpg)
Entities represent things inthe problem-space
![Page 15: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/15.jpg)
Entity• Means something to the customer• An object defined primarily by its identity• Mutable• Persisted• Has a life cycle
![Page 16: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/16.jpg)
Identity• The identity of an object is what it means to the
customer• Unique within the scope of the domain
For a tube station, this is it’s name, not its database id.
My customer is never going to talk to me about station43, they are going to say “Euston Square”.
![Page 17: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/17.jpg)
Value objects• Defined primarily by its attributes• Immutable• Simple!• Do not exist without an entity
![Page 18: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/18.jpg)
A station has a location
![Page 19: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/19.jpg)
A station has a location
![Page 20: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/20.jpg)
Domain servicesIf a SERVICE were devised to make appropriatedebits and credits for a funds transfer, that capabilitywould belong in the domain layer.
Eric Evans
![Page 21: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/21.jpg)
Domain services• We map the business processes to services• Represents behaviour in the domain• A service does not have internal state• Usually a point of connection for many objects
![Page 22: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/22.jpg)
Let’s look at some code
![Page 23: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/23.jpg)
Some codeclass Journey {
function getStart() {}
function setStart(Station $start) {}
function getStop() {}
function setStop(Station $stop) {}
function setRoute() {}
function getRoute() {}
}
class RoutingService {
function route(Station $start, Station $stop) {}
}
![Page 24: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/24.jpg)
Anaemic domain modelWhen you look at the behavior, and you realize thatthere is hardly any behavior on these objects,making them little more than bags of getters andsetters.
Instead there are a set of service objects whichcapture all the domain logic.
Martin Fowler
![Page 25: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/25.jpg)
Entity with behaviourclass Journey {
function getStart() {}
function setStart(Station $start) {}
function getStop() {}
function setStop(Station $stop) {}
function route() {}
}
![Page 26: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/26.jpg)
What happens if route() iscomplex?
![Page 27: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/27.jpg)
Double dispatchThe entity calls the helper domain service, passing areference to itself.
// Helper service
class JourneyRouter {
function route(Journey $journey) {}
}
// Journey class
function route() {
$router = $new JourneyRouter();
$this->route = $router->route($this);
}
![Page 28: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/28.jpg)
Persistence
![Page 29: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/29.jpg)
Persistence options
A simple domain model can use ActiveRecord/TDG;a complex one will require mapping.
I don’t really care what you choose!
![Page 30: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/30.jpg)
I lied.
![Page 31: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/31.jpg)
Don’t use ActiveRecord!
![Page 32: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/32.jpg)
It integrates the database code intoyour domain model
![Page 33: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/33.jpg)
Table Data Gateway• Operates on a single database table• Contains all the SQL for accessing the table• Doesn’t know anything about the entity.• Simple to implement
![Page 34: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/34.jpg)
Table Data Gatewayclass JourneyGateway {
function __construct($dsn, $username, $password) {}
function find($id) {}
function findForStartingStation($stationId) {}
function insert($startId, $stopId) {}
function update($id, $startId, $stopId) {}
}
![Page 35: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/35.jpg)
Data Mapper• Class to transfer data from objects to the database
and back.• Entity aware• Isolates the domain model from the database• Not limited to a single table
![Page 36: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/36.jpg)
Data Mapperclass JourneyMapper {
function __construct($dsn, $username, $password) {}
function find($id) {}
function findForStartingStation($stationId) {}
public function save(Journey $journey) {}
}
![Page 37: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/37.jpg)
Increased scope: ORMPersistence layer is more complicated:
• Identity map to hold loaded objects• Storage of entire object graphs to the database• Unit of Work to track changed objects for saving
If you need this, then use a pre-written ORM library!
![Page 38: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/38.jpg)
Web services• Still within your persistence layer• Data mappers work really well
![Page 39: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/39.jpg)
Integrating our model intothe application
![Page 40: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/40.jpg)
The service layer*It does not contain business rules or knowledge, butonly coordinates tasks and delegates work tocollaborations of domain objects in the next layerdown.
Eric Evans
* (Known as application layer in DDD)
![Page 41: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/41.jpg)
Service layerWe can sub-divide:
• Application services• Infrastructural services
![Page 42: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/42.jpg)
Application servicesIf the banking application can convert and export ourtransactions into a spreadsheet file for us to analyze,this is an application SERVICE.
Eric Evans
![Page 43: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/43.jpg)
Infrastructural servicesA bank might have an application that sends out anemail to a customer when an account balance fallsbelow a specific threshold. The interface thatencapsulates the email system, and perhapsalternate means of notification, is a SERVICE in theinfrastructure layer.
Eric Evans
![Page 44: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/44.jpg)
Application serviceclass JourneyService {
function createJourney($customer, $start, $stop)
{
$journey = $customer->createJourney($start,$stop);
$journey->route();
$this->entityManager->flush();
$this->mailer->newJourneyNofication($journey);
$this->auditor->log('newJourney', $journey);
}
}
![Page 45: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/45.jpg)
Beware the Fat serviceDecouple using Observer pattern
![Page 46: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/46.jpg)
CQRS: Separating readingand writing
![Page 47: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/47.jpg)
CQRSCommand Query Responsibility Segregation.
• Commands change data• Queries read data
Most useful when:
• Separate hardware• Optimise performance
![Page 48: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/48.jpg)
CQRS at its most basicTwo services where there was one
![Page 49: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/49.jpg)
CQRSOne useful case is a summary object for a read-only list
![Page 50: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/50.jpg)
Final point
![Page 51: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/51.jpg)
The success of a design is not necessarily markedby its longevity. It is the nature of software tochange.
Eric Evans
![Page 52: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/52.jpg)
To sum upEntity:
Object with identity that do stuff
Value object:Immutable with no identity
Domain service:Behaviours that don’t belong to an entity
![Page 53: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/53.jpg)
To sum upMappers / Repository:
Transfer your model to and from persistence
Application services:Isolate your domain model from your controllers
Infrastructure services:Support services for your application
![Page 54: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/54.jpg)
Thank you!Feedback: https://bit.ly/creatingmodels
Rob Allen - http://akrabat.com - @akrabat
![Page 55: Creating Models2014/07/22 · Creating Models Rob Allen, July 2014 I make business websites 19ft.com The business logic is the hard part MVC MVC The model is the solution to a problem](https://reader034.vdocuments.site/reader034/viewer/2022042307/5ed2d268b162b967ae07aedf/html5/thumbnails/55.jpg)
Resources- Domain Driven Design by Eric Evans- Patterns of Enterprise Application Architecture by Martin Fowler
- DDDQ: http://www.infoq.com/minibooks/domain-driven-design-quickly- Double dispatch: http://lostechies.com/jimmybogard/?p=394- CQRS: http://martinfowler.com/bliki/CQRS.html