SlideShare uma empresa Scribd logo
1 de 39
Baixar para ler offline
Debasish Ghosh, Sep 22, 2022
Functional Domain Modeling
The ZIO 2 way
What is a domain model ?
A domain model in problem solving and software engineering is a
conceptual model of all the topics related to a speci
fi
c problem. It
describes the various entities, their attributes, roles, and relationships,
plus the constraints that govern the problem domain. It does not
describe the solutions to the problem.
Wikipedia (http://en.wikipedia.org/wiki/Domain_model)
https://msdn.microsoft.com/en-us/library/jj591560.aspx
A Pattern Language
for domain modeling
A Pattern Language
for domain modeling
•Common vocabulary
A Pattern Language
for domain modeling
•Common vocabulary
•Modularization
• Nouns in the model
• Pure objects
• Names consistent with domain vocabulary
• Strongly typed (newtypes, refinement types)
• Provides object storage and access
• Abstractions with Effects
• Names consistent with the aggregate root that gets stored
• Presents clients with simple interfaces to manage persistence
• Decouples model design from persistence technology
• Provides object storage and access
• Abstractions with Effects
• Names consistent with the aggregate root that gets stored
• Presents clients with simple interfaces to manage persistence
• Decouples model design from persistence technology
Client code ignores repository implementation, developers do not
- Eric Evans in the DDD blue book
• Coarse grained abstractions
• Relates to domain concept not specific to a single entity
• Interface defined in terms of other elements of the model
• Abstractions with Effects
A Pattern Language
for domain modeling
final case class Account private (
no: AccountNo,
name: AccountName,
dateOfOpen: ZonedDateTime,
//..
)
Model definition of an entity as an algebraic data type
• Types with names derived from domain
vocabulary
• Rich semantics with newtypes,
re
fi
nement types etc. from zio-prelude
final case class Account private (
no: AccountNo,
name: AccountName,
dateOfOpen: ZonedDateTime,
//..
)
object Account {
def tradingAccount(
no: String,
name: String,
openDate: Option[ZonedDateTime],
//..
): Validation[String, Account] = { //..
private[model] def validateAccountNo(
no: String): Validation[String, AccountNo] = //..
//..
def close(
a: Account,
closeDate: ZonedDateTime
): Validation[String, Account] = { //..
}
Model definition of an entity as an algebraic data type A Module for the model containing definitions for
smart constructors, validations, and other domain
logic for the model
• Types with names derived from domain
vocabulary
• Rich semantics with newtypes,
re
fi
nement types etc. from zio-prelude
• Abstractions that compose
• Build larger abstractions - validations
for entire domain entity as a
composition of smaller validations
• Supported by zio-prelude
• Smart constructor
• Domain behaviour
final case class Account private (
no: AccountNo,
name: AccountName,
dateOfOpen: ZonedDateTime,
//..
)
object Account {
def tradingAccount(
no: String,
name: String,
openDate: Option[ZonedDateTime],
//..
): Validation[String, Account] = { //..
private[model] def validateAccountNo(
no: String): Validation[String, AccountNo] = //..
//..
def close(
a: Account,
closeDate: ZonedDateTime
): Validation[String, Account] = { //..
}
Model definition of an entity as an algebraic data type A Module for the model containing definitions for
smart constructors, validations, and other domain
logic for the model
• Types with names derived from domain
vocabulary
• Rich semantics with newtypes,
re
fi
nement types etc. from zio-prelude
• Abstractions that compose
• Build larger abstractions - validations
for entire domain entity as a
composition of smaller validations
• Supported by zio-prelude
• Smart constructor
• Domain behaviour
•Pure model
•Functional core
•Compositional with zio-prelude
Repository
trait AccountRepository {
/** query by account number */
def queryByAccountNo(no: AccountNo): Task[Option[Account]]
/** store */
def store(a: Account): Task[Account]
/** store many */
def store(as: List[Account]): Task[Unit]
/** query by opened date */
def allOpenedOn(openedOnDate: LocalDate): Task[List[Account]]
//..
}
Repository
trait AccountRepository {
/** query by account number */
def queryByAccountNo(no: AccountNo): Task[Option[Account]]
/** store */
def store(a: Account): Task[Account]
/** store many */
def store(as: List[Account]): Task[Unit]
/** query by opened date */
def allOpenedOn(openedOnDate: LocalDate): Task[List[Account]]
//..
}
• Effectful contract
• Concrete ZIO effect (unlike tagless
fi
nal)
• Less parametric than the tagless
fi
nal approach
• No dependency on any speci
fi
c environment
• zio.Task[A] => ZIO[Any,Throwable,A]
Repository - Implementations
final case class AccountRepositoryLive(
xaResource: Resource[Task, Transactor[Task]])
extends AccountRepository {
import AccountRepositoryLive.SQL
def all: Task[List[Account]] =
xaResource.use { xa =>
SQL.getAll
.to[List]
.transact(xa)
.orDie
}
def queryByAccountNo(no: AccountNo): Task[Option[Account]] =
xaResource.use { xa =>
SQL
.get(no.value.value)
.option
.transact(xa)
.orDie
}
//..
}
• Implementation
details
• Functional core
Repository - Implementations
final case class AccountRepositoryLive(
xaResource: Resource[Task, Transactor[Task]])
extends AccountRepository {
import AccountRepositoryLive.SQL
def all: Task[List[Account]] =
xaResource.use { xa =>
SQL.getAll
.to[List]
.transact(xa)
.orDie
}
def queryByAccountNo(no: AccountNo): Task[Option[Account]] =
xaResource.use { xa =>
SQL
.get(no.value.value)
.option
.transact(xa)
.orDie
}
//..
}
object AccountRepositoryLive extends CatzInterop {
val layer: ZLayer[DBConfig, Throwable, AccountRepository] = {
ZLayer
.scoped(for {
cfg <- ZIO.service[DBConfig]
transactor <- mkTransactor(cfg)
} yield new AccountRepositoryLive(transactor))
}
//..
}
• Implementation
details
• Functional core
• ZLayer as a recipe for creating ZIO services from dependencies
• ZLayer serves as the factory for creating domain artifacts
• The scoped API ensures that resource lifetimes are properly
managed
• Inject the proper layer as dependency
REPOSITORIES have many advantages, including the following:
• They present clients with a simple model for obtaining persistent
objects and managing their life cycle.
• They decouple application and domain design from persistence
technology, multiple database strategies, or even multiple data
sources
• They communicate design decisions about object access
• They allow easy substitution of dummy implementation, for use in
testing (typically using an in-memory allocation)
- Eric Evans (The Blue DDD book)
REPOSITORIES have many advantages, including the following:
• They present clients with a simple model for obtaining persistent
objects and managing their life cycle.
• They decouple application and domain design from persistence
technology, multiple database strategies, or even multiple data
sources
• They communicate design decisions about object access
• They allow easy substitution of dummy implementation, for use in
testing (typically using an in-memory allocation)
- Eric Evans (The Blue DDD book)
Single interface principle with return types
that abstract the value as well as handling of
failures through zio effects
REPOSITORIES have many advantages, including the following:
• They present clients with a simple model for obtaining persistent
objects and managing their life cycle.
• They decouple application and domain design from persistence
technology, multiple database strategies, or even multiple data
sources
• They communicate design decisions about object access
• They allow easy substitution of dummy implementation, for use in
testing (typically using an in-memory allocation)
- Eric Evans (The Blue DDD book)
Single interface principle with return types
that abstract the value as well as handling of
failures through zio effects
Allow multiple implementations (the OO way)
that can be injected dynamically and
compositionally through ZLayers
REPOSITORIES have many advantages, including the following:
• They present clients with a simple model for obtaining persistent
objects and managing their life cycle.
• They decouple application and domain design from persistence
technology, multiple database strategies, or even multiple data
sources
• They communicate design decisions about object access
• They allow easy substitution of dummy implementation, for use in
testing (typically using an in-memory allocation)
- Eric Evans (The Blue DDD book)
Single interface principle with return types
that abstract the value as well as handling of
failures through zio effects
Allow multiple implementations (the OO way)
that can be injected dynamically and
compositionally through ZLayers
The concrete zio effect that gets returned
clearly indicates semantics of failure handling,
environment dependencies (if any) and the
exception types
REPOSITORIES have many advantages, including the following:
• They present clients with a simple model for obtaining persistent
objects and managing their life cycle.
• They decouple application and domain design from persistence
technology, multiple database strategies, or even multiple data
sources
• They communicate design decisions about object access
• They allow easy substitution of dummy implementation, for use in
testing (typically using an in-memory allocation)
- Eric Evans (The Blue DDD book)
Single interface principle with return types
that abstract the value as well as handling of
failures through zio effects
Allow multiple implementations (the OO way)
that can be injected dynamically and
compositionally through ZLayers
The concrete zio effect that gets returned
clearly indicates semantics of failure handling,
environment dependencies (if any) and the
exception types
ZLayers! ZLayers!
Services
trait TradingService {
def getAccountsOpenedOn(openDate: LocalDate): IO[TradingError, List[Account]]
def getTrades(forAccountNo: AccountNo,
forDate: Option[LocalDate] = None
): IO[TradingError, List[Trade]]
def getTradesByISINCodes(forDate: LocalDate,
forIsins: Set[model.instrument.ISINCode]
): IO[TradingError, List[Trade]]
def orders(frontOfficeOrders: NonEmptyList[FrontOfficeOrder]
): IO[TradingError, NonEmptyList[Order]]
def execute(orders: NonEmptyList[Order],
market: Market,
brokerAccountNo: AccountNo
): IO[TradingError, NonEmptyList[Execution]]
def allocate(executions: NonEmptyList[Execution],
clientAccounts: NonEmptyList[AccountNo],
userId: UserId
): IO[TradingError, NonEmptyList[Trade]]
}
• Effectful contracts
• Concrete ZIO effect (unlike tagless
fi
nal) - clearly publishes
all exceptions and returned value types
• Less parametric than the tagless
fi
nal approach
• No dependency on any speci
fi
c environment
• zio.IO[A] => ZIO[Any, E, A]
• Coarse grained abstractions - not speci
fi
c to any entity
• All names derived from domain vocabulary
Services
trait TradingService {
def getAccountsOpenedOn(openDate: LocalDate): IO[TradingError, List[Account]]
def getTrades(forAccountNo: AccountNo,
forDate: Option[LocalDate] = None
): IO[TradingError, List[Trade]]
def getTradesByISINCodes(forDate: LocalDate,
forIsins: Set[model.instrument.ISINCode]
): IO[TradingError, List[Trade]]
def orders(frontOfficeOrders: NonEmptyList[FrontOfficeOrder]
): IO[TradingError, NonEmptyList[Order]]
def execute(orders: NonEmptyList[Order],
market: Market,
brokerAccountNo: AccountNo
): IO[TradingError, NonEmptyList[Execution]]
def allocate(executions: NonEmptyList[Execution],
clientAccounts: NonEmptyList[AccountNo],
userId: UserId
): IO[TradingError, NonEmptyList[Trade]]
}
Services
trait TradingService {
def getAccountsOpenedOn(openDate: LocalDate): IO[TradingError, List[Account]]
def getTrades(forAccountNo: AccountNo,
forDate: Option[LocalDate] = None
): IO[TradingError, List[Trade]]
def getTradesByISINCodes(forDate: LocalDate,
forIsins: Set[model.instrument.ISINCode]
): IO[TradingError, List[Trade]]
def orders(frontOfficeOrders: NonEmptyList[FrontOfficeOrder]
): IO[TradingError, NonEmptyList[Order]]
def execute(orders: NonEmptyList[Order],
market: Market,
brokerAccountNo: AccountNo
): IO[TradingError, NonEmptyList[Execution]]
def allocate(executions: NonEmptyList[Execution],
clientAccounts: NonEmptyList[AccountNo],
userId: UserId
): IO[TradingError, NonEmptyList[Trade]]
}
• Effectful contracts
• Concrete ZIO effect (unlike tagless
fi
nal) - clearly publishes
all exceptions and returned value types
• Less parametric than the tagless
fi
nal approach
• No dependency on any speci
fi
c environment
• zio.IO[A] => ZIO[Any, E, A]
• Coarse grained abstractions - not speci
fi
c to any entity
• All names derived from domain vocabulary
Services - Functional Core
final def generateTrade(
frontOfficeInput: GenerateTradeFrontOfficeInput,
userId: UserId
): IO[Throwable, NonEmptyList[Trade]] = {
for {
orders <- orders(frontOfficeInput.frontOfficeOrders)
executions <- execute(
orders,
frontOfficeInput.market,
frontOfficeInput.brokerAccountNo
)
trades <- allocate(executions, frontOfficeInput.clientAccountNos, userId)
} yield trades
}
Services - Implementation
final case class TradingServiceLive(
ar: AccountRepository,
or: OrderRepository,
er: ExecutionRepository,
tr: TradeRepository
) extends TradingService { //..
object TradingServiceLive {
val layer: ZLayer[
AccountRepository with OrderRepository with
ExecutionRepository with TradeRepository,
Nothing,
TradingServiceLive
] = ZLayer.fromFunction(TradingServiceLive.apply _)
}
• Dependencies passed as constructor arguments
• And automatically becomes part of the ZLayer and hence
part of the dependency graph
• Dependencies are interfaces only and not implementations
Services
Wiring up ..
ZIO
.serviceWithZIO[TradingService](service => //..)
.provide(
AccountRepositoryLive.layer,
TradingServiceLive.layer,
OrderRepositoryLive.layer,
TradeRepositoryLive.layer,
ExecutionRepositoryLive.layer,
config.live
)
Wiring up ..
ZIO
.serviceWithZIO[TradingService](service => //..)
.provide(
AccountRepositoryLive.layer,
TradingServiceLive.layer,
OrderRepositoryLive.layer,
TradeRepositoryLive.layer,
ExecutionRepositoryLive.layer,
config.live
)
• Declarative way to build an instance of your domain
service from the speci
fi
ed dependencies
• ZLayers
- Allow automatic construction of the dependency
graph of your domain model
- Are composable
- Are asynchronous and non-blocking
- Can be acquired in parallel unlike class constructors
Entities
Value Objects
<<pure>>
• pure
• functional
• statically typed
• use zio-prelude for
newtypes,
re
fi
nement types,
validations etc.
Entities
Value Objects
<<pure>> Repositories
<<interfaces>>
Implementations
• pure
• functional
• statically typed
• use zio-prelude for
newtypes,
re
fi
nement types,
validations etc.
• plain old scala traits
• one per aggregate root
• effectual contracts
• concrete zio effects
• persistence platform dependent
• pass dependencies as constructor arguments
• can be multiple for a single contract
Entities
Value Objects
<<pure>> Repositories
<<interfaces>>
Implementations
Domain
Services
Domain
Service
Implementations
<<interfaces>>
• pure
• functional
• statically typed
• use zio-prelude for
newtypes,
re
fi
nement types,
validations etc.
• plain old scala traits
• one per aggregate root
• effectual contracts
• concrete zio effects
• persistence platform dependent
• pass dependencies as constructor arguments
• can be multiple for a single contract
• coarse grained
• can interact with multiple domain entities
• effectual contracts
• concrete zio effects
• pass dependencies as constructor
arguments
• depends only on repository interfaces and
NOT implementations
Entities
Value Objects
<<pure>> Repositories
<<interfaces>>
Implementations
Domain
Services
Domain
Service
Implementations
<<interfaces>>
Application
• pure
• functional
• statically typed
• use zio-prelude for
newtypes,
re
fi
nement types,
validations etc.
• plain old scala traits
• one per aggregate root
• effectual contracts
• concrete zio effects
• persistence platform dependent
• pass dependencies as constructor arguments
• can be multiple for a single contract
• coarse grained
• can interact with multiple domain entities
• effectual contracts
• concrete zio effects
• pass dependencies as constructor
arguments
• depends only on repository interfaces and
NOT implementations
• ZLayer magic!
Entities
Value Objects
<<pure>> Repositories
<<interfaces>>
Implementations
Domain
Services
Domain
Service
Implementations
<<interfaces>>
Application
• pure
• functional
• statically typed
• use zio-prelude for
newtypes,
re
fi
nement types,
validations etc.
• plain old scala traits
• one per aggregate root
• effectual contracts
• concrete zio effects
• persistence platform dependent
• pass dependencies as constructor arguments
• can be multiple for a single contract
• coarse grained
• can interact with multiple domain entities
• effectual contracts
• concrete zio effects
• pass dependencies as constructor
arguments
• depends only on repository interfaces and
NOT implementations
• ZLayer magic!
Questions ?

Mais conteúdo relacionado

Mais procurados

Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaKnoldus Inc.
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...Philip Schwarz
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of CompositionScott Wlaschin
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldPhilip Schwarz
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Philip Schwarz
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to ScalaTim Underwood
 
Functional and Algebraic Domain Modeling
Functional and Algebraic Domain ModelingFunctional and Algebraic Domain Modeling
Functional and Algebraic Domain ModelingDebasish Ghosh
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed worldDebasish Ghosh
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented ProgrammingScott Wlaschin
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaJorge Vásquez
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?FITC
 
Clean architecture
Clean architectureClean architecture
Clean architectureandbed
 
Functional Patterns in Domain Modeling
Functional Patterns in Domain ModelingFunctional Patterns in Domain Modeling
Functional Patterns in Domain ModelingDebasish Ghosh
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldJorge Vásquez
 
Quill vs Slick Smackdown
Quill vs Slick SmackdownQuill vs Slick Smackdown
Quill vs Slick SmackdownAlexander Ioffe
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean ArchitectureMattia Battiston
 
Parboiled explained
Parboiled explainedParboiled explained
Parboiled explainedPaul Popoff
 

Mais procurados (20)

ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
 
Functors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In ScalaFunctors, Applicatives and Monads In Scala
Functors, Applicatives and Monads In Scala
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of Composition
 
The Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and FoldThe Functional Programming Triad of Map, Filter and Fold
The Functional Programming Triad of Map, Filter and Fold
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
Functional and Algebraic Domain Modeling
Functional and Algebraic Domain ModelingFunctional and Algebraic Domain Modeling
Functional and Algebraic Domain Modeling
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented Programming
 
Zio in real world
Zio in real worldZio in real world
Zio in real world
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Functional Patterns in Domain Modeling
Functional Patterns in Domain ModelingFunctional Patterns in Domain Modeling
Functional Patterns in Domain Modeling
 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorld
 
Quill vs Slick Smackdown
Quill vs Slick SmackdownQuill vs Slick Smackdown
Quill vs Slick Smackdown
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Parboiled explained
Parboiled explainedParboiled explained
Parboiled explained
 

Semelhante a Functional Domain Modeling - The ZIO 2 Way

ZZ BC#7.5 asp.net mvc practice and guideline refresh!
ZZ BC#7.5 asp.net mvc practice  and guideline refresh! ZZ BC#7.5 asp.net mvc practice  and guideline refresh!
ZZ BC#7.5 asp.net mvc practice and guideline refresh! Chalermpon Areepong
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patternsamitarcade
 
Software design with Domain-driven design
Software design with Domain-driven design Software design with Domain-driven design
Software design with Domain-driven design Allan Mangune
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven DesignRyan Riley
 
Most Useful Design Patterns
Most Useful Design PatternsMost Useful Design Patterns
Most Useful Design PatternsSteven Smith
 
Agile Secure Cloud Application Development Management
Agile Secure Cloud Application Development ManagementAgile Secure Cloud Application Development Management
Agile Secure Cloud Application Development ManagementAdam Getchell
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?Guillaume AGIS
 
Sterling for Windows Phone 7
Sterling for Windows Phone 7Sterling for Windows Phone 7
Sterling for Windows Phone 7Jeremy Likness
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2Julie Iskander
 
ZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven DesignZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven DesignBradley Holt
 
JavaOne 2014 - Supporting Multi-tenancy Applications with Java EE
JavaOne 2014 - Supporting Multi-tenancy Applications with Java EEJavaOne 2014 - Supporting Multi-tenancy Applications with Java EE
JavaOne 2014 - Supporting Multi-tenancy Applications with Java EERodrigo Cândido da Silva
 
Kelis king - introduction to software design
Kelis king -  introduction to software designKelis king -  introduction to software design
Kelis king - introduction to software designKelisKing
 
An Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHPAn Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHPChris Renner
 
Application Architecture April 2014
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014Lars-Erik Kindblad
 

Semelhante a Functional Domain Modeling - The ZIO 2 Way (20)

ZZ BC#7.5 asp.net mvc practice and guideline refresh!
ZZ BC#7.5 asp.net mvc practice  and guideline refresh! ZZ BC#7.5 asp.net mvc practice  and guideline refresh!
ZZ BC#7.5 asp.net mvc practice and guideline refresh!
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
 
Software design with Domain-driven design
Software design with Domain-driven design Software design with Domain-driven design
Software design with Domain-driven design
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Design pattern
Design patternDesign pattern
Design pattern
 
Most Useful Design Patterns
Most Useful Design PatternsMost Useful Design Patterns
Most Useful Design Patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
Agile Secure Cloud Application Development Management
Agile Secure Cloud Application Development ManagementAgile Secure Cloud Application Development Management
Agile Secure Cloud Application Development Management
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?
 
Sterling for Windows Phone 7
Sterling for Windows Phone 7Sterling for Windows Phone 7
Sterling for Windows Phone 7
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
ZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven DesignZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven Design
 
JavaOne 2014 - Supporting Multi-tenancy Applications with Java EE
JavaOne 2014 - Supporting Multi-tenancy Applications with Java EEJavaOne 2014 - Supporting Multi-tenancy Applications with Java EE
JavaOne 2014 - Supporting Multi-tenancy Applications with Java EE
 
Kelis king - introduction to software design
Kelis king -  introduction to software designKelis king -  introduction to software design
Kelis king - introduction to software design
 
An Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHPAn Introduction to Domain Driven Design in PHP
An Introduction to Domain Driven Design in PHP
 
Application Architecture April 2014
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014
 
Unit 2
Unit 2Unit 2
Unit 2
 

Mais de Debasish Ghosh

Approximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsApproximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsDebasish Ghosh
 
Architectural Patterns in Building Modular Domain Models
Architectural Patterns in Building Modular Domain ModelsArchitectural Patterns in Building Modular Domain Models
Architectural Patterns in Building Modular Domain ModelsDebasish Ghosh
 
Mining Functional Patterns
Mining Functional PatternsMining Functional Patterns
Mining Functional PatternsDebasish Ghosh
 
An Algebraic Approach to Functional Domain Modeling
An Algebraic Approach to Functional Domain ModelingAn Algebraic Approach to Functional Domain Modeling
An Algebraic Approach to Functional Domain ModelingDebasish Ghosh
 
Functional and Algebraic Domain Modeling
Functional and Algebraic Domain ModelingFunctional and Algebraic Domain Modeling
Functional and Algebraic Domain ModelingDebasish Ghosh
 
From functional to Reactive - patterns in domain modeling
From functional to Reactive - patterns in domain modelingFrom functional to Reactive - patterns in domain modeling
From functional to Reactive - patterns in domain modelingDebasish Ghosh
 
Domain Modeling with Functions - an algebraic approach
Domain Modeling with Functions - an algebraic approachDomain Modeling with Functions - an algebraic approach
Domain Modeling with Functions - an algebraic approachDebasish Ghosh
 
Property based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesProperty based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesDebasish Ghosh
 
Big Data - architectural concerns for the new age
Big Data - architectural concerns for the new ageBig Data - architectural concerns for the new age
Big Data - architectural concerns for the new ageDebasish Ghosh
 
Functional and Event Driven - another approach to domain modeling
Functional and Event Driven - another approach to domain modelingFunctional and Event Driven - another approach to domain modeling
Functional and Event Driven - another approach to domain modelingDebasish Ghosh
 
DSL - expressive syntax on top of a clean semantic model
DSL - expressive syntax on top of a clean semantic modelDSL - expressive syntax on top of a clean semantic model
DSL - expressive syntax on top of a clean semantic modelDebasish Ghosh
 
Dependency Injection in Scala - Beyond the Cake Pattern
Dependency Injection in Scala - Beyond the Cake PatternDependency Injection in Scala - Beyond the Cake Pattern
Dependency Injection in Scala - Beyond the Cake PatternDebasish Ghosh
 

Mais de Debasish Ghosh (12)

Approximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsApproximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming Applications
 
Architectural Patterns in Building Modular Domain Models
Architectural Patterns in Building Modular Domain ModelsArchitectural Patterns in Building Modular Domain Models
Architectural Patterns in Building Modular Domain Models
 
Mining Functional Patterns
Mining Functional PatternsMining Functional Patterns
Mining Functional Patterns
 
An Algebraic Approach to Functional Domain Modeling
An Algebraic Approach to Functional Domain ModelingAn Algebraic Approach to Functional Domain Modeling
An Algebraic Approach to Functional Domain Modeling
 
Functional and Algebraic Domain Modeling
Functional and Algebraic Domain ModelingFunctional and Algebraic Domain Modeling
Functional and Algebraic Domain Modeling
 
From functional to Reactive - patterns in domain modeling
From functional to Reactive - patterns in domain modelingFrom functional to Reactive - patterns in domain modeling
From functional to Reactive - patterns in domain modeling
 
Domain Modeling with Functions - an algebraic approach
Domain Modeling with Functions - an algebraic approachDomain Modeling with Functions - an algebraic approach
Domain Modeling with Functions - an algebraic approach
 
Property based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesProperty based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rules
 
Big Data - architectural concerns for the new age
Big Data - architectural concerns for the new ageBig Data - architectural concerns for the new age
Big Data - architectural concerns for the new age
 
Functional and Event Driven - another approach to domain modeling
Functional and Event Driven - another approach to domain modelingFunctional and Event Driven - another approach to domain modeling
Functional and Event Driven - another approach to domain modeling
 
DSL - expressive syntax on top of a clean semantic model
DSL - expressive syntax on top of a clean semantic modelDSL - expressive syntax on top of a clean semantic model
DSL - expressive syntax on top of a clean semantic model
 
Dependency Injection in Scala - Beyond the Cake Pattern
Dependency Injection in Scala - Beyond the Cake PatternDependency Injection in Scala - Beyond the Cake Pattern
Dependency Injection in Scala - Beyond the Cake Pattern
 

Último

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 

Último (20)

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

Functional Domain Modeling - The ZIO 2 Way

  • 1. Debasish Ghosh, Sep 22, 2022 Functional Domain Modeling The ZIO 2 way
  • 2. What is a domain model ? A domain model in problem solving and software engineering is a conceptual model of all the topics related to a speci fi c problem. It describes the various entities, their attributes, roles, and relationships, plus the constraints that govern the problem domain. It does not describe the solutions to the problem. Wikipedia (http://en.wikipedia.org/wiki/Domain_model)
  • 4.
  • 5. A Pattern Language for domain modeling
  • 6. A Pattern Language for domain modeling •Common vocabulary
  • 7. A Pattern Language for domain modeling •Common vocabulary •Modularization
  • 8. • Nouns in the model • Pure objects • Names consistent with domain vocabulary • Strongly typed (newtypes, refinement types)
  • 9. • Provides object storage and access • Abstractions with Effects • Names consistent with the aggregate root that gets stored • Presents clients with simple interfaces to manage persistence • Decouples model design from persistence technology
  • 10. • Provides object storage and access • Abstractions with Effects • Names consistent with the aggregate root that gets stored • Presents clients with simple interfaces to manage persistence • Decouples model design from persistence technology Client code ignores repository implementation, developers do not - Eric Evans in the DDD blue book
  • 11. • Coarse grained abstractions • Relates to domain concept not specific to a single entity • Interface defined in terms of other elements of the model • Abstractions with Effects
  • 12. A Pattern Language for domain modeling
  • 13. final case class Account private ( no: AccountNo, name: AccountName, dateOfOpen: ZonedDateTime, //.. ) Model definition of an entity as an algebraic data type • Types with names derived from domain vocabulary • Rich semantics with newtypes, re fi nement types etc. from zio-prelude
  • 14. final case class Account private ( no: AccountNo, name: AccountName, dateOfOpen: ZonedDateTime, //.. ) object Account { def tradingAccount( no: String, name: String, openDate: Option[ZonedDateTime], //.. ): Validation[String, Account] = { //.. private[model] def validateAccountNo( no: String): Validation[String, AccountNo] = //.. //.. def close( a: Account, closeDate: ZonedDateTime ): Validation[String, Account] = { //.. } Model definition of an entity as an algebraic data type A Module for the model containing definitions for smart constructors, validations, and other domain logic for the model • Types with names derived from domain vocabulary • Rich semantics with newtypes, re fi nement types etc. from zio-prelude • Abstractions that compose • Build larger abstractions - validations for entire domain entity as a composition of smaller validations • Supported by zio-prelude • Smart constructor • Domain behaviour
  • 15. final case class Account private ( no: AccountNo, name: AccountName, dateOfOpen: ZonedDateTime, //.. ) object Account { def tradingAccount( no: String, name: String, openDate: Option[ZonedDateTime], //.. ): Validation[String, Account] = { //.. private[model] def validateAccountNo( no: String): Validation[String, AccountNo] = //.. //.. def close( a: Account, closeDate: ZonedDateTime ): Validation[String, Account] = { //.. } Model definition of an entity as an algebraic data type A Module for the model containing definitions for smart constructors, validations, and other domain logic for the model • Types with names derived from domain vocabulary • Rich semantics with newtypes, re fi nement types etc. from zio-prelude • Abstractions that compose • Build larger abstractions - validations for entire domain entity as a composition of smaller validations • Supported by zio-prelude • Smart constructor • Domain behaviour •Pure model •Functional core •Compositional with zio-prelude
  • 16. Repository trait AccountRepository { /** query by account number */ def queryByAccountNo(no: AccountNo): Task[Option[Account]] /** store */ def store(a: Account): Task[Account] /** store many */ def store(as: List[Account]): Task[Unit] /** query by opened date */ def allOpenedOn(openedOnDate: LocalDate): Task[List[Account]] //.. }
  • 17. Repository trait AccountRepository { /** query by account number */ def queryByAccountNo(no: AccountNo): Task[Option[Account]] /** store */ def store(a: Account): Task[Account] /** store many */ def store(as: List[Account]): Task[Unit] /** query by opened date */ def allOpenedOn(openedOnDate: LocalDate): Task[List[Account]] //.. } • Effectful contract • Concrete ZIO effect (unlike tagless fi nal) • Less parametric than the tagless fi nal approach • No dependency on any speci fi c environment • zio.Task[A] => ZIO[Any,Throwable,A]
  • 18. Repository - Implementations final case class AccountRepositoryLive( xaResource: Resource[Task, Transactor[Task]]) extends AccountRepository { import AccountRepositoryLive.SQL def all: Task[List[Account]] = xaResource.use { xa => SQL.getAll .to[List] .transact(xa) .orDie } def queryByAccountNo(no: AccountNo): Task[Option[Account]] = xaResource.use { xa => SQL .get(no.value.value) .option .transact(xa) .orDie } //.. } • Implementation details • Functional core
  • 19. Repository - Implementations final case class AccountRepositoryLive( xaResource: Resource[Task, Transactor[Task]]) extends AccountRepository { import AccountRepositoryLive.SQL def all: Task[List[Account]] = xaResource.use { xa => SQL.getAll .to[List] .transact(xa) .orDie } def queryByAccountNo(no: AccountNo): Task[Option[Account]] = xaResource.use { xa => SQL .get(no.value.value) .option .transact(xa) .orDie } //.. } object AccountRepositoryLive extends CatzInterop { val layer: ZLayer[DBConfig, Throwable, AccountRepository] = { ZLayer .scoped(for { cfg <- ZIO.service[DBConfig] transactor <- mkTransactor(cfg) } yield new AccountRepositoryLive(transactor)) } //.. } • Implementation details • Functional core • ZLayer as a recipe for creating ZIO services from dependencies • ZLayer serves as the factory for creating domain artifacts • The scoped API ensures that resource lifetimes are properly managed • Inject the proper layer as dependency
  • 20. REPOSITORIES have many advantages, including the following: • They present clients with a simple model for obtaining persistent objects and managing their life cycle. • They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources • They communicate design decisions about object access • They allow easy substitution of dummy implementation, for use in testing (typically using an in-memory allocation) - Eric Evans (The Blue DDD book)
  • 21. REPOSITORIES have many advantages, including the following: • They present clients with a simple model for obtaining persistent objects and managing their life cycle. • They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources • They communicate design decisions about object access • They allow easy substitution of dummy implementation, for use in testing (typically using an in-memory allocation) - Eric Evans (The Blue DDD book) Single interface principle with return types that abstract the value as well as handling of failures through zio effects
  • 22. REPOSITORIES have many advantages, including the following: • They present clients with a simple model for obtaining persistent objects and managing their life cycle. • They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources • They communicate design decisions about object access • They allow easy substitution of dummy implementation, for use in testing (typically using an in-memory allocation) - Eric Evans (The Blue DDD book) Single interface principle with return types that abstract the value as well as handling of failures through zio effects Allow multiple implementations (the OO way) that can be injected dynamically and compositionally through ZLayers
  • 23. REPOSITORIES have many advantages, including the following: • They present clients with a simple model for obtaining persistent objects and managing their life cycle. • They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources • They communicate design decisions about object access • They allow easy substitution of dummy implementation, for use in testing (typically using an in-memory allocation) - Eric Evans (The Blue DDD book) Single interface principle with return types that abstract the value as well as handling of failures through zio effects Allow multiple implementations (the OO way) that can be injected dynamically and compositionally through ZLayers The concrete zio effect that gets returned clearly indicates semantics of failure handling, environment dependencies (if any) and the exception types
  • 24. REPOSITORIES have many advantages, including the following: • They present clients with a simple model for obtaining persistent objects and managing their life cycle. • They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources • They communicate design decisions about object access • They allow easy substitution of dummy implementation, for use in testing (typically using an in-memory allocation) - Eric Evans (The Blue DDD book) Single interface principle with return types that abstract the value as well as handling of failures through zio effects Allow multiple implementations (the OO way) that can be injected dynamically and compositionally through ZLayers The concrete zio effect that gets returned clearly indicates semantics of failure handling, environment dependencies (if any) and the exception types ZLayers! ZLayers!
  • 25.
  • 26. Services trait TradingService { def getAccountsOpenedOn(openDate: LocalDate): IO[TradingError, List[Account]] def getTrades(forAccountNo: AccountNo, forDate: Option[LocalDate] = None ): IO[TradingError, List[Trade]] def getTradesByISINCodes(forDate: LocalDate, forIsins: Set[model.instrument.ISINCode] ): IO[TradingError, List[Trade]] def orders(frontOfficeOrders: NonEmptyList[FrontOfficeOrder] ): IO[TradingError, NonEmptyList[Order]] def execute(orders: NonEmptyList[Order], market: Market, brokerAccountNo: AccountNo ): IO[TradingError, NonEmptyList[Execution]] def allocate(executions: NonEmptyList[Execution], clientAccounts: NonEmptyList[AccountNo], userId: UserId ): IO[TradingError, NonEmptyList[Trade]] } • Effectful contracts • Concrete ZIO effect (unlike tagless fi nal) - clearly publishes all exceptions and returned value types • Less parametric than the tagless fi nal approach • No dependency on any speci fi c environment • zio.IO[A] => ZIO[Any, E, A] • Coarse grained abstractions - not speci fi c to any entity • All names derived from domain vocabulary
  • 27. Services trait TradingService { def getAccountsOpenedOn(openDate: LocalDate): IO[TradingError, List[Account]] def getTrades(forAccountNo: AccountNo, forDate: Option[LocalDate] = None ): IO[TradingError, List[Trade]] def getTradesByISINCodes(forDate: LocalDate, forIsins: Set[model.instrument.ISINCode] ): IO[TradingError, List[Trade]] def orders(frontOfficeOrders: NonEmptyList[FrontOfficeOrder] ): IO[TradingError, NonEmptyList[Order]] def execute(orders: NonEmptyList[Order], market: Market, brokerAccountNo: AccountNo ): IO[TradingError, NonEmptyList[Execution]] def allocate(executions: NonEmptyList[Execution], clientAccounts: NonEmptyList[AccountNo], userId: UserId ): IO[TradingError, NonEmptyList[Trade]] }
  • 28. Services trait TradingService { def getAccountsOpenedOn(openDate: LocalDate): IO[TradingError, List[Account]] def getTrades(forAccountNo: AccountNo, forDate: Option[LocalDate] = None ): IO[TradingError, List[Trade]] def getTradesByISINCodes(forDate: LocalDate, forIsins: Set[model.instrument.ISINCode] ): IO[TradingError, List[Trade]] def orders(frontOfficeOrders: NonEmptyList[FrontOfficeOrder] ): IO[TradingError, NonEmptyList[Order]] def execute(orders: NonEmptyList[Order], market: Market, brokerAccountNo: AccountNo ): IO[TradingError, NonEmptyList[Execution]] def allocate(executions: NonEmptyList[Execution], clientAccounts: NonEmptyList[AccountNo], userId: UserId ): IO[TradingError, NonEmptyList[Trade]] } • Effectful contracts • Concrete ZIO effect (unlike tagless fi nal) - clearly publishes all exceptions and returned value types • Less parametric than the tagless fi nal approach • No dependency on any speci fi c environment • zio.IO[A] => ZIO[Any, E, A] • Coarse grained abstractions - not speci fi c to any entity • All names derived from domain vocabulary
  • 29. Services - Functional Core final def generateTrade( frontOfficeInput: GenerateTradeFrontOfficeInput, userId: UserId ): IO[Throwable, NonEmptyList[Trade]] = { for { orders <- orders(frontOfficeInput.frontOfficeOrders) executions <- execute( orders, frontOfficeInput.market, frontOfficeInput.brokerAccountNo ) trades <- allocate(executions, frontOfficeInput.clientAccountNos, userId) } yield trades }
  • 30. Services - Implementation final case class TradingServiceLive( ar: AccountRepository, or: OrderRepository, er: ExecutionRepository, tr: TradeRepository ) extends TradingService { //.. object TradingServiceLive { val layer: ZLayer[ AccountRepository with OrderRepository with ExecutionRepository with TradeRepository, Nothing, TradingServiceLive ] = ZLayer.fromFunction(TradingServiceLive.apply _) } • Dependencies passed as constructor arguments • And automatically becomes part of the ZLayer and hence part of the dependency graph • Dependencies are interfaces only and not implementations
  • 32. Wiring up .. ZIO .serviceWithZIO[TradingService](service => //..) .provide( AccountRepositoryLive.layer, TradingServiceLive.layer, OrderRepositoryLive.layer, TradeRepositoryLive.layer, ExecutionRepositoryLive.layer, config.live )
  • 33. Wiring up .. ZIO .serviceWithZIO[TradingService](service => //..) .provide( AccountRepositoryLive.layer, TradingServiceLive.layer, OrderRepositoryLive.layer, TradeRepositoryLive.layer, ExecutionRepositoryLive.layer, config.live ) • Declarative way to build an instance of your domain service from the speci fi ed dependencies • ZLayers - Allow automatic construction of the dependency graph of your domain model - Are composable - Are asynchronous and non-blocking - Can be acquired in parallel unlike class constructors
  • 34. Entities Value Objects <<pure>> • pure • functional • statically typed • use zio-prelude for newtypes, re fi nement types, validations etc.
  • 35. Entities Value Objects <<pure>> Repositories <<interfaces>> Implementations • pure • functional • statically typed • use zio-prelude for newtypes, re fi nement types, validations etc. • plain old scala traits • one per aggregate root • effectual contracts • concrete zio effects • persistence platform dependent • pass dependencies as constructor arguments • can be multiple for a single contract
  • 36. Entities Value Objects <<pure>> Repositories <<interfaces>> Implementations Domain Services Domain Service Implementations <<interfaces>> • pure • functional • statically typed • use zio-prelude for newtypes, re fi nement types, validations etc. • plain old scala traits • one per aggregate root • effectual contracts • concrete zio effects • persistence platform dependent • pass dependencies as constructor arguments • can be multiple for a single contract • coarse grained • can interact with multiple domain entities • effectual contracts • concrete zio effects • pass dependencies as constructor arguments • depends only on repository interfaces and NOT implementations
  • 37. Entities Value Objects <<pure>> Repositories <<interfaces>> Implementations Domain Services Domain Service Implementations <<interfaces>> Application • pure • functional • statically typed • use zio-prelude for newtypes, re fi nement types, validations etc. • plain old scala traits • one per aggregate root • effectual contracts • concrete zio effects • persistence platform dependent • pass dependencies as constructor arguments • can be multiple for a single contract • coarse grained • can interact with multiple domain entities • effectual contracts • concrete zio effects • pass dependencies as constructor arguments • depends only on repository interfaces and NOT implementations • ZLayer magic!
  • 38. Entities Value Objects <<pure>> Repositories <<interfaces>> Implementations Domain Services Domain Service Implementations <<interfaces>> Application • pure • functional • statically typed • use zio-prelude for newtypes, re fi nement types, validations etc. • plain old scala traits • one per aggregate root • effectual contracts • concrete zio effects • persistence platform dependent • pass dependencies as constructor arguments • can be multiple for a single contract • coarse grained • can interact with multiple domain entities • effectual contracts • concrete zio effects • pass dependencies as constructor arguments • depends only on repository interfaces and NOT implementations • ZLayer magic!