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

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

Embed Size (px)

Citation preview

Object Oriented Views

Aki Salmi@rinkkasatiainen

Wednesday, May 20, 15

Programmer@Ambientia

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

Wednesday, May 20, 15

Wednesday, May 20, 15

Content

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

Wednesday, May 20, 15

Very Brief Background

• Confluence plugin

Wednesday, May 20, 15

We have all seen

Wednesday, May 20, 15

Controllers like

Wednesday, May 20, 15

This

Wednesday, May 20, 15

/INDEX (1 of many actions)

ParamsService

“DAO”“or not DAO”

DAO-DAO-DAO

ServiceService

DomainServiceRender

Wednesday, May 20, 15

Not that, not today

Wednesday, May 20, 15

Let’s assume we’re working with thin

controllers

Wednesday, May 20, 15

Have we seen

Wednesday, May 20, 15

views like

Wednesday, May 20, 15

thisOnly today: logic in a view

Wednesday, May 20, 15

So how then?

Wednesday, May 20, 15

How about Code I’ve written myself?

Wednesday, May 20, 15

Not much better

Wednesday, May 20, 15

A Deeper Dive

Params

BUILDCONTEXT

GET MODEL

RENDEROR RENDER

Wednesday, May 20, 15

But there is a catch

Wednesday, May 20, 15

Render Many BlogsOr 1, or 0

Wednesday, May 20, 15

... based on object types

Wednesday, May 20, 15

Build a ‘different’ domain entity?

Build ‘real’

Domain

Build ‘real’

Domain

Wednesday, May 20, 15

Build a ‘different’ Context

BUILD context

Diff context

no context

Wednesday, May 20, 15

Render ‘different’ template

Templ_0Templ_1

Templ_n

Wednesday, May 20, 15

Seems like a violation of SRP

(Single Responsibility Principle)

Wednesday, May 20, 15

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

Visually

Controller

HTTP GET

Domain

View 0 View 1 View N

Wednesday, May 20, 15

It works

Wednesday, May 20, 15

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

What IF

Controller

HTTP GET

Domain

View 0 View 1 View N

Wednesday, May 20, 15

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

Decorate / Exhibit

Controller

HTTP GET

Domain

View

Exhibit

1) build model

2) render

3) exhibit

View tmpl

4) render

Wednesday, May 20, 15

How does it work

Wednesday, May 20, 15

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

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Wednesday, May 20, 15

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Wednesday, May 20, 15

Domain Entity: Group

GroupEntity* immutable

* final private prop* behavior rich

GroupDescriptor* public properties

* tiny behavior

AGroupThatDoesNotExistYet

AGroup

Wednesday, May 20, 15

Domain Entity: Group

<if> ReadOrWriteModel

<if> ReadModel <if> WriteModel

GroupEntity GroupDescrGroupList

Wednesday, May 20, 15

Domain Entity: Group

GroupList* behavior: iterate

Wednesday, May 20, 15

Introducing Exhibits

• Introduced by Avdi Grimm• sort of presenter-decorator

Wednesday, May 20, 15

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

Exhibit BExhibit A

Exhibit visually

Model object

Wednesday, May 20, 15

Exhibits: Group

<if> Exhibit+ renderBody()

GroupListExh GroupList

GroupExhibit GroupEntity

EditGroupExh GroupDesc

GroupWPicture

Wednesday, May 20, 15

Exhibits: Group

GroupListExh ListView

GroupExhibit ShowGroup

EditGroupExh GroupDesc

GroupWPicture ShowGPicture

Wednesday, May 20, 15

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

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

Creating exhibitsthe ruby way 1/2

Avdi Grimm: Object on Rails, Listing 77

Wednesday, May 20, 15

Creating exhibitsthe ruby way 2/2

Avdi Grimm: Object on Rails, Listing 77

Wednesday, May 20, 15

Creating exhibitsthe Java 8 way 1/N

Wednesday, May 20, 15

Time for some codeConfluence pluginVelocity templates

Wednesday, May 20, 15

View

Wednesday, May 20, 15

Controller

Wednesday, May 20, 15

Exhibit A

Wednesday, May 20, 15

Exhibit B

Wednesday, May 20, 15

Exhibit C

Wednesday, May 20, 15

Exhibit D

Wednesday, May 20, 15

And visually

Wednesday, May 20, 15

Surroundings

Exhibit templates

Wednesday, May 20, 15

RemotePictureExhibit ||DefaultPictureExhibit

UserProfileExhibitCould be composite

Wednesday, May 20, 15

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

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

What could I experiment

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

Wednesday, May 20, 15

Thanks!Questions?

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

Wednesday, May 20, 15