where i put my business logic - greach 2014, madrid, spain
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
Where do I put my Business Logic?
Antonio de la Torre@adelatorrefoss
March 28th 2014
Grails is not my Domain Model
Antonio de la Torre@adelatorrefoss
Engineer & Agile Coach @ KaleidosMadrid, Spain
Me
A lot of questions&
one Proposal
1st. step
All code inside Controller
Scaffolding helps
Common methods
MVC&
Skinny ControllerFat Model
2nd. step
Move to Domain Model
Extra large User
God Object&
Services layer
3rd. step
Move to Services
And now XXL Services
Anemic Model&
Transaction Script
Problems
Service with too muchresponsibility
1.- in Creationcomplex objects, constraints and invariants
2.- too much invocationsa lot of injected services, no separation
3.- how to save()?addTo, hstore, GORM, mongo, ...
Persistence is a Hardware problem
No OOP
We implement behaviour in Agile
Where is our Business Logic?
but …
here it is ...
How looks like our unit tests?
mocks & stored data
Actions
Actions
Single Responsibility Principle (SRP)
SOLID
Tell, don’t ask
“Object Calisthenics. 9 steps to better SW design” , Jeff Bay
GRASP
… are all smells
OOP?
What about FP?
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
So better if we useAgnostic Patterns
Domain Driven Designby Eric Evans
DDD
“The goal of domain-driven design is to create better software by focusing on a model of the domain rather than the technology”
Tackling complexity
DDD is a way of dealing with complexity.
Complex is easy. Simple is a lot harder.
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”
DDD leads to Model Driven Design
Test your model
Behaviour centric
Layered Arquitecture
Diagram
Entities
“When an object is distinguished by its identity, rather than its attributes, make this primary to its definition in the model”
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…”
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.”
Modules
“Choose MODULES that tell the story of the system and contain a cohesive set of concepts.”
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.”
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.”
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.”
Implementing
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.
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
Implementing Services
With our Services in Grails● Dependency Injection● Transaction support
Implementing Modules
Grails plug-in mechanism.
Implementing Aggregates
Aggregates are implemented in the hasMany - belongsTo relationship.Can control the root access within a Service.
Implementing Factories
Implement Factories classes to create new instances with an abstract interface… It could be configurable with properties.
Implementing Repositories
Grails Service can be used to implement a dedicated Repository object that simply delegates its operation to Grails GORM.
All together
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 …
In code ...
Model your domain, create your business logic, and separate it from your infrastructure.
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
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
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)