OneOf.Chaining
Extension methods which use the excellent OneOf library to enable asynchronous method chaining in C#
Install / Use
/learn @andrewjpoole/OneOf.ChainingREADME
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 aTask<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()andThenWaitForFirst()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
node-connect
354.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
112.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
354.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
354.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
