API Reference ResultSideEffectExtensions - ulfbou/Zentient.Results GitHub Wiki

๐Ÿ” ResultSideEffectExtensions API Reference

namespace Zentient.Results

๐Ÿ“˜ Summary

Provides fluent, expressive side-effect extension methods for IResult and IResult<T>. These enable conditional execution of actionsโ€”such as logging, metrics, or notificationsโ€”without mutating the result or disrupting the functional chain.


๐Ÿ“Œ Metadata

Property Value
๐Ÿ“ฆ Assembly Zentient.Results.dll
๐Ÿ‘ Visibility public static class
๐Ÿงฑ Category Result Extensions
๐ŸŽฏ Purpose Tap-style side-effect hooks

๐Ÿ”ง Usage

โœจ Fluent Tap Example

var result = DoSomething()
    .OnSuccess(() => Log.Information("Success!"))
    .OnFailure(errors => Log.Error("Failure: {Errors}", errors));
var result = GetUser()
    .OnSuccess(user => Audit(user))
    .OnFailure(errs => AlertAdmin(errs));

๐Ÿงฉ Extension Methods

๐Ÿ”น OnSuccess(this IResult result, Action onSuccess)

Executes onSuccess only if the non-generic IResult is successful.

Parameter Type Description
result IResult The result to inspect.
onSuccess Action Action to execute on success.
Returns IResult
The original result (unmodified).

๐Ÿ”’ Throws ArgumentNullException if result or onSuccess is null.


๐Ÿ”น OnSuccess<TValue>(this IResult<TValue> result, Action<TValue> onSuccess)

Executes onSuccess only if the generic result is successful, passing the success value.

Parameter Type Description
result IResult<TValue> The result to inspect.
onSuccess Action<TValue> Action to execute with the value.
Returns IResult<TValue>
The original result (unmodified).

๐Ÿ”’ Throws ArgumentNullException if any argument is null.


๐Ÿ”น OnFailure(this IResult result, Action<IReadOnlyList<ErrorInfo>> onFailure)

Executes onFailure only if the result indicates failure, passing the full error list.

Parameter Type Description
result IResult The result to inspect.
onFailure Action<IReadOnlyList<ErrorInfo>> Action to execute on failure.
Returns IResult
The original result (unmodified).

๐Ÿ”’ Throws ArgumentNullException if any argument is null.


๐Ÿ”น OnFailure<TValue>(this IResult<TValue> result, Action<IReadOnlyList<ErrorInfo>> onFailure)

Executes onFailure only if the generic result is failure, passing the error list.

Parameter Type Description
result IResult<TValue> The result to inspect.
onFailure Action<IReadOnlyList<ErrorInfo>> Action to execute on failure.
Returns IResult<TValue>
The original result (unmodified).

๐Ÿ”’ Throws ArgumentNullException if any argument is null.


๐Ÿ“™ Best Practices

Scenario Recommendation
Logging result outcomes Use OnSuccess / OnFailure for non-intrusive logging
Adding metrics or telemetry Inject counters or traces inside tap callbacks
Maintaining functional purity Avoid mutating shared state inside side-effect actions
Fluent method chaining All methods return the original result for chaining

๐Ÿงช Example Usage

return SomeOperation()
    .OnSuccess(() => logger.Log("All good"))
    .OnFailure(errors => logger.Warn("Failed: {0}", string.Join(", ", errors)));
โš ๏ธ **GitHub.com Fallback** โš ๏ธ