Top Banner
Object Oriented Views Aki Salmi @rinkkasatiainen Wednesday, May 20, 15
63

Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Aug 14, 2015

Download

Technology

Mozaic Works
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Object Oriented Views

Aki Salmi@rinkkasatiainen

Wednesday, May 20, 15

Page 2: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Programmer@Ambientia

skype: @rinkkasatiainentwitter: @rinkkasatiainenemail: [email protected]

Wednesday, May 20, 15

Page 3: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Wednesday, May 20, 15

Page 4: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Content

• Background• A way to solve• Discussion• Open Space: Mob Programming?

Wednesday, May 20, 15

Page 5: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Very Brief Background

• Confluence plugin

Wednesday, May 20, 15

Page 6: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

We have all seen

Wednesday, May 20, 15

Page 7: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Controllers like

Wednesday, May 20, 15

Page 8: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

This

Wednesday, May 20, 15

Page 9: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

/INDEX (1 of many actions)

ParamsService

“DAO”“or not DAO”

DAO-DAO-DAO

ServiceService

DomainServiceRender

Wednesday, May 20, 15

Page 10: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Not that, not today

Wednesday, May 20, 15

Page 11: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Let’s assume we’re working with thin

controllers

Wednesday, May 20, 15

Page 12: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Have we seen

Wednesday, May 20, 15

Page 13: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

views like

Wednesday, May 20, 15

Page 14: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

thisOnly today: logic in a view

Wednesday, May 20, 15

Page 15: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

So how then?

Wednesday, May 20, 15

Page 16: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

How about Code I’ve written myself?

Wednesday, May 20, 15

Page 17: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Not much better

Wednesday, May 20, 15

Page 18: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

A Deeper Dive

Params

BUILDCONTEXT

GET MODEL

RENDEROR RENDER

Wednesday, May 20, 15

Page 19: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

But there is a catch

Wednesday, May 20, 15

Page 20: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Render Many BlogsOr 1, or 0

Wednesday, May 20, 15

Page 21: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

... based on object types

Wednesday, May 20, 15

Page 22: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Build a ‘different’ domain entity?

Build ‘real’

Domain

Build ‘real’

Domain

Wednesday, May 20, 15

Page 23: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Build a ‘different’ Context

BUILD context

Diff context

no context

Wednesday, May 20, 15

Page 24: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Render ‘different’ template

Templ_0Templ_1

Templ_n

Wednesday, May 20, 15

Page 25: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Seems like a violation of SRP

(Single Responsibility Principle)

Wednesday, May 20, 15

Page 26: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

What did we see

• build parameters• build context (on 2 places)• build domain• render different data, differently, based

on the domain entity

Wednesday, May 20, 15

Page 27: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Visually

Controller

HTTP GET

Domain

View 0 View 1 View N

Wednesday, May 20, 15

Page 28: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

It works

Wednesday, May 20, 15

Page 29: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

..sort of

• most of the bugs I introduced, were on this behavior of the system.• This class• Concepts introduced by this class• Integration with Confluence API

• especially ‘null’s

Wednesday, May 20, 15

Page 30: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

What IF

Controller

HTTP GET

Domain

View 0 View 1 View N

Wednesday, May 20, 15

Page 31: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

What IF...C’s responsibility

• Given a set of parameters...• Build Model (service / repository) • Render Template with model given

• and let other’s to figure out which template to render

Wednesday, May 20, 15

Page 32: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Decorate / Exhibit

Controller

HTTP GET

Domain

View

Exhibit

1) build model

2) render

3) exhibit

View tmpl

4) render

Wednesday, May 20, 15

Page 33: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

How does it work

Wednesday, May 20, 15

Page 34: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

few design decision done by me

• avoid nulls • separate Read Model from Write Model• 1st class collections• Separated Query and Command

Controller (sort of CQRS)

Wednesday, May 20, 15

Page 35: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Wednesday, May 20, 15

Page 36: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Wednesday, May 20, 15

Page 37: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Domain Entity: Group

GroupEntity* immutable

* final private prop* behavior rich

GroupDescriptor* public properties

* tiny behavior

AGroupThatDoesNotExistYet

AGroup

Wednesday, May 20, 15

Page 38: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Wednesday, May 20, 15

Page 39: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Domain Entity: Group

GroupList* behavior: iterate

Wednesday, May 20, 15

Page 40: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Introducing Exhibits

• Introduced by Avdi Grimm• sort of presenter-decorator

Wednesday, May 20, 15

Page 41: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibit Characteristics

• Wraps a single model instance• Is a true Decorator• Brings together model and context• Encapsulates the decision about how to

render an object• May modify the behavior of an object

Wednesday, May 20, 15

Page 42: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibit BExhibit A

Exhibit visually

Model object

Wednesday, May 20, 15

Page 43: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibits: Group

<if> Exhibit+ renderBody()

GroupListExh GroupList

GroupExhibit GroupEntity

EditGroupExh GroupDesc

GroupWPicture

Wednesday, May 20, 15

Page 44: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibits: Group

GroupListExh ListView

GroupExhibit ShowGroup

EditGroupExh GroupDesc

GroupWPicture ShowGPicture

Wednesday, May 20, 15

Page 45: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

REST viewHTTP PATH Contr#Action Domain Class

GET /foo(.:format) foos#index FooList

POST /foo(.:format) foos#create FooDescriptor

GET /foo/new foos#new FooDescriptor

GET /foo/:id/edit foos#edit FooDescriptor

GET /foo/:id foos#show FooEntry

PATCH/PUT /foo/:id foos#update FooDescriptor

DELETE /foo/:id foos#delete FooDescriptor

Wednesday, May 20, 15

Page 46: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Creating exhibits

public exhibit(ReadWriteModel model){for_each( exhibit: exhibit_classes ){

if( exhibit.applicableTo( model )// wrap model to exhibit and serve exhibit as new model

}}

Wednesday, May 20, 15

Page 47: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Creating exhibitsthe ruby way 1/2

Avdi Grimm: Object on Rails, Listing 77

Wednesday, May 20, 15

Page 48: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Creating exhibitsthe ruby way 2/2

Avdi Grimm: Object on Rails, Listing 77

Wednesday, May 20, 15

Page 49: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Creating exhibitsthe Java 8 way 1/N

Wednesday, May 20, 15

Page 50: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Time for some codeConfluence pluginVelocity templates

Wednesday, May 20, 15

Page 51: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

View

Wednesday, May 20, 15

Page 52: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Controller

Wednesday, May 20, 15

Page 53: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibit A

Wednesday, May 20, 15

Page 54: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibit B

Wednesday, May 20, 15

Page 55: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibit C

Wednesday, May 20, 15

Page 56: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Exhibit D

Wednesday, May 20, 15

Page 57: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

And visually

Wednesday, May 20, 15

Page 58: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Surroundings

Exhibit templates

Wednesday, May 20, 15

Page 59: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

RemotePictureExhibit ||DefaultPictureExhibit

UserProfileExhibitCould be composite

Wednesday, May 20, 15

Page 60: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Did it work - in retrospect

• No issues in production with this code• It is not, by far, easy to grasp. Without

someone explaining

Wednesday, May 20, 15

Page 61: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

What I did not show

• transformations of domain entities:• Request -> Write -> Persistence• Persistence -> Write (-> Read)

• Composite Exhibit - how to show 0, 1 or many concepts, dynamically, based on the ModelObject rendered

Wednesday, May 20, 15

Page 62: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

What could I experiment

• Capabilities for exhibits• Capability.mobile• Capability.REST• Capability.json

Wednesday, May 20, 15

Page 63: Aki Salmi: Object Oriented Views at I T.A.K.E. Unconference 2015

Thanks!Questions?

skype: @rinkkasatiainentwitter: @rinkkasatiainenemail: [email protected]

Wednesday, May 20, 15