Slide 1

Slide 1 text

A resource orientated framework using the DI /AOP/REST Triangle BEAR.Sunday (2017)

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

BEAR.Sunday is an application framework. But it offers no libraries. (We have good stuff)

Slide 4

Slide 4 text

instead, it offers three object frameworks.

Slide 5

Slide 5 text

What is framework ?

Slide 6

Slide 6 text

“imposes a set of design constraints on end-user code.”

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

3.hypermedia framework

Slide 10

Slide 10 text

DI benefits • Dependency inversion principle (DIP) • Object instantiation / usage separation

Slide 11

Slide 11 text

DIP • Code should depend on things that are at the same or higher level of abstraction • High level policy should not depend on low level details

Slide 12

Slide 12 text

“The principle of dependency inversion is at the root of many of the benefits claimed for object- oriented technology. Its proper application is necessary for the creation of reusable frameworks”

Slide 13

Slide 13 text

Ray.DI dependency injection framework

Slide 14

Slide 14 text

/** * @Inject */ public function setRenderer(RenderInterface $renderer) { ... 1.annotate at injection point class RendererModule extends AbstractModule { protected function configure() { $this->bind('RenderInterface') ->to('HalRenderer') ->in(Scope::SINGLETON); } $injector = new Injector(new RendererModule); 2 bind abstraction to concretion 3.create an injector

Slide 15

Slide 15 text

/** * @Inject */ public function setRenderer(RenderInterface $renderer) { ... 1.annotate at injection point class RendererModule extends AbstractModule { protected function configure() { $this->bind('RenderInterface') ->to('HalRenderer') ->in(Scope::SINGLETON); } $injector = new Injector(new RendererModule); 2. bind abstraction to concretion 3.create an injector

Slide 16

Slide 16 text

$injector = new Injector(new HalRendererModule); /** * @Inject */ public function setRenderer(RenderInterface $renderer) { ... 1.annotate at injection point class RendererModule extends AbstractModule { protected function configure() { $this->bind('RenderInterface') ->to('HalRenderer') ->in(Scope::SINGLETON); } 2 bind abstraction to concretion 3.create an injector

Slide 17

Slide 17 text

$user = $injector->getInstance('UserInterface');
 4. Get the object with dependent User Render er Renderer Interface depends A B

Slide 18

Slide 18 text

QSPDFEVSBM PCKFDUPSJFUOFE DPNQJMF SVOUJNF Implement the structure, not a procedure

Slide 19

Slide 19 text

class RendererModule extends AbstractModule { protected function configure() { $this ->bind('RenderInterface') ->to('HalRenderer') ->in(Scope::SINGLETON); } } Use concrete class only in compile

Slide 20

Slide 20 text

$renderer = $injector->getInstance('UserInterface');
 Only abstraction in runtime /** * @Inject */ public function __construct(RenderInterface $renderer) {

Slide 21

Slide 21 text

DI Best practice “Your code should deal directly with the Injector as little as possible. Instead, you want to bootstrap your application by injecting one root object.”

Slide 22

Slide 22 text

Application = one root object

Slide 23

Slide 23 text

Application class

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Application is root object retrieved with injector:
 
 
 
 
 app name app context

Slide 26

Slide 26 text

Application has dependency.

Slide 27

Slide 27 text

Dependency has dependency and so on.. Object is either contain or belonged

Slide 28

Slide 28 text

You get a application object graph. huge, but can be stored one single root value $app

Slide 29

Slide 29 text

"QQMJDBUJPODBOCFTFSJBMJ[FE $app can be serialized and stored Injection is reused beyond requests.

Slide 30

Slide 30 text

$app Object i/f i/f Object i/f i/f Object Router Response JSON XM L 1st framework: DI Framework • DI framework w/ binding DSL • compile / runtime separation • use only “socket” in runtime • application is single big one value • implement structure, not behavior

Slide 31

Slide 31 text

Aspect Oriented Programing

Slide 32

Slide 32 text

What is AOP? Cache Log Auth A programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns

Slide 33

Slide 33 text

/** * @Cacheable */
 class Post { public function onGet($id) { // ... $this->body = $stmt->fetchAll(PDO::FETCH_ASSOC); return $this; } class Post extends AppModel { public function newest() { $result = Cache::read('newest_posts', 'longterm'); if (!$result) { $result = $this->find('all'); Cache::write('newest_posts', $result, 'longterm'); } return $result; } }

Slide 34

Slide 34 text

M C I I AOP

Slide 35

Slide 35 text

M C Cache Cache is called by method invocation,
 If the cache is warm the model is never called. $obj->read(2); Miss !

Slide 36

Slide 36 text

Aspects Core Concern Cross Cutting Concern Separation

Slide 37

Slide 37 text

Rock Concert Example

Slide 38

Slide 38 text

interface MethodInterceptor { public function invoke(MethodInvocation $invocation); } 


Slide 39

Slide 39 text

class Transactional implements MethodInterceptor { public function invoke(MethodInvocation $invocation) { $object = $invocation->getThis(); $ref = new ReflectionProperty($object, 'db'); $ref->setAccessible(true); $db = $ref->getValue($object); $db->beginTransaction(); try { $invocation->proceed(); $db->commit(); } catch (Exception $e) { $db->rollback(); } } } Transactional interceptor Core Concern Cross Cutting Concern

Slide 40

Slide 40 text

class CacheInterceptor implements MethodInterceptor { public function invoke(MethodInvocation $invocation) { $obj = $invocation->getThis(); $args = $invocation->getArguments(); $id = get_class($obj) . serialize($args); $saved = $this->cache->fetch($id); if ($saved) { return $saved; } $result = $invocation->proceed(); $this->cache->save($id, $result); return $result; } } Core Concern Cross Cutting Concern Cache interceptor

Slide 41

Slide 41 text

4JNQMZBOOPUBUF UIFODSFBUFZPVSCJOEJOH #JOE

Slide 42

Slide 42 text

Layering by context • MVC, Is 3 enough ?

Slide 43

Slide 43 text

API Client

Slide 44

Slide 44 text

API Log Client Valid Auth

Slide 45

Slide 45 text

API Log !7BMJE BENJO %&-&5& Client Valid Auth

Slide 46

Slide 46 text

Aspect layering by context Model Cache Form Transaction Auth Validation

Slide 47

Slide 47 text

rayAopBind[__FUNCTION__])) { return call_user_func_array('parent::' . __FUNCTION__, func_get_args()); } // proceed source method from interceptor if (!$this->rayAopIntercept) { $this->rayAopIntercept = true; return call_user_func_array('parent::' . __FUNCTION__, func_get_args()); } // proceed next interceptor $this->rayAopIntercept = false; $interceptors = $this->rayAopBind[__FUNCTION__]; $annotation = isset($this->rayAopBind->annotation[__FUNCTION__]) ? $this->rayAopBind >annotation[__FUNCTION__] : null; $invocation = new \Ray\Aop\ReflectiveMethodInvocation(array($this, __FUNCTION__), func_get_args(), $interceptors, $annotation); return $invocation->proceed(); } Under the hood: Method interception sub class is created in order enable this interception and keep type safety.

Slide 48

Slide 48 text

Runtime injection by aspect • method / parameter lookup • test friendly

Slide 49

Slide 49 text

2nd framework: Aspect Oriented Framework • AOP alliance standard • Layering by context • Type safe • Runtime injection

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Hypermedia framework for object as a service It allows objects to have RESTful web service benefits such as client-server, uniform interface, statelessness, resource expression with mutual connectivity and layered components.

Slide 52

Slide 52 text

CLI Web

Slide 53

Slide 53 text

Embedded Resource

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Linked Resource

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Web Context Parameter

Slide 59

Slide 59 text

Resource Parameter

Slide 60

Slide 60 text

https://www.infoq.com/jp/articles/webber-rest-workflow Figure1 The Customer State Machine Figure 2 The Barista's State Machine Hypermedia as the Engine of Application State

Slide 61

Slide 61 text

Hypermedia as the Engine of Application State

Slide 62

Slide 62 text

Order Payment hyper reference: payment Hypermedia Driven API

Slide 63

Slide 63 text

Content-Type: application/hal+json

Slide 64

Slide 64 text

https://www.infoq.com/jp/news/2014/03/amazon-hal-appstream

Slide 65

Slide 65 text

The key of success of web • URI • Unified Interface • Hyperlink

Slide 66

Slide 66 text

• API is hub • API is core value API driven development DB Mobil e Web API Cloud Moc k URI API API

Slide 67

Slide 67 text

http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html

Slide 68

Slide 68 text

Layered Resource UI Mobile Web Page Resource App script App Resource Entity

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

1. Entry file

Slide 72

Slide 72 text

2. Application script ᶃ create root object with context ᶄ 304 ? ᶅ create request ᶆ invoke request ᶇ transfer

Slide 73

Slide 73 text

3. Resource state

Slide 74

Slide 74 text

3. Representation state transfer

Slide 75

Slide 75 text

Performance • annotation ? dependency injection ? 
 method interception ? DSL ? named parameter ? • Fast • cache all compiled object • generate raw factory code • http friendly architecture

Slide 76

Slide 76 text

Scale • “model proxy” pattern • ‘app://self/blog/entry’ can be anything. • contextual injection makes db scale easy • assisted injection

Slide 77

Slide 77 text

Hard spot / Soft spot • DI configure hard spot. QFSTZTUFN • Aop configure softspot, change on request


Slide 78

Slide 78 text

Connecting frameworks • DI - object as dependency • AOP - domain logic to application logic • Hypermedia - resource to resource

Slide 79

Slide 79 text

Abstraction frameworks • DSL • Annotation • URI • Interface • Aspects • Hypermedia

Slide 80

Slide 80 text

AOP (Gregor Kiczales) DI (Martin Fowler) REST (Roy Fielding) OOP (Allan Kay) Annotation (Anders Hejlsberg) Guice (Bob Lee)

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

“Zen” framework

Slide 83

Slide 83 text

Thanks. http://www.flickr.com/photos/stevehoad/4678289858/ @mackstar @koriym