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


Published on


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

Published in: Software
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

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

  1. 1. Where do I put my Business Logic? Antonio de la Torre @adelatorrefoss March 28th 2014 Grails is not my Domain Model
  2. 2. Antonio de la Torre @adelatorrefoss Engineer & Agile Coach @ Kaleidos Madrid, Spain Me
  3. 3. A lot of questions & one Proposal
  4. 4. 1st. step All code inside Controller
  5. 5. Scaffolding helps
  6. 6. Common methods
  7. 7. MVC & Skinny Controller Fat Model
  8. 8. 2nd. step Move to Domain Model
  9. 9. Extra large User
  10. 10. God Object & Services layer
  11. 11. 3rd. step Move to Services
  12. 12. And now XXL Services
  13. 13. Anemic Model & Transaction Script
  14. 14. Problems
  15. 15. Service with too much responsibility
  16. 16. 1.- in Creation complex objects, constraints and invariants
  17. 17. 2.- too much invocations a lot of injected services, no separation
  18. 18. 3.- how to save()? addTo, hstore, GORM, mongo, ...
  19. 19. Persistence is a Hardware problem
  20. 20. No OOP
  21. 21. We implement behaviour in Agile
  22. 22. Where is our Business Logic? but …
  23. 23. here it is ...
  24. 24. How looks like our unit tests? mocks & stored data
  25. 25. Actions
  26. 26. Actions Single Responsibility Principle (SRP) SOLID Tell, don’t ask “Object Calisthenics. 9 steps to better SW design” , Jeff Bay GRASP
  27. 27. … are all smells
  28. 28. OOP? What about FP?
  29. 29. 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
  30. 30. So better if we use Agnostic Patterns
  31. 31. Domain Driven Design by Eric Evans DDD
  32. 32. “The goal of domain- driven design is to create better software by focusing on a model of the domain rather than the technology”
  33. 33. Tackling complexity DDD is a way of dealing with complexity. Complex is easy. Simple is a lot harder.
  34. 34. 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”
  35. 35. DDD leads to Model Driven Design
  36. 36. Test your model
  37. 37. Behaviour centric
  38. 38. Layered Arquitecture Diagram
  39. 39. Entities “When an object is distinguished by its identity, rather than its attributes, make this primary to its definition in the model”
  40. 40. 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…”
  41. 41. 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.”
  42. 42. Modules “Choose MODULES that tell the story of the system and contain a cohesive set of concepts.”
  43. 43. 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.”
  44. 44. 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.”
  45. 45. 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.”
  46. 46. Implementing
  47. 47. 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.
  48. 48. 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
  49. 49. Implementing Services With our Services in Grails ● Dependency Injection ● Transaction support
  50. 50. Implementing Modules Grails plug-in mechanism.
  51. 51. Implementing Aggregates Aggregates are implemented in the hasMany - belongsTo relationship. Can control the root access within a Service.
  52. 52. Implementing Factories Implement Factories classes to create new instances with an abstract interface… It could be configurable with properties.
  53. 53. Implementing Repositories Grails Service can be used to implement a dedicated Repository object that simply delegates its operation to Grails GORM.
  54. 54. All together
  55. 55. 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 …
  56. 56. In code ...
  57. 57. Model your domain, create your business logic, and separate it from your infrastructure.
  58. 58. Resources Domain 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
  59. 59. Resources http://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
  60. 60. Resources http://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 Programmer https://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 comparto http://vimeo.com/69157481 (Spanish)