Skip to content

andrewjpoole/OneOf.Chaining

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages