SkillAgentSearch skills...

OneOf.Chaining

Extension methods which use the excellent OneOf library to enable asynchronous method chaining in C#

Install / Use

/learn @andrewjpoole/OneOf.Chaining
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Nuget Nuget

OneOf.Chaining

Extension methods which enable method chaining in C#, using the excellent OneOf library.

Turn this:

public async Task<OneOf<WeatherReport, Failure>> Handle(string requestedRegion, DateTime requestedDate)
{
	var isValidRequest = await regionValidator.Validate(requestedRegion);
	if (!isValidRequest)
		return new UnsupportedRegionFailure();

	var dateCheckPassed = await dateChecker.CheckDate(requestedDate);
	if (!dateCheckPassed)
		return new InvalidRequestFailure();

	var cacheCheckResult = CheckCache(requestedRegion, requestedDate);
	if (cacheCheckResult.Hit)
		return cacheCheckResult.Data;
	else
		return weatherForecastGenerator.Generate(requestedRegion, requestedDate);
}

into this:

public async Task<OneOf<WeatherReport, Failure>> Handle(string requestedRegion, DateTime requestedDate)
{
	return await WeatherReport.Create(requestedRegion, requestedDate)
		.Then(regionValidator.ValidateRegion)
		.Then(dateChecker.CheckDate)
		.Then(CheckCache)
		.IfThen(report => report.PopulatedFromCache is false, 
			weatherForecastGenerator.Generate);
}

For an explanation, see my blog post here or on ClearBank's Medium publication here

Includes

  • Then() which enables fluent chaining of any method which returns a Task<OneOf<Tsuccess, Tfailure>>.
  • Overload of Then() which takes an onFailure func, useful for tidying up previous tasks. Also able to mutate the Tfailure result.
  • IfThen() which takes a func of Tsuccess and bool, which should return true in order to invoke the nextJob.
  • ThenWaitForAll() and ThenWaitForFirst() methods which accept a list of tasks to be executed in parallel.
  • Versions of all extension methods with Cancellation support

The example project that used to be in this repository has moved to Event sourced but flow deriven examples

Related Skills

View on GitHub
GitHub Stars73
CategoryDevelopment
Updated29d ago
Forks4

Languages

C#

Security Score

95/100

Audited on Mar 12, 2026

No findings