WS.DomainModelling.Common
1.0.15
dotnet add package WS.DomainModelling.Common --version 1.0.15
NuGet\Install-Package WS.DomainModelling.Common -Version 1.0.15
<PackageReference Include="WS.DomainModelling.Common" Version="1.0.15" />
<PackageVersion Include="WS.DomainModelling.Common" Version="1.0.15" />
<PackageReference Include="WS.DomainModelling.Common" />
paket add WS.DomainModelling.Common --version 1.0.15
#r "nuget: WS.DomainModelling.Common, 1.0.15"
#:package WS.DomainModelling.Common@1.0.15
#addin nuget:?package=WS.DomainModelling.Common&version=1.0.15
#tool nuget:?package=WS.DomainModelling.Common&version=1.0.15
WS.DomainModelling.Common
Common types used by the WS.DomainModelling source generator package. These types are also available for direct use in your own projects.
Option
Option<T> represents a value that may or may not be present, similar to F#'s Option type.
Creating Options
var some = Option<int>.Some(42);
var none = Option<int>.None;
// Using the static helper
var some = Option.Some(42);
var none = Option.None;
// Using the extension method
var some = 42.Return();
Accessing Values
Use Match to extract the value by providing handlers for both cases:
string message = option.Match(
value => $"Got {value}",
() => "No value"
);
Use Switch for side effects:
option.Switch(
value => Console.WriteLine($"Got {value}"),
() => Console.WriteLine("No value")
);
Composing Options
Use Bind and Map to chain operations:
Option<int> result = Option.Some(5)
.Bind(x => x > 0 ? Option.Some(x) : Option.None)
.Map(x => x * 2);
Result
Result<T, TError> represents the outcome of an operation that can either succeed or fail.
Creating Results
var success = Result<int, string>.Success(42);
var error = Result<int, string>.Error("Something went wrong");
// Implicit conversions are supported
Result<int, string> result = 42; // Success
Result<int, string> result = "Error"; // Error
Accessing Values
string message = result.Match(
value => $"Got {value}",
error => $"Failed: {error}"
);
Composing Results
Use Bind and Map to chain operations, errors short-circuit automatically:
Result<int, string> result = Result<int, string>.Success(5)
.Bind(x => x > 0
? Result<int, string>.Success(x)
: Result<int, string>.Error("Must be positive"))
.Map(x => x * 2);
Railway-Oriented Programming
Result supports railway-oriented programming patterns with Plus for combining parallel validations and TryCatch for wrapping exception-throwing code:
var validate = Result.Plus<Input, Name, Age, Person, IReadOnlyList<string>>(
(name, age) => new Person(name, age),
(errors1, errors2) => [.. errors1, .. errors2],
input => ValidateName(input),
input => ValidateAge(input)
);
TaskResult
TaskResult<T, TError> wraps a Task<Result<T, TError>> and provides async-friendly versions of
Bind, Map, Match and Switch. It allows async and synchronous operations to be chained
fluently without awaiting at each step.
Creating a TaskResult
// From an existing Result (implicit conversion)
TaskResult<int, string> tr = Result<int, string>.Success(42);
// From a Task<Result<T, TError>> (implicit conversion)
TaskResult<int, string> tr = SomeAsyncMethodReturningResult();
// Or via the async extension methods on Result
TaskResult<int, string> tr = Result<int, string>.Success(5)
.Bind(async x => await LookupAsync(x));
Composing async pipelines
Bind and Map accept both synchronous and asynchronous delegates. Errors short-circuit automatically:
TaskResult<Order, string> result = await Result<int, string>.Success(orderId)
.Bind(async id => await LoadOrderAsync(id)) // async Bind
.Map(order => order with { Status = "Processed" }) // sync Map
.Bind(async order => await SaveOrderAsync(order)); // async Bind
Accessing the value
string message = await taskResult.Match(
value => $"Got {value}",
error => $"Failed: {error}"
);
await taskResult.Switch(
value => Console.WriteLine($"Got {value}"),
error => Console.WriteLine($"Failed: {error}")
);
Async TryCatch
Wrap exception-throwing async code using TryCatchAsync:
TaskResult<string, string> result = await orderId
.TryCatchAsync(
async id => await FetchFromApiAsync(id),
ex => ex.Message
);
Extension Methods
FuncExtensionMethods
Compose— Composes two functions together (f.Compose(g)producesx => g(f(x)))
ObjectExtensionMethods
Pipe— Pipes a value into a function (value.Pipe(f)is equivalent tof(value))
ListExtensionMethods
Sequence— Converts a list ofOption<T>into anOption<IReadOnlyList<T>>(returnsNoneif any element isNone)Sequence— Converts a list ofResult<T, TError>into aResult<IReadOnlyList<T>, IReadOnlyList<TError>>(collects all errors)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on WS.DomainModelling.Common:
| Package | Downloads |
|---|---|
|
WS.DomainModelling
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.