Skip to content

Instantly share code, notes, and snippets.

@kahneraja
Created December 21, 2017 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kahneraja/cc8f47e3a0eb876ef2f4c94f21cdc90a to your computer and use it in GitHub Desktop.
Save kahneraja/cc8f47e3a0eb876ef2f4c94f21cdc90a to your computer and use it in GitHub Desktop.
SCALA WORKSHEET: Some ideas around how a Use Case pattern might help.
// REQUESTVAlIDATORS
trait RequestValidator {
def Validate(): Boolean // various approaches to validating http requests
}
// AUTHPOLICIES
trait AuthPolicy {
def Authorize() // handle user context and various exceptions
}
// REPO
trait UserRepository {
def Save(user: User): User
}
// REQUESTS
case class UserRequest(name: String, minimumBalance: Int)
// RESPONSES
case class UserReponse(name: String, minimumBalance: Int)
// DOMAIN
case class User(name: String, age: Int)
// USE CASES
// ### Basic DI that is easy to understand and easy to test.
class CalculateMinimumBalanceUseCase(
userRepository: UserRepository, // DI
specialMathLibrary: SpecialMathLibrary
authPolicy: AuthPolicy,
userContext: UserContext,
validators: Seq[RequestValidator]
) {
def execute(
userRequest: UserRequest,
resultHandler: ResultHandler
) = {
// ### Separate auth
// authPolicy.Authorize()
// ### Separate request validation
// validators.all.validator
// business logic.... do maths.
// return resultHandler.OnSuccess(minimumBalanace)
// return resultHandler.OnUnknownUser()
// return resultHandler.OnUnableToCalculateBalance()
}
// ### A response contract that is business readable. Easy to understand. Outlining all behaviour.
trait ResultHandler {
def OnSuccess(): Double
def OnUnkownUser()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment