Created
September 2, 2024 09:07
-
-
Save MichalBrylka/38895c1b324cdf0df7e8b6fe18235b4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://aka.ms/new-console-template for more information | |
using FluentResults; | |
//1. simple | |
Console.WriteLine(Divide(5, 0)); | |
//2. fluent | |
var fluent = Result.Fail("error message 1") | |
.WithError("error message 2") | |
.WithError("error message 3") | |
.WithSuccess("success message 1"); | |
//3. Try | |
var result = Result.Try(() => | |
{ | |
if (IsInvalid()) | |
{ | |
return Result.Fail("Some error"); | |
} | |
int id = DoSomethingCritical(); | |
return Result.Ok(id); | |
static bool IsInvalid() => Random.Shared.NextDouble() > 0.5; | |
static int DoSomethingCritical() => Random.Shared.NextDouble() > 0.5 ? 42 : throw new Exception("Bad"); | |
}); | |
Console.WriteLine(result); | |
static Result<int> Divide(int dividend, int divisor) => | |
divisor == 0 ? Result.Fail("Cannot divide by zero") : (dividend / divisor); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net8.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="FluentResults" Version="3.16.0" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://aka.ms/new-console-template for more information | |
using Vogen; | |
var ttt = ScrapedString.TryFrom("\t \t", out var parsed); | |
var tdtt = ScrapedString.From("\t \t"); | |
[ValueObject<string>(conversions: Conversions.TypeConverter | Conversions.SystemTextJson)] | |
public readonly partial struct Password { } | |
[ValueObject] | |
public readonly partial struct CustomerId { } | |
[ValueObject] | |
public readonly partial struct AccountId | |
{ | |
private static int NormalizeInput(int input) | |
{ | |
// todo: normalize (sanitize) your input; | |
return input; | |
} | |
} | |
[ValueObject<decimal>] | |
public readonly partial struct PaymentAmount; | |
[ValueObject<string>(parsableForStrings: ParsableForStrings.GenerateMethodsAndInterface)] | |
public partial class ScrapedString | |
{ | |
private static Validation Validate(string value) => | |
value.Length == 0 ? Validation.Invalid("Can't be empty") : Validation.Ok; | |
private static string NormalizeInput(string input) => input.Trim(); | |
} | |
[ValueObject(typeof(float), | |
conversions: Conversions.SystemTextJson, | |
throws: typeof(ArgumentException), | |
customizations: Customizations.TreatNumberAsStringInSystemTextJson)] | |
[Instance("Freezing", 0)] | |
[Instance("Boiling", 100)] | |
public readonly partial struct Celsius | |
{ | |
public static Validation Validate(float value) => | |
value >= -273 ? Validation.Ok : Validation.Invalid("Cannot be colder than absolute zero"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net8.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
<!--Be able to browse generated files--> | |
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | |
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Vogen" Version="4.0.19" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment