Object Oriented Views / Aki Salmi

69
Object Oriented Views Aki Salmi @rinkkasatiainen

Transcript of Object Oriented Views / Aki Salmi

Page 1: Object Oriented Views / Aki Salmi

Object Oriented Views

Aki Salmi @rinkkasatiainen

Page 2: Object Oriented Views / Aki Salmi

I like to do crazy stuff

codefreeze.fi experimental coding

Page 3: Object Oriented Views / Aki Salmi

Programmer @Ambientia

skype: rinkkasatiainentwitter: @rinkkasatiainenweb: rinkkasatiainen.fiemail: [email protected]

Page 4: Object Oriented Views / Aki Salmi
Page 5: Object Oriented Views / Aki Salmi

Goal for this talk

Page 6: Object Oriented Views / Aki Salmi

is to

Page 7: Object Oriented Views / Aki Salmi

Initiate discussion

Page 8: Object Oriented Views / Aki Salmi

anyone @ Socrates 2014

Page 9: Object Oriented Views / Aki Salmi
Page 10: Object Oriented Views / Aki Salmi

Content

• Background

• A way to solve

• Discussion

• Open Space: Mob Programming?

Page 11: Object Oriented Views / Aki Salmi

Very Brief Background

• Confluence plugin

• real production code (used by thousands, daily)

Page 12: Object Oriented Views / Aki Salmi

We have all seen

Page 13: Object Oriented Views / Aki Salmi

Controllers like

Page 14: Object Oriented Views / Aki Salmi

This

Page 15: Object Oriented Views / Aki Salmi

/INDEX (1 of many actions)

ParamsService

“DAO”“or not DAO”

DAO-DAO-DAO

ServiceService

DomainServiceRender

Page 16: Object Oriented Views / Aki Salmi

Not that, not today

Page 17: Object Oriented Views / Aki Salmi

Let’s assume we’re working with thin

controllers

Page 18: Object Oriented Views / Aki Salmi

Have we seen

Page 19: Object Oriented Views / Aki Salmi

views like

Page 20: Object Oriented Views / Aki Salmi

this Only today: logic in a view

Page 21: Object Oriented Views / Aki Salmi

So how then?

Page 22: Object Oriented Views / Aki Salmi

How about Code I’ve written myself?

Page 23: Object Oriented Views / Aki Salmi

Not much better

Page 24: Object Oriented Views / Aki Salmi

A Deeper Dive

Params

BUILD CONTEXT

GET MODEL

RENDEROR RENDER

Page 25: Object Oriented Views / Aki Salmi

But there is a catch

Page 26: Object Oriented Views / Aki Salmi

Render Many Blogs Or 1, or 0

Page 27: Object Oriented Views / Aki Salmi

... based on object types

Page 28: Object Oriented Views / Aki Salmi

Build a ‘different’ domain entity?

Build ‘real’

Domain

Build ‘real’

Domain

Page 29: Object Oriented Views / Aki Salmi

Build a ‘different’ Context

BUILD context

Diff context

no context

Page 30: Object Oriented Views / Aki Salmi

Render ‘different’ template

Templ_0Templ_1

Templ_n

Page 31: Object Oriented Views / Aki Salmi

Seems like a violation of SRP

(Single Responsibility Principle)

Page 32: Object Oriented Views / Aki Salmi

What did we see

• build parameters

• build context (on 2 places)

• build domain

• render different data, differently, based on the domain entity

Page 33: Object Oriented Views / Aki Salmi

Visually

Controller

HTTP GET

Domain

View 0 View 1 View N

Page 34: Object Oriented Views / Aki Salmi

It works

Page 35: Object Oriented Views / Aki Salmi

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

Page 36: Object Oriented Views / Aki Salmi

What IF

Controller

HTTP GET

Domain

View 0 View 1 View N

Page 37: Object Oriented Views / Aki Salmi

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 is the view to render

Page 38: Object Oriented Views / Aki Salmi

Decorate / Exhibit

Controller

HTTP GET

Domain

View

Exhibit

1) build model

2) render

3) exhibit

View tmpl

4) render

Page 39: Object Oriented Views / Aki Salmi

How does it work

Page 40: Object Oriented Views / Aki Salmi

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)

Page 41: Object Oriented Views / Aki Salmi

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Page 42: Object Oriented Views / Aki Salmi

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Page 43: Object Oriented Views / Aki Salmi

Domain Entity: Group

GroupEntity* immutable

* final private prop* behavior rich

GroupDescriptor* public properties

* tiny behavior

AGroupThatDoesNotExistYet

AGroup

Page 44: Object Oriented Views / Aki Salmi

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Page 45: Object Oriented Views / Aki Salmi

Domain Entity: Group

GroupList* behavior: iterate

Page 46: Object Oriented Views / Aki Salmi

Introducing Exhibits

• Introduced by Avdi Grimm

• sort of presenter-decorator

Page 47: Object Oriented Views / Aki Salmi

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

Page 48: Object Oriented Views / Aki Salmi

Exhibit BExhibit A

Exhibit visually

Model object

Page 49: Object Oriented Views / Aki Salmi

Exhibits: Group

<if> Exhibit+ renderBody()

GroupListExh GroupList

GroupExhibit GroupEntity

EditGroupExh GroupDesc

GroupWPicture

Page 50: Object Oriented Views / Aki Salmi

Exhibits: Group

GroupListExh ListView

GroupExhibit GroupView

EditGroupExh EditGroup

GroupWPicture GPictureView

Page 51: Object Oriented Views / Aki Salmi

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

Page 52: Object Oriented Views / Aki Salmi

Creating exhibits the ruby way 1/2

Avdi Grimm: Object on Rails, Listing 76

Page 53: Object Oriented Views / Aki Salmi

Creating exhibits the ruby way 2/2

Avdi Grimm: Object on Rails, Listing 77

Page 54: Object Oriented Views / Aki Salmi

Creating exhibits the Java 8 way 1/N

Page 55: Object Oriented Views / Aki Salmi

Time for some code Confluence plugin Velocity templates

Page 56: Object Oriented Views / Aki Salmi

View

Page 57: Object Oriented Views / Aki Salmi

Controller

Page 58: Object Oriented Views / Aki Salmi

Exhibit A

Page 59: Object Oriented Views / Aki Salmi

Exhibit B

Page 60: Object Oriented Views / Aki Salmi

Exhibit C

Page 61: Object Oriented Views / Aki Salmi

Exhibit D

Page 62: Object Oriented Views / Aki Salmi

Decorate / Exhibit

Controller

HTTP GET

Domain

View

Exhibit

1) build model

2) render

3) exhibit

View tmpl

4) render

Page 63: Object Oriented Views / Aki Salmi

And visually

Page 64: Object Oriented Views / Aki Salmi

Surroundings

Exhibit templates

Page 65: Object Oriented Views / Aki Salmi

RemotePictureExhibit ||DefaultPictureExhibit

UserProfileExhibitCould be composite

Page 66: Object Oriented Views / Aki Salmi

Did it work - in retrospect

• No issues in production with this code

• It is not, by far, easy to grasp. Without someone explaining

Page 67: Object Oriented Views / Aki Salmi

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

Page 68: Object Oriented Views / Aki Salmi

What could I experiment

• Capabilities for exhibits

• Capability.mobile

• Capability.REST

• Capability.json

Page 69: Object Oriented Views / Aki Salmi

Thanks! Questions?

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