OptionTypes.Ef
1.0.5
See the version list below for details.
dotnet add package OptionTypes.Ef --version 1.0.5
NuGet\Install-Package OptionTypes.Ef -Version 1.0.5
<PackageReference Include="OptionTypes.Ef" Version="1.0.5" />
paket add OptionTypes.Ef --version 1.0.5
#r "nuget: OptionTypes.Ef, 1.0.5"
// Install OptionTypes.Ef as a Cake Addin #addin nuget:?package=OptionTypes.Ef&version=1.0.5 // Install OptionTypes.Ef as a Cake Tool #tool nuget:?package=OptionTypes.Ef&version=1.0.5
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 | net6.0 is compatible. 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. |
-
net6.0
- Microsoft.EntityFrameworkCore (>= 6.0.13)
- OptionTypes (>= 1.0.5)
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.13)
- OptionTypes (>= 1.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.