OptionTypes 1.1.0
dotnet add package OptionTypes --version 1.1.0
NuGet\Install-Package OptionTypes -Version 1.1.0
<PackageReference Include="OptionTypes" Version="1.1.0" />
paket add OptionTypes --version 1.1.0
#r "nuget: OptionTypes, 1.1.0"
// Install OptionTypes as a Cake Addin
#addin nuget:?package=OptionTypes&version=1.1.0
// Install OptionTypes as a Cake Tool
#tool nuget:?package=OptionTypes&version=1.1.0
OptionTypes
Description
OptionTypes is a package to use some useful monads in C#. It contains basically 2 classes:
The
Maybe<T>
class allows to create an item of type T that may have no value. This value cannot be accessed in an unsafe manner, making really easy to completely remove null references from your code and reducing the number ofNullReferenceException
thrown.The
Result
class represents an operation that has been completed. It's slim API makes taking a decision based on the result of the operation straightforward.
How to use it
To create a variable as a Maybe<T>
, helper methods in the static Maybe
class can be used, or use the static Some
/None
methods in Maybe<T>
class.
To use the value inside the Maybe
class, use the Map
, Bind
, Match
or ValueOr
methods.
To create a Result
type, use the static methods Ok
/Error
.
Usage example
Maybe
public static async Task Main(string[] args)
{
var userTelephone = await GetUser(args[0])
.Map(u => u.Telephone);
Console.WriteLine(userTelephone.ValueOr("Telephone not found"));
}
private static async Task<Maybe<User>> GetUser(string id)
{
var user = await UserManager.GetAsync(id);
return Maybe.FromValue(user);
}
Result
public Result<Unit, string> WriteFile(string path, byte[] content)
{
try
{
File.WriteAllBytes(path, content);
return Result<Unit, string>.Ok(default);
}
catch
{
return Result<Unit, string>.Error("Error");
}
}
public static void Main(string[] args)
{
var result = WriteFile(path, content);
result.Match(
_ => Console.WriteLine("Success"),
err => Console.WriteLine(err)
);
}
Usage inside Entity Framework Core
This uses the internal EF Api
The project OptionTypes.Ef contains the ValueConverters
needed to map the Maybe<T>
type to the Entity Framework columns.
In the OnModelCreating
method overriden in your DbContext, call AddOptionTypeConverters
.
This will add all the converters needed in your model.
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 is compatible. 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. |
.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 is compatible. |
.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.
-
.NETStandard 2.1
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on OptionTypes:
Package | Downloads |
---|---|
OptionTypes.Ef
Value converters for EntityFramework to convert to OptionTypes |
GitHub repositories
This package is not used by any popular GitHub repositories.