IPC07 Talk - Beautiful Code with AOP and DI
-
Upload
robert-lemke -
Category
Documents
-
view
1.321 -
download
0
description
Transcript of IPC07 Talk - Beautiful Code with AOP and DI
Beautiful Code with AOP and DIInternational PHP Conference – Frankfurt, Germany
CAUTION!HOT CODE
Overview
Background Information
Domain Driven Design
Components and Dependecy Injection
Aspect Oriented Programming
TYPO3 Framework
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
Background Information
Core + Extensions = TYPO3
Background Information
Three Sub-Projects
TYPO3 CMS TYPO3 Framework TYPO3 CR
Background Information
Empty Canvas
?
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
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
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
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.
Domain Driven Design
Ubiquitous language
The common vocabulary is an important prerequisitefor successful collaboration
Use the same words for discussion, modeling, developmentand documentation
Domain Driven Design
Phone Book Domain Model
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
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
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
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
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“
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);
Components
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!
Components
Component Dependencies
Components seldomly come alone
Components depend on other components which depend on other components which ...
Inspiring people toshare
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
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
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
Components
Practical examples for DI
Example using ...
Constructor Injection
Setter Injection
Autowiring Demo
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
Aspect Oriented Programming
Cross-cutting concerns
Presentation
Domain
Data source
Aspect Oriented Programming
Cross-cutting concerns
Presentation
Domain
Data source
The concerns
live here
Aspect Oriented Programming
Cross-cutting concerns
Event Domain Model
Security
Logging
CONCERNSX-ING
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
Aspect Oriented Programming
Aspects in action
Demo
Read before first use
Try it yourself
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
....
Try it yourself
Installation
You need PHP6
Current installation options:
svn checkout http://5-0.dev.typo3.org/svn/TYPO3/
download the TYPO3 installer
Try it yourself
Installation
When ...?
Recommended literature
Recommended literature
Eric Evans:
Domain Driven DesignTackling Complexity in the Heart of Software
Addison Wesley 2002
Martin Fowler:Patterns of Enterprise Application Architecture
Addison Wesley 2002
Recommended literature
Jimmy Nilsson:Applying Domain-DrivenDesign and Patterns
Addison Wesley 2006
Recommended literature
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
So long and thanks for the fish