From 790b1bd68744beadb703fa76545cd779af80ec2d Mon Sep 17 00:00:00 2001 From: Philippe Matray Date: Mon, 19 Feb 2024 12:37:30 +0100 Subject: [PATCH] use Vogen and Intellenum --- .../GenerateImage/GenerateImageUseCase.cs | 6 +- .../Geometrix.Domain/Cells/CellsCollection.cs | 15 +-- .../Geometrix.Domain/Geometrix.Domain.csproj | 5 + .../Geometrix.Domain/GlobalUsings.cs | 3 + .../Geometrix.Domain/ImageDescription.cs | 19 ++-- .../ValueObjects/CellWidthPixel.cs | 4 + .../ValueObjects/PatternSeed.cs | 3 +- .../Geometrix.Domain/ValueObjects/Settings.cs | 2 +- .../ValueObjects/ThemeColor.cs | 90 +++-------------- .../ValueObjects/TriangleDirection.cs | 96 +++++++------------ .../ImageCreation/TriangleService.cs | 17 ++-- .../Geometrix.WebApi/Geometrix.WebApi.csproj | 1 + .../Geometrix.WebApi/ViewModels/ImageModel.cs | 2 +- geometrix-api/global.json | 4 +- 14 files changed, 84 insertions(+), 183 deletions(-) create mode 100644 geometrix-api/Geometrix.Domain/GlobalUsings.cs create mode 100644 geometrix-api/Geometrix.Domain/ValueObjects/CellWidthPixel.cs diff --git a/geometrix-api/Geometrix.Application/UseCases/GenerateImage/GenerateImageUseCase.cs b/geometrix-api/Geometrix.Application/UseCases/GenerateImage/GenerateImageUseCase.cs index 51dc49e..a9e50dd 100644 --- a/geometrix-api/Geometrix.Application/UseCases/GenerateImage/GenerateImageUseCase.cs +++ b/geometrix-api/Geometrix.Application/UseCases/GenerateImage/GenerateImageUseCase.cs @@ -28,9 +28,9 @@ public Task Execute( includeEmptyAndFill, seed); var imageConfiguration = new Settings( - cellWidthPixel, - new ThemeColor(backgroundColor), - new ThemeColor(foregroundColor)); + CellWidthPixel.From(cellWidthPixel), + ThemeColor.From(backgroundColor), + ThemeColor.From(foregroundColor)); var imageDescription = imageDescriptionFactory .NewImage(pattern, imageConfiguration); diff --git a/geometrix-api/Geometrix.Domain/Cells/CellsCollection.cs b/geometrix-api/Geometrix.Domain/Cells/CellsCollection.cs index 3809998..6bdd6c4 100644 --- a/geometrix-api/Geometrix.Domain/Cells/CellsCollection.cs +++ b/geometrix-api/Geometrix.Domain/Cells/CellsCollection.cs @@ -3,24 +3,17 @@ namespace Geometrix.Domain.Cells; -public sealed class CellsCollection : List +public sealed class CellsCollection(int cellGroupLength) : List { - private readonly int _cellGroupLength; - - public CellsCollection(int cellGroupLength) - { - _cellGroupLength = cellGroupLength; - } - public void FillWithRandomCells(int seed, bool includeEmptyAndFill) { Random random = new(seed); Clear(); - for (var x = 0; x < _cellGroupLength; x++) + for (var x = 0; x < cellGroupLength; x++) { - for (var y = 0; y < _cellGroupLength; y++) + for (var y = 0; y < cellGroupLength; y++) { var triangleDirection = TriangleDirection.CreateRandom(random, includeEmptyAndFill); Cell cell = new(x, y, triangleDirection); @@ -47,7 +40,7 @@ public void ExpandDown(int currentPower) private Cell CreateMirrorCell(Cell original, bool isRight, int currentPower) { - var mirrorFactor = _cellGroupLength * 2.Pow(currentPower) - 1; + var mirrorFactor = cellGroupLength * 2.Pow(currentPower) - 1; var x = isRight ? -original.X + mirrorFactor : original.X; var y = isRight ? original.Y : -original.Y + mirrorFactor; diff --git a/geometrix-api/Geometrix.Domain/Geometrix.Domain.csproj b/geometrix-api/Geometrix.Domain/Geometrix.Domain.csproj index 5601cea..d0aa46f 100644 --- a/geometrix-api/Geometrix.Domain/Geometrix.Domain.csproj +++ b/geometrix-api/Geometrix.Domain/Geometrix.Domain.csproj @@ -11,4 +11,9 @@ en + + + + + \ No newline at end of file diff --git a/geometrix-api/Geometrix.Domain/GlobalUsings.cs b/geometrix-api/Geometrix.Domain/GlobalUsings.cs new file mode 100644 index 0000000..a9abef6 --- /dev/null +++ b/geometrix-api/Geometrix.Domain/GlobalUsings.cs @@ -0,0 +1,3 @@ +// Global using directives + +global using Vogen; \ No newline at end of file diff --git a/geometrix-api/Geometrix.Domain/ImageDescription.cs b/geometrix-api/Geometrix.Domain/ImageDescription.cs index 4d5ddaf..9417032 100644 --- a/geometrix-api/Geometrix.Domain/ImageDescription.cs +++ b/geometrix-api/Geometrix.Domain/ImageDescription.cs @@ -3,25 +3,20 @@ namespace Geometrix.Domain; -public class ImageDescription : IImageDescription +public class ImageDescription(Pattern pattern, Settings settings) + : IImageDescription { - public ImageDescription(Pattern pattern, Settings settings) - { - Pattern = pattern; - Settings = settings; - } - + public Pattern Pattern { get; } = pattern; + public Settings Settings { get; } = settings; + public string Id => $"{Pattern.Id}-{Settings.Id}"; - public Pattern Pattern { get; } - public Settings Settings { get; } - public int WidthPixel => - Pattern.HorizontalCell * Settings.CellWidthPixel; + Pattern.HorizontalCell * Settings.CellWidthPixel.Value; public int HeightPixel => - Pattern.VerticalCell * Settings.CellWidthPixel; + Pattern.VerticalCell * Settings.CellWidthPixel.Value; public void Deconstruct( out Pattern pattern, diff --git a/geometrix-api/Geometrix.Domain/ValueObjects/CellWidthPixel.cs b/geometrix-api/Geometrix.Domain/ValueObjects/CellWidthPixel.cs new file mode 100644 index 0000000..bcd9d9a --- /dev/null +++ b/geometrix-api/Geometrix.Domain/ValueObjects/CellWidthPixel.cs @@ -0,0 +1,4 @@ +namespace Geometrix.Domain.ValueObjects; + +[ValueObject] +public readonly partial struct CellWidthPixel; \ No newline at end of file diff --git a/geometrix-api/Geometrix.Domain/ValueObjects/PatternSeed.cs b/geometrix-api/Geometrix.Domain/ValueObjects/PatternSeed.cs index 7eee783..20a83b6 100644 --- a/geometrix-api/Geometrix.Domain/ValueObjects/PatternSeed.cs +++ b/geometrix-api/Geometrix.Domain/ValueObjects/PatternSeed.cs @@ -1,3 +1,4 @@ namespace Geometrix.Domain.ValueObjects; -public record PatternSeed(string Seed); \ No newline at end of file +[ValueObject] +public readonly partial struct PatternSeed; diff --git a/geometrix-api/Geometrix.Domain/ValueObjects/Settings.cs b/geometrix-api/Geometrix.Domain/ValueObjects/Settings.cs index 08244cb..82ffd16 100644 --- a/geometrix-api/Geometrix.Domain/ValueObjects/Settings.cs +++ b/geometrix-api/Geometrix.Domain/ValueObjects/Settings.cs @@ -1,7 +1,7 @@ namespace Geometrix.Domain.ValueObjects; public record Settings( - int CellWidthPixel, + CellWidthPixel CellWidthPixel, ThemeColor BackgroundColor, ThemeColor ForegroundColor) { diff --git a/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs b/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs index 17dc502..4f6e86c 100644 --- a/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs +++ b/geometrix-api/Geometrix.Domain/ValueObjects/ThemeColor.cs @@ -1,81 +1,13 @@ namespace Geometrix.Domain.ValueObjects; -public readonly struct ThemeColor(string value) - : IEquatable -{ - public string Value { get; } = value; - - public override bool Equals(object? obj) - => obj is ThemeColor o && Equals(o); - - public bool Equals(ThemeColor other) - => Value == other.Value; - - public override int GetHashCode() - => HashCode.Combine(Value); - - public static bool operator ==(ThemeColor left, ThemeColor right) - => left.Equals(right); - - public static bool operator !=(ThemeColor left, ThemeColor right) - => !(left == right); - - /// - /// Light. - /// - /// ThemeColor. - public static readonly ThemeColor Light = new("light"); - - /// - /// Dark. - /// - /// ThemeColor. - public static readonly ThemeColor Dark = new("dark"); - - /// - /// Red. - /// - /// ThemeColor. - public static readonly ThemeColor Red = new("red"); - - /// - /// Yellow. - /// - /// ThemeColor. - public static readonly ThemeColor Yellow = new("yellow"); - - /// - /// Green. - /// - /// ThemeColor. - public static readonly ThemeColor Green = new("green"); - - /// - /// Blue. - /// - /// ThemeColor. - public static readonly ThemeColor Blue = new("blue"); - - /// - /// Indigo. - /// - /// ThemeColor. - public static readonly ThemeColor Indigo = new("indigo"); - - /// - /// Purple. - /// - /// ThemeColor. - public static readonly ThemeColor Purple = new("purple"); - - /// - /// Pink. - /// - /// ThemeColor. - public static readonly ThemeColor Pink = new("pink"); - - public override string ToString() - { - return Value; - } -} \ No newline at end of file +[ValueObject] +[Instance("Light", "light")] +[Instance("Dark", "dark")] +[Instance("Red", "red")] +[Instance("Yellow", "yellow")] +[Instance("Green", "green")] +[Instance("Blue", "blue")] +[Instance("Indigo", "indigo")] +[Instance("Purple", "purple")] +[Instance("Pink", "pink")] +public readonly partial struct ThemeColor; diff --git a/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs b/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs index 6505056..1ad6576 100644 --- a/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs +++ b/geometrix-api/Geometrix.Domain/ValueObjects/TriangleDirection.cs @@ -1,78 +1,50 @@ -namespace Geometrix.Domain.ValueObjects; - -public readonly struct TriangleDirection(TriangleDirection.Direction value) - : IEquatable +using Intellenum; + +namespace Geometrix.Domain.ValueObjects; + +[Intellenum] +[Member("None", 0)] +[Member("TopLeft", 1)] +[Member("TopRight", 2)] +[Member("BottomLeft", 3)] +[Member("BottomRight", 4)] +[Member("Filled", 5)] +public partial class TriangleDirection { - public Direction Value { get; } = value; - - public override bool Equals(object? obj) - => obj is TriangleDirection other && Equals(other); - - public bool Equals(TriangleDirection other) - => Value == other.Value; - - public override int GetHashCode() - => (int) Value; - - public static bool operator ==(TriangleDirection left, TriangleDirection right) - => left.Equals(right); - - public static bool operator !=(TriangleDirection left, TriangleDirection right) - => !(left == right); - - public static readonly TriangleDirection None = new(Direction.None); - public static readonly TriangleDirection TopLeft = new(Direction.TopLeft); - public static readonly TriangleDirection TopRight = new(Direction.TopRight); - public static readonly TriangleDirection BottomLeft = new(Direction.BottomLeft); - public static readonly TriangleDirection BottomRight = new(Direction.BottomRight); - public static readonly TriangleDirection Filled = new(Direction.Filled); - public static TriangleDirection MirrorRight(TriangleDirection direction) - => direction.Value switch + { + return direction.Value switch { - Direction.None => None, - Direction.TopLeft => TopRight, - Direction.TopRight => TopLeft, - Direction.BottomLeft => BottomRight, - Direction.BottomRight => BottomLeft, - Direction.Filled => Filled, + 0 => None, + 1 => TopRight, + 2 => TopLeft, + 3 => BottomRight, + 4 => BottomLeft, + 5 => Filled, _ => throw new ArgumentOutOfRangeException(nameof(direction)) }; + } public static TriangleDirection MirrorDown(TriangleDirection direction) - => direction.Value switch + { + return direction.Value switch { - Direction.None => None, - Direction.TopLeft => BottomLeft, - Direction.TopRight => BottomRight, - Direction.BottomLeft => TopLeft, - Direction.BottomRight => TopRight, - Direction.Filled => Filled, + 0 => None, + 1 => BottomLeft, + 2 => BottomRight, + 3 => TopLeft, + 4 => TopRight, + 5 => Filled, _ => throw new ArgumentOutOfRangeException(nameof(direction)) }; + } public static TriangleDirection CreateRandom(Random random, bool includeEmptyAndFill) { var direction = includeEmptyAndFill - ? (Direction) random.Next(6) - : (Direction) (random.Next(4) + 1); - - var triangleDirection = new TriangleDirection(direction); - return triangleDirection; - } + ? random.Next(6) + : random.Next(4) + 1; - public override string ToString() - { - return $"{nameof(Value)}: {Value}"; - } - - public enum Direction - { - None = 0, - TopLeft = 1, - TopRight = 2, - BottomLeft = 3, - BottomRight = 4, - Filled = 5 + return FromValue(direction); } -} \ No newline at end of file +} diff --git a/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs b/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs index 07a6571..265c250 100644 --- a/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs +++ b/geometrix-api/Geometrix.Infrastructure/ImageCreation/TriangleService.cs @@ -19,12 +19,7 @@ public sealed class TriangleService /// direction public Polygon? GetTriangle(TriangleDirection direction, int x, int y, int cellWidthPixel) { - if (!Enum.IsDefined(typeof(TriangleDirection.Direction), direction.Value)) - { - throw new ArgumentOutOfRangeException(nameof(direction), $"Invalid triangle direction: {direction}"); - } - - return direction.Value == TriangleDirection.Direction.None + return direction.Value == TriangleDirection.None.Value ? null : _triangleFactory.CreateTriangle(direction, x, y, cellWidthPixel); } @@ -36,11 +31,11 @@ public Polygon CreateTriangle(TriangleDirection direction, int x, int y, int cel { return direction.Value switch { - TriangleDirection.Direction.TopLeft => CreateTriangle(new[] { (x, y), (x + cellWidthPixel, y), (x, y + cellWidthPixel) }), - TriangleDirection.Direction.TopRight => CreateTriangle(new[] { (x + cellWidthPixel, y), (x + cellWidthPixel, y + cellWidthPixel), (x, y) }), - TriangleDirection.Direction.BottomLeft => CreateTriangle(new[] { (x, y + cellWidthPixel), (x, y), (x + cellWidthPixel, y + cellWidthPixel) }), - TriangleDirection.Direction.BottomRight => CreateTriangle(new[] { (x + cellWidthPixel, y + cellWidthPixel), (x, y + cellWidthPixel), (x + cellWidthPixel, y) }), - TriangleDirection.Direction.Filled => CreateRectangle(x, y, cellWidthPixel), + 1 => CreateTriangle([(x, y), (x + cellWidthPixel, y), (x, y + cellWidthPixel)]), + 2 => CreateTriangle([(x + cellWidthPixel, y), (x + cellWidthPixel, y + cellWidthPixel), (x, y)]), + 3 => CreateTriangle([(x, y + cellWidthPixel), (x, y), (x + cellWidthPixel, y + cellWidthPixel)]), + 4 => CreateTriangle([(x + cellWidthPixel, y + cellWidthPixel), (x, y + cellWidthPixel), (x + cellWidthPixel, y)]), + 5 => CreateRectangle(x, y, cellWidthPixel), _ => throw new ArgumentException("Invalid triangle direction", nameof(direction)) }; } diff --git a/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj b/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj index 9dd374d..adbed17 100644 --- a/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj +++ b/geometrix-api/Geometrix.WebApi/Geometrix.WebApi.csproj @@ -49,6 +49,7 @@ README.md + \ No newline at end of file diff --git a/geometrix-api/Geometrix.WebApi/ViewModels/ImageModel.cs b/geometrix-api/Geometrix.WebApi/ViewModels/ImageModel.cs index 00b8393..4f4e8cc 100644 --- a/geometrix-api/Geometrix.WebApi/ViewModels/ImageModel.cs +++ b/geometrix-api/Geometrix.WebApi/ViewModels/ImageModel.cs @@ -17,7 +17,7 @@ public ImageModel(ImageDescription imageDescription) CellGroupLength = imageDescription.Pattern.CellGroupLength; HorizontalCell = imageDescription.Pattern.HorizontalCell; VerticalCell = imageDescription.Pattern.VerticalCell; - CellWidthPixel = imageDescription.Settings.CellWidthPixel; + CellWidthPixel = imageDescription.Settings.CellWidthPixel.Value; BackgroundColor = imageDescription.Settings.BackgroundColor.Value; ForegroundColor = imageDescription.Settings.ForegroundColor.Value; } diff --git a/geometrix-api/global.json b/geometrix-api/global.json index dad2db5..1270094 100644 --- a/geometrix-api/global.json +++ b/geometrix-api/global.json @@ -1,7 +1,7 @@ { "sdk": { "version": "8.0.0", - "rollForward": "latestMajor", - "allowPrerelease": true + "rollForward": "feature", + "allowPrerelease": false } } \ No newline at end of file