SlideShare ist ein Scribd-Unternehmen logo
1 von 59
Railway Orientated
Programming
C# Edition
Tama Waddell
 No Functional Programming experience is
required
 Experience in C# or other Object Oriented
languages helps
 Basic understanding of Trains
Prerequisites
Set the scene
 Working on a team at AGL
 Discussions about code cleanliness comes up
regularly
 Wanted to help out
 This is the story
The Problem
Example project
 Top up MyKi
 An API
 Example Only
 Credit card only
payment
Happy Path
1.Validate request
2.Charge the credit card
3.Update balance
4.Return receipt
Sad Path
 Increases the size of
the code
 Makes it less
readable
 Losing the domain of
the code
ModelView Controller
 Model View Controller (MVC)
first used at Xerox in the 1970s
 Popularised on the web thanks
to Ruby on Rails.
 Fat Controller and Fat Model
problem
The Service Layer
 Enterprise Application
Architectures by Martin
Fowlers
 Manipulate the model
 Can the controller show us
the flow
The God Object
 MVC
 Service layer
 MVVM
 CQS
 Serverless
Goals
 Happy Path code without sacrificing error
checking
 Remove the God Object and compose smaller
functions
 Make the controller show the data flow
 Introduce some Functional Programming
concepts
 Learn something new and have fun!
A Functional Approach
C# and Functional
Programming
 Lambda expressions
 LINQ
 Better tuple support,
 Pattern matching
 Read only collections
 Local functions
 etc
Functional ApproachTo Error
Handling Scott Wlashin
https://fsharpforfunandprofit.com/rop
 Marcus Denny from ALT.NET
 Vladimir Khorikov – Microsoft MVP from
Washington DC
 https://enterprisecraftsmanship.com
 The railway is the analogy
 Happy path programming
Introducing ROP
 Result
 Error
 Tracks (success, fail)
 Functions (execute)
 Switches (join tracks)
Success
Input
Failure
Function
Result
Result
 Holds either success or
failure
 Can hold errors on failure
 Holds a value on success
 Is immutable
 Recreated on each call
 Can transform the type
Success Failure
Input
F# Results
 Native support F# 4.1 (early 2017)
 https://docs.microsoft.com/en-
us/dotnet/fsharp/language-
reference/results
 Has Error and Ok as keywords
 Can we do this in C# ?
C# Result
 Immutable
 Private constructors
 Constructs either
Success or Fail
Error
ErrorType
 Store all the business
errors in an enumeration
 Forces you to handle all
errors
 Keeps all the errors
together
 Could be a big ugly in
size
Exceptions
 Exceptions are for
exceptional
circumstances
 Don’t use for control
flow
 Good use for I/O errors
Tracks
SingleTrack Function
 Single track function is just
a function which returns a
value
Input
Output
TwoTrack Function
Success
Input
Failure
 Two track function is a
function which returns a
result type
Dead End Function Input
 Dead End function is a
function which doesn't
return anything
Supervisory
 Both tracks
 E.g. Logging, metrics
Success Failure
Success Failure
Switches
Switches
 Constructors Create a new track
 Adapters Convert one kind of track into
another
 Combiners Joining many tracks
Bind
 Adapter Block
 Two-Track in, Two-Track out
 Only executes on the
Success Track
Success Failure
Success
Function
Failure
Map
 Adapter Block
 One-Track in, Two-Track out
 Only executes on the
Success Track
Success Failure
Success Failure
DoubleMap
 Supervisory functions
 Executes on both Tracks
Success Failure
Success Failure
Tee
 Adapter Block
 Takes a dead-end function
 Turns it into a one-track
Success
Success
Succeed
Success Failure
 A Constructor
 Always on the Success Track
 One-Track, Two-Track
Input
Fail
FailureSuccess
 A Constructor
 One-Track, Two-Track
 Always on the Failure Track
Input
Lift Example 1
 Exception handling
 One-Track, Two-Track
FailureSuccess
Success
Lift Example 2
 Boolean to result
 One-Track, Two-Track
FailureSuccess
Success
Handle Success Failure
Output
In Action
 New methods for this
example
 Bind two Two-Track
functions
 Validate the Amount
 Validate the Account
Number
Validation
Validation Cont.
Validate
Amount
Validate
Account
Failure
Input
 Two-Track function
 Join them together with
Bind
Credit Card Charge
 Exception Handling
 Single-Track to Two-Track within
the function
Charge
Credit Card
Validate
Amount
Success Failure
Update Card
Charge
Credit Card
Success Failure
Update
Card
 Dead End Function
 Tee
Log
 DoubleMap Update
Card
Log
Return Receipt
 Map
Log
Success Failure
Make
Response
Bonus!
 OnSuccess
 OnFailure
 OnBoth
 SucceedWith
 FailWith
Bringing It AllTogether
Handle
Output
Validate
Account
Input
Validate
Amount
Charge
Credit Card Update Card
Log Make
Response
Evaluation
Developer Feedback
 Liked the reduced nesting
 Was more readable
 Service layer broken down into reusable parts
 Is showing dataflow better
 Bit of a learning curve
 Bit difficult to debug
 Preferred the OnSuccess/OnFailure over Map,
Bind
 State machines
 Validation
 Business logic
 Lambdas/ Azure Functions
Good Use Cases
 Code shows the Happy Path without sacrificing
error checking
 Compose smaller reusable functions
 The controller shows the data flow
 Learned some minor Functional Programming
concepts
 And have fun!
Recap Goals
Advance
Async/Await
 Task to overlap result
 Needs combinations of accepted and
return
CQS
 Command Query Separation
 Command returns the result
type
 Query should be Single Track
Azure Functions / AWS
Lamdas Single page please
 Show the flow at the top of
the page
 Keep it simple
Resources
 https://www.pluralsight.com/courses/csharp-
applying-functional-principles
 https://fsharpforfunandprofit.com/rop/
Thank you!
Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

Quản lý hoạt động giảng dạy sử dụng ASP.NET
Quản lý hoạt động giảng dạy sử dụng ASP.NETQuản lý hoạt động giảng dạy sử dụng ASP.NET
Quản lý hoạt động giảng dạy sử dụng ASP.NET
Trung Thành Nguyễn
 
Defensive programing 101
Defensive programing 101Defensive programing 101
Defensive programing 101
Niall Merrigan
 

Was ist angesagt? (20)

Bai tap java_script-html-2016
Bai tap java_script-html-2016Bai tap java_script-html-2016
Bai tap java_script-html-2016
 
[Practical] Functional Programming in Rails
[Practical] Functional Programming in Rails[Practical] Functional Programming in Rails
[Practical] Functional Programming in Rails
 
ChChương 2_Thiết kế website thương mại điện tử_ThS Lê Thị Bích Hoà
ChChương 2_Thiết kế website thương mại điện tử_ThS Lê Thị Bích HoàChChương 2_Thiết kế website thương mại điện tử_ThS Lê Thị Bích Hoà
ChChương 2_Thiết kế website thương mại điện tử_ThS Lê Thị Bích Hoà
 
Http
HttpHttp
Http
 
Building confidence in concurrent code with a model checker: TLA+ for program...
Building confidence in concurrent code with a model checker: TLA+ for program...Building confidence in concurrent code with a model checker: TLA+ for program...
Building confidence in concurrent code with a model checker: TLA+ for program...
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Building occasionally connected applications using event sourcing
Building occasionally connected applications using event sourcingBuilding occasionally connected applications using event sourcing
Building occasionally connected applications using event sourcing
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
Quản lý hoạt động giảng dạy sử dụng ASP.NET
Quản lý hoạt động giảng dạy sử dụng ASP.NETQuản lý hoạt động giảng dạy sử dụng ASP.NET
Quản lý hoạt động giảng dạy sử dụng ASP.NET
 
Defensive programing 101
Defensive programing 101Defensive programing 101
Defensive programing 101
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of Composition
 
Spring Boot 소개
Spring Boot 소개Spring Boot 소개
Spring Boot 소개
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Pairwise testing
Pairwise testingPairwise testing
Pairwise testing
 
What is Ajax technology?
What is Ajax technology?What is Ajax technology?
What is Ajax technology?
 
Báo cáo môn đảm bảo chất lượng phần mềm
Báo cáo môn đảm bảo chất lượng phần mềmBáo cáo môn đảm bảo chất lượng phần mềm
Báo cáo môn đảm bảo chất lượng phần mềm
 

Ähnlich wie Railway Orientated Programming In C#

...and thus your forms automagically disappeared
...and thus your forms automagically disappeared...and thus your forms automagically disappeared
...and thus your forms automagically disappeared
Luc Bors
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010
Christian Heilmann
 

Ähnlich wie Railway Orientated Programming In C# (20)

[4DEV] Bartosz Sokół - Functional developer in object oriented world - how F#...
[4DEV] Bartosz Sokół - Functional developer in object oriented world - how F#...[4DEV] Bartosz Sokół - Functional developer in object oriented world - how F#...
[4DEV] Bartosz Sokół - Functional developer in object oriented world - how F#...
 
Ppt lesson 06
Ppt lesson 06Ppt lesson 06
Ppt lesson 06
 
Ppt lesson 06
Ppt lesson 06Ppt lesson 06
Ppt lesson 06
 
Ppt lesson 06
Ppt lesson 06Ppt lesson 06
Ppt lesson 06
 
An easy way to automate complex UI
An easy way to automate complex UIAn easy way to automate complex UI
An easy way to automate complex UI
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Test-Driven Development Introduction
Test-Driven Development IntroductionTest-Driven Development Introduction
Test-Driven Development Introduction
 
Using feature teams to deliver high business value
Using feature teams to deliver high business valueUsing feature teams to deliver high business value
Using feature teams to deliver high business value
 
...and thus your forms automagically disappeared
...and thus your forms automagically disappeared...and thus your forms automagically disappeared
...and thus your forms automagically disappeared
 
Moving to Microsoft Visual Basic .NET
Moving to Microsoft Visual Basic .NETMoving to Microsoft Visual Basic .NET
Moving to Microsoft Visual Basic .NET
 
Best practices for upgrading vb 6.0 projects to vb.net
Best practices for upgrading vb 6.0 projects to vb.netBest practices for upgrading vb 6.0 projects to vb.net
Best practices for upgrading vb 6.0 projects to vb.net
 
Pragmatic Architecture in .NET
Pragmatic Architecture in .NETPragmatic Architecture in .NET
Pragmatic Architecture in .NET
 
Class 7 lecture notes
Class 7 lecture notesClass 7 lecture notes
Class 7 lecture notes
 
Meet Magento Belarus 2015: Uladzimir Kalashnikau
Meet Magento Belarus 2015: Uladzimir KalashnikauMeet Magento Belarus 2015: Uladzimir Kalashnikau
Meet Magento Belarus 2015: Uladzimir Kalashnikau
 
Experience With Aida Web
Experience With Aida WebExperience With Aida Web
Experience With Aida Web
 
AD207 Presentation
AD207 PresentationAD207 Presentation
AD207 Presentation
 
Evolutionary db development
Evolutionary db development Evolutionary db development
Evolutionary db development
 
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
 
Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010Understanding progressive enhancement - yuiconf2010
Understanding progressive enhancement - yuiconf2010
 
SAP ABAP Online Training Institute in Hyderabad - C-Point
SAP ABAP Online Training Institute in Hyderabad - C-PointSAP ABAP Online Training Institute in Hyderabad - C-Point
SAP ABAP Online Training Institute in Hyderabad - C-Point
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Kürzlich hochgeladen (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Railway Orientated Programming In C#

Hinweis der Redaktion

  1. Thank you to our sponsors
  2. Take us back a year from now. We we're happy developers coding away. However, as usual, we kept having difficulties deciding what we considered to be "clean" code. Difference between code cleanliness and code quality After many meetings, and broken design patterns we opted to give a functional approach a go. Raise your hand if you’ve ever had any discussions about code cleanliness This was originally proposed as a solution to some of the issues we had discussing code cleanliness Let’s see how it works and how it worked out
  3. 3 Mins
  4. We’re going to implement a simple API You could image it being what’s behind this technology you may have used We’re not going to show some real world code. None of this code is legit code, or for that matter; is intended to make any sense It’s purpose is to convey a problem and show the solution It’s something we could all be somewhat familiar with
  5. 1. Validate request 2. Charge the credit card 3. Update balance 4 Return receipt Looks pretty simple So, this is generally how your code starts off Explain code In reality we have to deal with validation, logging, errors, exceptions etc
  6. Throw some null checks in Test return conditions Add some Try catches Lots of if statements for error handling Plus some logging, if you’re lucky Can turn 6 lines of code in 19 How many people have this problem? How can we make the code more readable without sacrificing our standard checks?
  7. We’ve also have another problem Were does the business logic go? In the MVC design pattern, the controller manipulates the model and the view generates the content based off the model. Create reusable business logic Who remembers the fat model / fat controller arguments? Do we put it all in the controller or the model? This is something I’ve seen in projects for years. “Where does my business logic lie”
  8. After following Martins Fowlers advice or reading Enterprise Application Architectures you’ll probably be in favour of the service layer But we can keep coming back to the same issue of the God Object
  9. We’ve had solutions in the past but it keeps reoccurring It’s coined the God Object, where all the important code lies where it’s just more connected to everything than it should be Other patterns like MVVM explicitly separate the business logic a bit better CQS seems with frameworks like the mediator patterns gets you a bit out of this problem But we have we solves the problem We want: No separation of concern Ease of reusability By making and composing smaller functions
  10. 7 mins
  11. F# code is pretty neat We've got lots of different styles to programming such as Object Oriented (C#, Java), Event driven, Procedural, declarative etc Railway oriented programming is more of a design pattern than a rule to the above Functional programming is another which is slowly entering the mainstream more each year This has affected our programming in some ways For example, C# 3 brought us lambda expressions, LINQ and now we have better tuple support, pattern matching, expression bodied members, read only collections, local functions Other libraries bought us ReactiveX with rx.net, language-ext So why can’t we have a functional approach to error handlng
  12. Can we have a functional approach to error handling? Will it help show us the flow of the code to neaten it up? It was originally popularised by Scott Wlaschin in his talk at NDC Oslo which is entirely in F# The name railway is there to help explain the concept as a metaphor for how the errors are handled Scott Wlashin also has blogged about ROP which is one of his top hits which you can check out further if you're Other developers have also attempted to convert this into c# My main inspiration was Marcus Denny from alt.net! Vladimir Khorikov MVP from Washinton DC Functional programming in C# course on pluralsight It’s rather simple but takes a little practice
  13. We want to code for the happy path As the input comes in here, it enters the function Which returns whether the result of that function was a success or failure The tracks are you input and output into your function Use switches to join the tracks together We can have many different types of tracks and switches which compose all of the functions together On the success line we can carry different types to be the next input
  14. 10 Mins
  15. This is easiest to start with in my opinion Either monad is either right or left Holds either success or failure When successful holds the returned data Can hold errors on failure Chain these two tracks together
  16. Native support in F# 4.1 (2017) Thanks to discriminated unions! Mentioned by Microsoft in their documentation Wont be covering F# only in C#
  17. Alternative - Can store Tsuccess Tfailure to return a data in the failed component
  18. 15 mins
  19. This way you can process it with more information Store all the different types of errors in an enumeration Keeps all the errors together Serves as documentation Forces you to handle all errors Type safety for testing Could be a big ugly in size
  20. Who would prefer to return false, raise your hand? Exceptions follow like a GOTO statement – hard to read Don’t use it for control flow Can be slower – compilers generally don’t optimise for exceptions as control flow Generally used for I/O problems Global Exception handlers
  21. 17 Mins
  22. The railway analogy Can we get it really the same a F# -> no
  23. Two track function is a function which returns a result type Useful for most cases
  24. Single track function is just a function which returns a value The easiest one, but can be a bit tricky to join No failure You can lift these with the switches
  25. Single track function is just a function which returns a value The easiest one, but can be a bit tricky to join No failure You can lift these with the switches
  26. 20 Mins
  27. We’re connecting them up through extension methods based off of the result type There are a few different types of switches Explain Extension methods, Explain func Combiners aren’t explained in the next slides but the idea is you can connect say a Map and a
  28. It’s not too hard Explain extension method Explain Func If just an if statement on a result type to execute another function Notice: The func return type Notice: The switchfunction doesn’t use result as it retuns a result The switch function can return a result which switches the tracks An adapter that takes a switch function and creates a new function that accepts two-track values as input. Can still transform types!
  29. An adapter that takes a normal one-track function and turns it into a two-track function. Switches are these maps but can change the success or fail based on the return type Map is good for getting functions that done return a Result object to continue to follow the same path Remember: The highlighted part is what is represented in this code Notice: they both create a new result type on each switch. They cannot change tracks.
  30. An adapter that takes two one-track functions and turns them into a single two-track function. Same as map but executes on both branches
  31. An adapter that takes a dead-end function and turns it into a one-track function that can be used in a data flow.
  32. A constructor that takes a one-track value and creates a two-track value on the success branch. Less used
  33. A constructor that takes a one-track value and creates a two-track value on the success branch. Less used
  34. Coming back to the exception handling These are a bit more custom, shown for an example Exceptions one isn’t the best example, can I say pokemon exception We can switch the result type here to the error path based on if an exception was throw or not It’s a single track function because it has no result return type
  35. This switch returns a success or fail On success only Can change tracks based on the single track return type No result return type
  36. Finally, you’ll need a adapter block to transfer the two tracks into some output Not the best of examples Tdata from result type can then become the returned data This has to be kind of short and sweet to get the point across, but it’s not entirety accurate. You can see how we’re returning a response based off of whether or not this was successful or not Brings this together for output, Lots of options for this depending on your desired outcome or where you intend to use this handle
  37. 30 Mins
  38. Apply it to our problem Chain together the functions with switches Which will decrease the amount of error handling code Making our controllers look pretty and understandable Service layer will have reusable code Lets see if we can make it happen Easiest one to grasp first There’s a few different ways we can approach this Lets start with a bind, the most common and easiest. We’ll create a two-track function for each Could use the bool lift function shown earlier
  39. Notice even though we’re returning a Two-Track, we excuse using the result with the _ score lambda That underscore actually represents the accountNumber as in the previous example We transform the type in the result with each call We still have access to the scope of whatever has been passed in the parameters No result prior to the first validate account call I hope this is making a bit more sense
  40. Again this exception code isn’t how you would implement it Here we’ve actually used an expression bodied member which says our whole method is an expression! You’re better off moving it into another method and have a result type returned but for the sake of a call you couldn’t change
  41. On a successful result, update the card
  42. Success or fail
  43. Executes on the success branch Return the same result with a different type Remember: difference between the Map and the DoubleMap is that the double map will execute on both tracks
  44. I know what you’re all thinking, where’s the handle Before we get to that and wrap it all up, we have a bonus! C# has operator overloading! We can give two methods the same name All you legitimately do is name the methods OnSuccess, OnFailure, OnBoth, FailWith Reduce the functional programming knowledge Make it easier for developers to just type OnSuccess and provide what they want, it will find the method for you! Reduces the thought process with no extra effort or errors
  45. The money shot It’s all the same Bind Map etc underneath Reusable on other similar Actions I could make another action that does something similar and join compose them together differently It doesn’t look as cool as F# though
  46. 35 Mins
  47. Come back to our story We revaluated our ROP trial on the AGL team Feedback was mostly positive Siva: Liked how we no longer needed if statements. May want to use it again. Found it a bit tricky at first but felt others could understand it easily. Noticed it did break down the services into reusable functions and the action was showing dataflow better. Anup: Thought the code was much more readable. Did notice that it broke down the service layer. Thought it was understandable at first but as the extension methods grew found it hard to work with. Thinks he needs more time to learn it and show a better understanding Sudheerah: Found it a bit difficult to debug. Overall did find it more expressive. Dylan: Once the extension methods started building up, he found it difficult to know what to use. There was a bit of a learning curve at the beginning to understand the flow of data. Felt that the flow of data was easy to read after it had been written. Varan: Found the map bind confusing. Wasn’t a big fan of the functional wording. Preferred onsuccess on failure etc. Did like using it over if statements to check if something was successful or not. Raghav: Thought the code turned out rather readable. Wanted to polish it up a bit more for future use.
  48. Does it fit everywhere? Validation could include like some sort of file parsing
  49. 40 mins
  50. So if you’re not a fan of MVC, maybe you just want routes and commands to be processed instead Becoming a bit more of a popular pattern
  51. Single file please
  52. If you’re interested further, learn functional programming