Skip to content

Commit

Permalink
chore: reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBogomips committed Sep 5, 2024
1 parent 7ab86c4 commit 80d8db6
Show file tree
Hide file tree
Showing 81 changed files with 4,546 additions and 4,547 deletions.
54 changes: 27 additions & 27 deletions sample/ConsoleApp/CustomError/ApplicationError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@

namespace Sample.CustomError;

public abstract class ApplicationError: LogicError
public abstract class ApplicationError : LogicError
{
public int ErrorCode { get; }

protected ApplicationError(string message, int errorCode)
: base(message)
{
ErrorCode = errorCode;
}

public int ErrorCode { get; }

protected ApplicationError(string message, int errorCode)
: base(message)
{
ErrorCode = errorCode;
}
}

public class NotFoundError : ApplicationError
{
public string ResourceName { get; }
public string ResourceId { get; }
public NotFoundError(string message, int errorCode, string resourceName, string resourceId)
: base(message, errorCode)
{
ResourceName = resourceName;
ResourceId = resourceId;
}

public string ResourceName { get; }
public string ResourceId { get; }
public NotFoundError(string message, int errorCode, string resourceName, string resourceId)
: base(message, errorCode)
{
ResourceName = resourceName;
ResourceId = resourceId;
}
}

public class InvalidOperationError : ApplicationError
{
public string OperationName { get; }
public string Reason { get; }
public InvalidOperationError(string message, int errorCode, string operationName, string reason)
: base(message, errorCode)
{
OperationName = operationName;
Reason = reason;
}

public string OperationName { get; }
public string Reason { get; }
public InvalidOperationError(string message, int errorCode, string operationName, string reason)
: base(message, errorCode)
{
OperationName = operationName;
Reason = reason;
}
}
10 changes: 5 additions & 5 deletions sample/ConsoleApp/Models/BookExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Sample.Models;

public static class BookExtensions
{
public static string GetPrintableTitle(this Book book)
=> book
.Author
.Map(author => $"{book.Title} by {author.GetFullName()}")
.GetValue(() => book.Title);
public static string GetPrintableTitle(this Book book)
=> book
.Author
.Map(author => $"{book.Title} by {author.GetFullName()}")
.GetValue(() => book.Title);
}
10 changes: 5 additions & 5 deletions sample/ConsoleApp/Models/PersonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Sample.Models;

public static class PersonExtensions
{
public static string GetFullName(this Person person)
=> person
.Surname
.Map(surname => $"{person.Name} {surname}")
.GetValue(() => person.Name);
public static string GetFullName(this Person person)
=> person
.Surname
.Map(surname => $"{person.Name} {surname}")
.GetValue(() => person.Name);
}
82 changes: 41 additions & 41 deletions sample/ConsoleApp/Pipelines/CreateUserPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@ namespace Sample.Pipelines;

public static class CreateUserPipeline
{
private const string DEMO_USER = "username@sample.com";
// Models
private record Username(string Value);
private record User(Username Username, string FirstName, string LastName);
public static void Run(string? username = null!)
{
username ??= DEMO_USER;
Console.WriteLine($"Creating user '{username}'");
var outcome = CreateUsername(username)
.Ensure(u => LookupUser(u).IsNone, new LogicError("User already exists"))
.Map(u => new User(u, "FirstName", "LastName"))
.Bind(CreateUser)
.ExecuteIfSuccess(NotifyCreation)
.Match(u => "User created.", e => $"The following error occurred: {e.Message}");

// In a real example the final Match would return a specific model, for example, an IActionResult
// or an IResult (Http) in case of AspNet Web Api.

Console.WriteLine(outcome);
}
private const string DEMO_USER = "username@sample.com";
// Models
private record Username(string Value);
private record User(Username Username, string FirstName, string LastName);
public static void Run(string? username = null!)
{
username ??= DEMO_USER;
Console.WriteLine($"Creating user '{username}'");
var outcome = CreateUsername(username)
.Ensure(u => LookupUser(u).IsNone, new LogicError("User already exists"))
.Map(u => new User(u, "FirstName", "LastName"))
.Bind(CreateUser)
.ExecuteIfSuccess(NotifyCreation)
.Match(u => "User created.", e => $"The following error occurred: {e.Message}");

private static Result<Username> CreateUsername(string username)
{
if (!username.Contains('@'))
return Result.Failure<Username>("Username must contain at least one '@'");
return Result.Success(new Username(username));
}
// In a real example the final Match would return a specific model, for example, an IActionResult
// or an IResult (Http) in case of AspNet Web Api.

private static Maybe<User> LookupUser(Username username)
{
if (username.Value == DEMO_USER)
return new User(username, "FirstName", "LastName");
return Maybe.None<User>();
}
Console.WriteLine(outcome);
}

private static Result<User> CreateUser(User user)
{
if (false) return Result.Failure<User>("Error creating user");
return new(user);
}
private static void NotifyCreation(User user)
{
Console.WriteLine(" >>> User has been notified!");
}
private static Result<Username> CreateUsername(string username)
{
if (!username.Contains('@'))
return Result.Failure<Username>("Username must contain at least one '@'");
return Result.Success(new Username(username));
}

private static Maybe<User> LookupUser(Username username)
{
if (username.Value == DEMO_USER)
return new User(username, "FirstName", "LastName");
return Maybe.None<User>();
}

private static Result<User> CreateUser(User user)
{
if (false) return Result.Failure<User>("Error creating user");

Check warning on line 44 in sample/ConsoleApp/Pipelines/CreateUserPipeline.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Check warning on line 44 in sample/ConsoleApp/Pipelines/CreateUserPipeline.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected
return new(user);
}
private static void NotifyCreation(User user)
{
Console.WriteLine(" >>> User has been notified!");
}
}
102 changes: 51 additions & 51 deletions sample/ConsoleApp/Pipelines/GamblingPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,65 @@ namespace Sample.Pipelines;
/// </summary>
public static class GamblingPipeline
{
public enum Colors
{
Red = 0, Black = 1
}
public record Amount(int Value);
public enum Colors
{
Red = 0, Black = 1
}
public record Amount(int Value);

public static void Run(int initialAmount, int numberOfAttempts)
{
Task<Result<Amount>> FourBetsInARow() => Result.Success(new Amount(initialAmount))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)));

Console.WriteLine($"You're attempting to win 4 red/black bets in a row in {numberOfAttempts} attempts.");
public static void Run(int initialAmount, int numberOfAttempts)
{
Task<Result<Amount>> FourBetsInARow() => Result.Success(new Amount(initialAmount))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)))
.Bind(a => Bet(a, (Colors)Random.Shared.Next(2)));

var attemptsTasks = new List<Task<Result<Amount>>>();
for (var i = 0; i < numberOfAttempts; i++)
{
attemptsTasks.Add(FourBetsInARow());
}
Console.WriteLine($"You're attempting to win 4 red/black bets in a row in {numberOfAttempts} attempts.");

Task.WaitAll(attemptsTasks.ToArray());
var attemptsTasks = new List<Task<Result<Amount>>>();
for (var i = 0; i < numberOfAttempts; i++)
{
attemptsTasks.Add(FourBetsInARow());
}

var attempts = attemptsTasks.Select(task => task.Result);
Task.WaitAll(attemptsTasks.ToArray());

var messages = attempts.MatchEach(
win => $"You won {win.Value}",
$"You lost {initialAmount}");

foreach (var message in messages)
{
Console.WriteLine(message);
}
var attempts = attemptsTasks.Select(task => task.Result);

var totalWin = (
from a in attempts.SelectValues()
select a.Value).Sum();
var messages = attempts.MatchEach(
win => $"You won {win.Value}",
$"You lost {initialAmount}");

Console.WriteLine($"You spent: {initialAmount * numberOfAttempts}");
Console.WriteLine($"You 'won': {totalWin}");
}
foreach (var message in messages)
{
Console.WriteLine(message);
}

private static async Task<Result<Amount>> Bet(Amount amount, Colors color)
{
var winColor = (Colors) Random.Shared.Next(2);
var totalWin = (
from a in attempts.SelectValues()
select a.Value).Sum();

await Task.Delay(Random.Shared.Next(1000));

if (winColor == color)
{
Console.WriteLine(" you won :)");
return Result.Success(new Amount(amount.Value * 2));
}
else
{
Console.WriteLine(" you lost :(");
return new LogicError("You lost");
}
}
Console.WriteLine($"You spent: {initialAmount * numberOfAttempts}");
Console.WriteLine($"You 'won': {totalWin}");
}

private static async Task<Result<Amount>> Bet(Amount amount, Colors color)
{
var winColor = (Colors)Random.Shared.Next(2);

await Task.Delay(Random.Shared.Next(1000));

if (winColor == color)
{
Console.WriteLine(" you won :)");
return Result.Success(new Amount(amount.Value * 2));
}
else
{
Console.WriteLine(" you lost :(");
return new LogicError("You lost");
}
}

}
Loading

0 comments on commit 80d8db6

Please sign in to comment.