Created
April 6, 2012 22:47
-
-
Save NoelKennedy/2323679 to your computer and use it in GitHub Desktop.
Either<T,U> in action
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
public abstract partial class Either<T, U> {...} | |
//here are some business rules | |
public Either<LoanDeclinedReason, LoanRequest> CheckCustomerCredit(LoanRequest request) { | |
if (request.Customer.CreditScore > 8) return Either.Right(request); | |
return Either.Left(LoanDeclinedReason.InsufficientCreditScore); | |
} | |
public Either<LoanDeclinedReason, LoanRequest> CheckGoldPrerequisiteCustomer(LoanRequest request) { | |
if (request.Value < 10000) return Either.Right(request); //dont need gold status for less than 10,000 | |
if (request.Customer.GoldCustomer) return Either.Right(request); | |
return Either.Left(LoanDeclinedReason.RequiresGoldCustomer); | |
} | |
public Either<LoanDeclinedReason, LoanRequest> CheckSillyAmount(LoanRequest request) { | |
if (request.Value > 1000000) | |
return Either.Left(LoanDeclinedReason.LoanRequestTooHigh); | |
return Either.Right(request); | |
} | |
public void LoanApproval(){ | |
LoanRequest request = new LoanRequest { | |
Customer = new Customer { CreditScore = 10, GoldCustomer = true }, | |
Value = 9000}; | |
Either<LoanDeclinedReason, LoanRequest> checkOne = CheckCustomerCredit(request); | |
Either<LoanDeclinedReason, LoanRequest> checkTwo = checkOne.FlatMap(CheckGoldPrerequisiteCustomer); | |
Either<LoanDeclinedReason, LoanRequest> checkThree = checkTwo.FlatMap(CheckSillyAmount); | |
bool loanApprovalStatus = checkThree.Fold(failReason=>false,passedLoan=>true); | |
loanApprovalStatus.Should().BeTrue(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment