Functional Domain Modeling


Published on

How to apply functional concepts in modeling domain objects. Examples are in Scala. Presented on Rubyslava / PyVo #32 (see

Published in: Technology, Business
  • Njce! Thanks for sharing.
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice slides, you cover a lot of what makes functional so powerful.
    Will your talk at Devfest be similar to this one? Esp. for domain modelling I would be interested in 1) kind of problems that lend themselves more to functional than object oriented modelling and 2) did you discover already some higher abstractions like Aggregates or Domain Services when using functional style and how would they look like?
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Functional Domain Modeling

  1. 1. Functional Domain ModelingBy /MichalBigos @teliatko
  2. 2. Agenda 1. FunctionalProgrammingGems 2. Example 3. Perception of State 4. What's Next...
  3. 3. Functional Programming Gems Pure Functions Functions w/o side-effects Theyare referentialytransparent //Definitionoffunction defsomeComputation(value:Long):Long=value+3 //Definitionasfunctionliteral valanotherComputation:(Long)=>Long=(value:Long)=>value+3
  4. 4. Functional Programming Gems Immutable Data Dataw/o shared mutable state Theycan be shared freely... no locks, no semafores etc. //Definitionofcaseobjects sealedtraitProgrammingLanguageKind caseobjectStaticextendsProgrammingLanguageKind caseobjectDynamicextendsProgrammingLanguageKind //Definitionofcaseclass caseclassProgrammingLanguage( name:String, kind:ProgrammingLanguageKind )
  5. 5. Functional Programming Gems Persistent Data Structures Always preserves the previous version of itself when itis modified Effectivelyimmutable with no in-place mutation //Supposewehavealist... vall1=List(1,2,3) //Thenweaddaitemtoit vall2=4::l assert(l1!=l2)//yieldstrue
  6. 6. Functional Programming Gems Algebraic Data Types, take one An algebraic datatype is akind of composite type, i.e. atype formed bycombiningother types Datatypes with some algebra, i.e. structure and operations Unit type, Sumtypeand Product type e.g. Lists can byformalydescribed as L = I + X * L valemptyList=List()//Unittype valsimpleList=1::emptyList//Sumtype valproductList=2::emptyList//Producttype
  7. 7. Functional Programming Gems Algebraic Data Types, take two Domain entities are represented usingproduct types caseclasses are bestfitin Scala //Tupleissimplestproducttype valprogrammingLanguage=(name,kind) //Producttypeviacaseclass caseclassProgrammingLanguage( name:String, kind:ProgrammingLanguageKind )
  8. 8. Functional Programming Gems Algebraic Data Types, take three Domain values and enums are represented usingsumtypes Inheritance and sealed traits/classes are bestfitin Scala //Exampleofsum-type,OptionfromScalalibrary(codesimplified) sealedabstractclassOption[+A] finalcaseclassSome[+A](x:A)extendsOption[A] caseobjectNoneextendsOption[Nothing] //Sumtypeviacaseobjects sealedtraitProgrammingLanguageKind caseobjectStaticextendsProgrammingLanguageKind caseobjectDynamicextendsProgrammingLanguageKind
  9. 9. Functional Programming Gems Function Composition Functions compose when theycause no side-effects Side-effects do notcompose scala>deffoo(something:String):String=s"foo($something)" scala>defbar(something:String):String=s"bar($something)" valcomposit=foo_composebar_ valreverseComposit=foo_andThenbar_ composit("anything") >foo(bar(anything)) reverseComposit("anything") >bar(foo(anything))
  10. 10. Functional Programming Gems Combinators Applyhigh-order functions Glue for chainingfunctions Pure functions as domain invariants and behavior foundmap{foundLine=>partitions.success} | | Combinator High-orderfn Monadicchaining,viafor-comprehensions for{ | _<-quantityInvariant(quantity) _<-priceInvariant(price) (found,rest)<-productAlreadyExists(productId) }yield{/*dosomething*/}
  11. 11. Functional Programming Gems Side Effects FP languages encourage isolatingside-effects from pure domain logic Known side-effects (notinsignigicant) 1. DBaccess 2. Logging 3. IO operations Example result.foreach{case(order,event)=> orderRepository.saveOrUpdate(order) }
  12. 12. Functional Programming Gems More to cover Ad-hoc polymorphism viaTypeClasses Type-Lenses functionalupdates in your domain model
  13. 13. Example
  14. 14. Perception of State Domain state is effecivelymutable Can retain previous states Historyof whathappened In-place mutation are wrongfor domain model Theycombine state with time
  15. 15. What's next Event Sourcing Everystate-modification on domain yields event Domain change is achieved byapplyingthe eventto the aggregate Eventstream, allthe events applied on domain
  16. 16. What's next CQRS Commands execute operations on domain model Queries arbitraryviews of data Independentscalability
  17. 17. What's next Memory Image and STM Application state in memory Application of events to currentstate on domain Enables transactions on memory
  18. 18. Interesting Links 1. 2. 3. 4. 5. 6. 7. 8. PureFunction(Wikipedia) Referenctial Transparency(Wikipedia) Persistent Data Structure(Wikipedia) Presistend Data Structures and Managed References ADT, Product and SumTypes (Bob Harper, PFPL) SimpleMadeEasy(RichHickey) FunctionComposition(Wikipedia) Building Apps w/ Functional DomainModels (Debasish Ghosh)
  19. 19. Thanks for your attention