where i put my business logic - greach 2014, madrid, spain

65
Where do I put my Business Logic? Antonio de la Torre @adelatorrefoss March 28th 2014 Grails is not my Domain Model

Upload: antonio-de-la-torre-fernandez

Post on 06-May-2015

735 views

Category:

Software


2 download

DESCRIPTION

WHERE DO I HAVE TO PUT MY BUSINESS LOGIC? GRAILS IS NOT MY DOMAIN MODEL. Greach 2014, Madrid, Spain Presented March, 28th, 2014 What’s business logic exactly? What’s a domain model? How does the use of a framework like Grails influence how we build a domain model? How can the use of patterns helps us in building our system? This talk will approach the problems that we have to face when we put a tool in the core of our system and we will discuss whether the price we pay for using the Grails framework is too high. A lot of resources at the end!!

TRANSCRIPT

Page 1: Where i put my business logic  - Greach 2014, Madrid, Spain

Where do I put my Business Logic?

Antonio de la Torre@adelatorrefoss

March 28th 2014

Grails is not my Domain Model

Page 2: Where i put my business logic  - Greach 2014, Madrid, Spain

Antonio de la Torre@adelatorrefoss

Engineer & Agile Coach @ KaleidosMadrid, Spain

Me

Page 3: Where i put my business logic  - Greach 2014, Madrid, Spain
Page 4: Where i put my business logic  - Greach 2014, Madrid, Spain

A lot of questions&

one Proposal

Page 5: Where i put my business logic  - Greach 2014, Madrid, Spain
Page 6: Where i put my business logic  - Greach 2014, Madrid, Spain
Page 7: Where i put my business logic  - Greach 2014, Madrid, Spain

1st. step

All code inside Controller

Page 8: Where i put my business logic  - Greach 2014, Madrid, Spain

Scaffolding helps

Page 9: Where i put my business logic  - Greach 2014, Madrid, Spain

Common methods

Page 10: Where i put my business logic  - Greach 2014, Madrid, Spain

MVC&

Skinny ControllerFat Model

Page 11: Where i put my business logic  - Greach 2014, Madrid, Spain

2nd. step

Move to Domain Model

Page 12: Where i put my business logic  - Greach 2014, Madrid, Spain

Extra large User

Page 13: Where i put my business logic  - Greach 2014, Madrid, Spain

God Object&

Services layer

Page 14: Where i put my business logic  - Greach 2014, Madrid, Spain

3rd. step

Move to Services

Page 15: Where i put my business logic  - Greach 2014, Madrid, Spain

And now XXL Services

Page 16: Where i put my business logic  - Greach 2014, Madrid, Spain

Anemic Model&

Transaction Script

Page 17: Where i put my business logic  - Greach 2014, Madrid, Spain

Problems

Page 18: Where i put my business logic  - Greach 2014, Madrid, Spain

Service with too muchresponsibility

Page 19: Where i put my business logic  - Greach 2014, Madrid, Spain

1.- in Creationcomplex objects, constraints and invariants

Page 20: Where i put my business logic  - Greach 2014, Madrid, Spain

2.- too much invocationsa lot of injected services, no separation

Page 21: Where i put my business logic  - Greach 2014, Madrid, Spain

3.- how to save()?addTo, hstore, GORM, mongo, ...

Page 22: Where i put my business logic  - Greach 2014, Madrid, Spain

Persistence is a Hardware problem

Page 23: Where i put my business logic  - Greach 2014, Madrid, Spain

No OOP

Page 24: Where i put my business logic  - Greach 2014, Madrid, Spain

We implement behaviour in Agile

Page 25: Where i put my business logic  - Greach 2014, Madrid, Spain

Where is our Business Logic?

but …

Page 26: Where i put my business logic  - Greach 2014, Madrid, Spain

here it is ...

Page 27: Where i put my business logic  - Greach 2014, Madrid, Spain

How looks like our unit tests?

mocks & stored data

Page 28: Where i put my business logic  - Greach 2014, Madrid, Spain

Actions

Page 29: Where i put my business logic  - Greach 2014, Madrid, Spain

Actions

Single Responsibility Principle (SRP)

SOLID

Tell, don’t ask

“Object Calisthenics. 9 steps to better SW design” , Jeff Bay

GRASP

Page 30: Where i put my business logic  - Greach 2014, Madrid, Spain

… are all smells

Page 31: Where i put my business logic  - Greach 2014, Madrid, Spain

OOP?

What about FP?

Page 32: Where i put my business logic  - Greach 2014, Madrid, Spain

Functional Programming

- Inmutability- Transaction scripts- Stateless services- Stateless API- Rare workflows … - Objects with state… but we don’t usually use them

“Why OO in web, when usually is DB -> Object -> Process -> Object -> DB”Functional Programming for the Object-Oriented Programmer Brian Marick

Page 33: Where i put my business logic  - Greach 2014, Madrid, Spain

So better if we useAgnostic Patterns

Page 34: Where i put my business logic  - Greach 2014, Madrid, Spain

Domain Driven Designby Eric Evans

DDD

Page 35: Where i put my business logic  - Greach 2014, Madrid, Spain

“The goal of domain-driven design is to create better software by focusing on a model of the domain rather than the technology”

Page 36: Where i put my business logic  - Greach 2014, Madrid, Spain

Tackling complexity

DDD is a way of dealing with complexity.

Complex is easy. Simple is a lot harder.

Page 37: Where i put my business logic  - Greach 2014, Madrid, Spain

Ubiquitous Language

“A language structured around the domain model and used by all team members to connect all the activities of the team with the software”

Page 38: Where i put my business logic  - Greach 2014, Madrid, Spain

DDD leads to Model Driven Design

Page 39: Where i put my business logic  - Greach 2014, Madrid, Spain

Test your model

Page 40: Where i put my business logic  - Greach 2014, Madrid, Spain

Behaviour centric

Page 41: Where i put my business logic  - Greach 2014, Madrid, Spain

Layered Arquitecture

Diagram

Page 42: Where i put my business logic  - Greach 2014, Madrid, Spain
Page 43: Where i put my business logic  - Greach 2014, Madrid, Spain
Page 44: Where i put my business logic  - Greach 2014, Madrid, Spain

Entities

“When an object is distinguished by its identity, rather than its attributes, make this primary to its definition in the model”

Page 45: Where i put my business logic  - Greach 2014, Madrid, Spain

Value Objects

“When you care only about the attributes of an element of the model, classify it as a VALUE OBJECT. Don’t give it any identity…”

Page 46: Where i put my business logic  - Greach 2014, Madrid, Spain

Services

“When a significant process or transformation in the domain is not a natural responsibility of an ENTITY or VALUE OBJECT, add an operation to the model as a standalone interface declared as a SERVICE. Make the SERVICE stateless.”

Page 47: Where i put my business logic  - Greach 2014, Madrid, Spain

Modules

“Choose MODULES that tell the story of the system and contain a cohesive set of concepts.”

Page 48: Where i put my business logic  - Greach 2014, Madrid, Spain

Aggregates

“Cluster the ENTITIES and VALUE OBJECTS into AGGREGATES and define boundaries around each. Choose one ENTITY to be the root of each AGGREGATE, and control all access to the objects inside the boundary through the root. Allow external objects to hold references to the root only.”

Page 49: Where i put my business logic  - Greach 2014, Madrid, Spain

Factories

“Shift the responsibility for creating instances of complex objects and AGGREGATES to a separate object, which may itself have no responsibility in the domain model but is still part of the domain design.”

Page 50: Where i put my business logic  - Greach 2014, Madrid, Spain

Repositories

“For each type of object that needs global access, create an object that can provide the illusion of an in-memory collection of all objects of that type.”

Page 51: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing

Page 52: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing Entities

These are Domain Classes in Grails. They come with persistence already resolved through GORM.

hasOne vs. belongsTo property can be used to define the lifecycle of entities and their relationships.

Page 53: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing Value Objects

In Grails, you can use “embedded” property in GORM field to manage a value object.

And deal with them with POGO or Command Objects

Page 54: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing Services

With our Services in Grails● Dependency Injection● Transaction support

Page 55: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing Modules

Grails plug-in mechanism.

Page 56: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing Aggregates

Aggregates are implemented in the hasMany - belongsTo relationship.Can control the root access within a Service.

Page 57: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing Factories

Implement Factories classes to create new instances with an abstract interface… It could be configurable with properties.

Page 58: Where i put my business logic  - Greach 2014, Madrid, Spain

Implementing Repositories

Grails Service can be used to implement a dedicated Repository object that simply delegates its operation to Grails GORM.

Page 59: Where i put my business logic  - Greach 2014, Madrid, Spain

All together

Page 60: Where i put my business logic  - Greach 2014, Madrid, Spain

All together …

In a more complex Aggregation, create a service to manage, this service acts as a Repository. Control invariants.

To access a simple Entity use GORM.In a Domain Model with hasOne and belongsTo can act as an Aggregate Root, and use GORM directly.

And we reserve Factories to instantiate external providers, like email service, S3 access, push mobile notifications, and so on …

Page 61: Where i put my business logic  - Greach 2014, Madrid, Spain

In code ...

Page 62: Where i put my business logic  - Greach 2014, Madrid, Spain

Model your domain, create your business logic, and separate it from your infrastructure.

Page 63: Where i put my business logic  - Greach 2014, Madrid, Spain

ResourcesDomain Driven Design

Eric Evans

http://www.slideshare.net/thinkddd/domain-driven-design-dddsydney-2011

- (Intro) Complex vs Simple

http://pragprog.com/articles/tell-dont-ask

- Tell, don't ask

http://juan-garcia-carmona.blogspot.com.es/2012/11/solid-y-grasp-buenas-practicas-hacia-el.html

- SOLID

- Spanish

http://www.martinfowler.com/bliki/AnemicDomainModel.html

- Anemic Model

Page 64: Where i put my business logic  - Greach 2014, Madrid, Spain

Resourceshttp://www.slideshare.net/sergiopolo/introduccin-a-ddd

(Spanish)

- Building Blocks

http://www.slideshare.net/harshjegadeesan/domain-driven-design-presentation

STARRED- Intro

- Building Blocks

http://www.slideshare.net/GlenWilson/domain-driven-design-pattern-summaries-presentation

- Extense document with Pattern Summaries

http://www.slideshare.net/ziobrando/taming-complex-domains-with-domain-driven-design-presentation

STARRED!!- Building Blocks

- Large Scale DDD

Page 65: Where i put my business logic  - Greach 2014, Madrid, Spain

Resourceshttp://www.slideshare.net/DimkaG/domain-driven-design-and-model-driven-development- ideas to implement the building blocks.

http://blog.refactoringin.net/2011/08/17/grails-as-a-ddd-platform/- Unique reference directly with Grails- Repository??? with GORM…

Brian Marick - Functional Programming for the Object-Oriented Programmerhttps://leanpub.com/fp-oo

http://www.mabishu.com/blog/2012/12/14/object-calisthenics-write-better-object-oriented-code/Object Calisthenics Write Better OO Code

Javier Acero - Mi dominio es mio y no lo compartohttp://vimeo.com/69157481(Spanish)