ipc07 talk - beautiful code with aop and di

44
Beautiful Code with AOP and DI International PHP Conference – Frankfurt, Germany

Upload: robert-lemke

Post on 19-May-2015

1.324 views

Category:

Documents


0 download

DESCRIPTION

This presentation at the International PHP Conference 2007 in Frankfurt, Germany outlines the concepts of Domain Driven Design, supported by techniques such as Aspect Oriented Programming and Dependency Injection. A practical example was shown, using a recent snapshot of the upcoming FLOW3 Framework.

TRANSCRIPT

Page 1: IPC07 Talk - Beautiful Code with AOP and DI

Beautiful Code with AOP and DIInternational PHP Conference – Frankfurt, Germany

Page 2: IPC07 Talk - Beautiful Code with AOP and DI

CAUTION!HOT CODE

Page 3: IPC07 Talk - Beautiful Code with AOP and DI

Overview

Background Information

Domain Driven Design

Components and Dependecy Injection

Aspect Oriented Programming

TYPO3 Framework

Page 4: IPC07 Talk - Beautiful Code with AOP and DI

Background Information

Mission "Ease of Use"

"Ease of Use" has been identified as the main topic for next TYPO3 versions

First steps were founding the TYPO3 Association, building a brand and "zapping the Gremlins"

Decision to rewrite TYPO3 based on a new architecture to achieve Ease of Use for developers

Currently TYPO3 5.0 is being developed - the 4.x branch will still be improved and see new features

Page 5: IPC07 Talk - Beautiful Code with AOP and DI

Background Information

Core + Extensions = TYPO3

Page 6: IPC07 Talk - Beautiful Code with AOP and DI

Background Information

Three Sub-Projects

TYPO3 CMS TYPO3 Framework TYPO3 CR

Page 7: IPC07 Talk - Beautiful Code with AOP and DI

Background Information

Empty Canvas

?

Page 8: IPC07 Talk - Beautiful Code with AOP and DI

Background Information

(Some) Guiding Principles

anticipate real world needs

focus on the domain

centralize concerns

convention over configuration

human readable code

modularity everywhere

design decisions can be changed

Page 9: IPC07 Talk - Beautiful Code with AOP and DI

Background Information

5 Development Methods

Work where it matters (DDD)

Work where it belongs to (DRY)

No chaos, ever (TDD)

No bad surprises (CI)

Innovation built-in (DI, AOP, GUI)

... and more XP techniques

Domain Driven Design

Don't Repeat Yourself

Test Driven Development

ContinuousIntegration Dependency Injection

Aspect Orient ProgrammingGraphical User Interface

Page 10: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

A domain is the activity or business of the user

Domain Driven Design is about

focussing on the domain and domain logic

accurately mapping the domain concepts to software

forming a ubiquitous language among the project members

Page 11: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

Why focus on the Domain?

Most time in development is spent on infrastructure instead of user interfaces and business logic.

We don't want that.

Page 12: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

Ubiquitous language

The common vocabulary is an important prerequisitefor successful collaboration

Use the same words for discussion, modeling, developmentand documentation

Page 13: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

Phone Book Domain Model

Page 14: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

More phone book actions

show phone book entries

check if user may delete phone book entry

export phone book entries

log phone book actions

Page 15: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

More phone book actions

show phone book entries

check if user may delete phone book entry

export phone book entries

log phone book actions✘ not in the domain of a phone book

Page 16: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

Layered Architecture

Presentation

Domain

Data source

Application Logic (Service Layer)

Domain Model (Domain Layer)

View

Controller

Data Mapper (part of Content Repository)

Data Source Abstraction

Page 17: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

Layered Architecture

Presentation

Domain

Data source

Application Logic (Service Layer)

Domain Model (Domain Layer)

View

Controller

Data Mapper (part of Content Repository)

Data Source Abstraction

Page 18: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

Domain-Driven Design

$res = $DB->execQuery('SELECT cnumber FROM customers WHERE lastname LIKE "Sarkosh"'); if (!$res) throw new Exception; $c = mysql_fetch_assoc($res); if (!is_array($c)) throw new Exception; $res = $DB->execQuery('SELECT * FROM registrations WHERE cnumber =' . intval($c['cnumber'])); $reg = mysql_fetch_assoc($res); if (!is_array($reg)) { $this->log('Registration not found'); throw new Exception; } if (!$reg['status'] == 3) throw new Exception;

„Cancel the registration for Mr. Sarkosh“

Page 19: IPC07 Talk - Beautiful Code with AOP and DI

Domain Driven Design

Domain-Driven Design

„Cancel the registration for Mr. Sarkosh“

$customer = $this->customerRepository->findByLastname('Sarkosh'); $event = $this->eventRepository->getEvent('IPC07');$event->addParticipant($customer);$event->removeParticipant($customer);

$customer = $this->customerRepository->findByLastname('Sarkosh'); $event = $this->eventRepository->getEvent('IPC07'); $registrationService->cancelRegistration($event, $customer);

Page 20: IPC07 Talk - Beautiful Code with AOP and DI

Components

Page 21: IPC07 Talk - Beautiful Code with AOP and DI

Components

Playing with building blocks

In TYPO3 5.0, objects considered as components

The combination of components used is configurable(orchestration)

The less components know about each other the easier it is to reuse them in a variety of contexts

Create your own LEGO set by creating cleanly separated, decoupled components!

Page 22: IPC07 Talk - Beautiful Code with AOP and DI

Components

Component Dependencies

Components seldomly come alone

Components depend on other components which depend on other components which ...

Inspiring people toshare

Page 23: IPC07 Talk - Beautiful Code with AOP and DI

Components

Component Dependencies

Components seldomly come alone

Components depend on other components which depend on other components which ...

Problem:

Components explicitly refer to other components:$event = new Event();

Inspiring people toshare

Page 24: IPC07 Talk - Beautiful Code with AOP and DI

Components

Component Dependencies

Possible solutions:

Factory pattern (GoF):$event = EventFactory::createEvent();

Registry pattern (Fowler) / Service Locator:class ServiceLocator { public function registerComponent($name, $component) {} public function getInstance($name);}

Inspiring people toshare

Page 25: IPC07 Talk - Beautiful Code with AOP and DI

Components

Dependency Injection

A component doesn't ask for the instance of another component but gets it injected

This a type of "Inversion of Control" also referred to as the "Hollywood Principle": "Don't call us, we'll call you"

Enforces loose coupling and high cohesion

Makes you a better programmer

Page 26: IPC07 Talk - Beautiful Code with AOP and DI

Components

Practical examples for DI

Example using ...

Constructor Injection

Setter Injection

Autowiring Demo

Page 27: IPC07 Talk - Beautiful Code with AOP and DI

Aspect Oriented Programming

AOP is a programming paradigm

complements OOP by separating concerns to improve modularization

OOP modularizes concerns: methods, classes, packages

AOP addresses cross-cutting concerns

Page 28: IPC07 Talk - Beautiful Code with AOP and DI

Aspect Oriented Programming

Cross-cutting concerns

Presentation

Domain

Data source

Page 29: IPC07 Talk - Beautiful Code with AOP and DI

Aspect Oriented Programming

Cross-cutting concerns

Presentation

Domain

Data source

The concerns

live here

Page 30: IPC07 Talk - Beautiful Code with AOP and DI

Aspect Oriented Programming

Cross-cutting concerns

Event Domain Model

Security

Logging

CONCERNSX-ING

Page 31: IPC07 Talk - Beautiful Code with AOP and DI

Aspect Oriented Programming

Some Definitions

Advice

encapsulated code, to be re-used

Joinpoint

places in the code where advice can be applied

Pointcut

identifies set of joinpoints where advice should be applied

Aspect

groups advices and pointcuts

Page 32: IPC07 Talk - Beautiful Code with AOP and DI

Aspect Oriented Programming

Aspects in action

Demo

Page 33: IPC07 Talk - Beautiful Code with AOP and DI

Read before first use

Try it yourself

Page 34: IPC07 Talk - Beautiful Code with AOP and DI

Try it yourself

(Advertising)

The TYPO3 Framework is the only PHP-based solution which truly supports Domain Driven Design

powerful, transparent and plain PHP-based AOP support(no extension required, no compile step)

full-featured IoC container with Dependency Injection

great (and big) community, backed by the TYPO3 Association

....

Page 35: IPC07 Talk - Beautiful Code with AOP and DI

Try it yourself

Installation

You need PHP6

Current installation options:

svn checkout http://5-0.dev.typo3.org/svn/TYPO3/

download the TYPO3 installer

Page 36: IPC07 Talk - Beautiful Code with AOP and DI

Try it yourself

Installation

Page 37: IPC07 Talk - Beautiful Code with AOP and DI

When ...?

Page 38: IPC07 Talk - Beautiful Code with AOP and DI

Recommended literature

Page 39: IPC07 Talk - Beautiful Code with AOP and DI

Recommended literature

Eric Evans:

Domain Driven DesignTackling Complexity in the Heart of Software

Addison Wesley 2002

Page 40: IPC07 Talk - Beautiful Code with AOP and DI

Martin Fowler:Patterns of Enterprise Application Architecture

Addison Wesley 2002

Recommended literature

Page 41: IPC07 Talk - Beautiful Code with AOP and DI

Jimmy Nilsson:Applying Domain-DrivenDesign and Patterns

Addison Wesley 2006

Recommended literature

Page 42: IPC07 Talk - Beautiful Code with AOP and DI

Links

TYPO3 5.0 Subsitehttp://typo3.org/gimmefive

TYPO3 5.0 Development Websitehttp://5-0.dev.typo3.org

TYPO3 5.0 Documentationhttp://5-0.dev.typo3.org/guide

Page 43: IPC07 Talk - Beautiful Code with AOP and DI

So long and thanks for the fish

[email protected]

Page 44: IPC07 Talk - Beautiful Code with AOP and DI