creating models2014/07/22  · creating models rob allen, july 2014 i make business websites...

55
Creating Models Rob Allen, July 2014

Upload: others

Post on 27-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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
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
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
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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