Created
December 21, 2017 15:48
-
-
Save kahneraja/cc8f47e3a0eb876ef2f4c94f21cdc90a to your computer and use it in GitHub Desktop.
SCALA WORKSHEET: Some ideas around how a Use Case pattern might help.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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