Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Bind in addition to Map to support Railway Oriented Programming #211

Closed
christophercalm opened this issue Oct 8, 2024 · 1 comment
Closed

Comments

@christophercalm
Copy link
Contributor

christophercalm commented Oct 8, 2024

I have found the map method to be extremely useful when chaining result types. In addition to the Map method, I have added Bind method with async overloads that allow me to combine operations that may fail or be invalid. Here is an example of a flow that is enabled by these extensions.

Result addOrUpdateResult = await GetProfile(request.Id, ct)
    .BindAsync(profile => AddOrUpdateExperience(profile, request))
    .BindAsync(updatedProfile => SendIntegrationEvent(updatedProfile, request, ct))
    .BindAsync(_ => PersistChanges(request))
    .ToAsyncResult();

return addOrUpdateResult;

This adds support for railway oriented programming Railway Oriented Programming that makes for very clean and terse mediatr handlers that create an easy to read data pipeline. I have used this in conjuction with your modular monolith course and found a lot of value in it.

Currently my version only supports the states that I use (Invalid, Error, and Success), but it would be trivial to add the rest. Would you be interested in a PR adding this to the library?

@KyleMcMaster
Copy link
Collaborator

KyleMcMaster commented Oct 10, 2024

With the introduction of Map in #97, this library does have some support for functional programming practices and as a fan of Railway Oriented Programming I would be in favor of adding a BindAsync method too. I have had a good experience using similar approaches from CSharpFunctionalExtensions.

Edit: I also believe we should support all statuses to be comprehensive in our approach.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants