diff --git a/README.md b/README.md index 79248f7..7cd1692 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,6 @@ Templates: For how to install, see:
[![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/Modern.CSharp.Templates?color=gold&label=NuGet:%20Modern.CSharp.Templates&style=plastic)](https://www.nuget.org/packages/Modern.CSharp.Templates) -More templates are coming (in Oct 2022): +More templates are coming (in Nov 2022): - Orleans.Multiservice (use a single Orleans service with multiple logical services inside it as an alternative to separate microservices) - Orleans.Multitenant (uses [Orleans.Multitenant](https://github.com/Applicita/Orleans.Multitenant) to separate grain state storage and grain/stream communication per tenant) diff --git a/src/Modern.CSharp.Templates.csproj b/src/Modern.CSharp.Templates.csproj index 4226194..6ee6f48 100644 --- a/src/Modern.CSharp.Templates.csproj +++ b/src/Modern.CSharp.Templates.csproj @@ -2,7 +2,7 @@ Modern.CSharp.Templates - 1.0.0-rc2 + 1.0.0 Template Modern C# 11 Templates A toolkit of modern dotnet new templates for C# 11, .NET 7 and Microsoft Orleans 7 diff --git a/src/Orleans.Results/.template.config/template.json b/src/Orleans.Results/.template.config/template.json index e965a18..1c171a3 100644 --- a/src/Orleans.Results/.template.config/template.json +++ b/src/Orleans.Results/.template.config/template.json @@ -24,7 +24,7 @@ "description": "Update namespace", "manualInstructions": [ { - "text": "update the Example namespace in the added ErrorCode.cs and Result.cs files to match your project" + "text": "update the Example namespace in the added ErrorNr.cs and Result.cs files to match your project" } ], "actionId": "AC1156F7-BB77-4DB8-B28F-24EEBCCA1E5C", diff --git a/src/Orleans.Results/ErrorCode.cs b/src/Orleans.Results/ErrorNr.cs similarity index 90% rename from src/Orleans.Results/ErrorCode.cs rename to src/Orleans.Results/ErrorNr.cs index 3df839e..a7c5cbf 100644 --- a/src/Orleans.Results/ErrorCode.cs +++ b/src/Orleans.Results/ErrorNr.cs @@ -1,7 +1,7 @@ namespace Example; [Flags] -public enum ErrorCode +public enum ErrorNr { UserNotFound = 1, NoUsersAtAddress = 2, diff --git a/src/Orleans.Results/Result.cs b/src/Orleans.Results/Result.cs index e18e89e..5f69216 100644 --- a/src/Orleans.Results/Result.cs +++ b/src/Orleans.Results/Result.cs @@ -1,5 +1,5 @@ -// Version: 1.0.0-preview.2 (Using https://semver.org/) -// Updated: 2022-08-26 +// Version: 1.0.0 (Using https://semver.org/) +// Updated: 2022-11-10 // See https://github.com/Applicita/Orleans.Results for updates to this file. using System.Collections.Immutable; @@ -9,10 +9,10 @@ namespace Example; /// -/// Result without value; use to return either or (s) +/// Result without value; use to return either or (s) /// [GenerateSerializer, Immutable] -public class Result : ResultBase +public class Result : ResultBase { public static Result Ok { get; } = new(); @@ -22,16 +22,16 @@ public Result(IEnumerable errors) : base(ImmutableArray.CreateRange(error Result(Error error) : base(error) { } public static implicit operator Result(Error error) => new(error); - public static implicit operator Result(ErrorCode code) => new(code); - public static implicit operator Result((ErrorCode code, string message) error) => new(error); + public static implicit operator Result(ErrorNr nr) => new(nr); + public static implicit operator Result((ErrorNr nr, string message) error) => new(error); public static implicit operator Result(List errors) => new(errors); } /// -/// Result with value; use to return either a or (s) +/// Result with value; use to return either a or (s) /// [GenerateSerializer] -public class Result : ResultBase +public class Result : ResultBase { public Result(ImmutableArray errors) : base(errors) { } public Result(IEnumerable errors) : base(ImmutableArray.CreateRange(errors)) { } @@ -40,13 +40,13 @@ public Result(IEnumerable errors) : base(ImmutableArray.CreateRange(error public static implicit operator Result(TValue value) => new(value); public static implicit operator Result(Error error) => new(error); - public static implicit operator Result(ErrorCode code) => new(code); - public static implicit operator Result((ErrorCode code, string message) error) => new(error); + public static implicit operator Result(ErrorNr nr) => new(nr); + public static implicit operator Result((ErrorNr nr, string message) error) => new(error); public static implicit operator Result(List errors) => new(errors); } [GenerateSerializer] -public abstract class ResultBase : ResultBase where TErrorCode : Enum +public abstract class ResultBase : ResultBase where TErrorNr : Enum { [Id(0)] TValue? value; @@ -77,11 +77,13 @@ public TValue Value } } + public override string ToString() => IsSuccess ? $"{Value}" : ErrorsText; + void ThrowIfFailed() { if (IsFailed) throw new InvalidOperationException("Attempt to access the value of a failed result"); } } [GenerateSerializer] -public abstract class ResultBase where TErrorCode : Enum +public abstract class ResultBase where TErrorNr : Enum { public bool IsSuccess => !IsFailed; public bool IsFailed => errors?.Length > 0; @@ -95,9 +97,9 @@ public abstract class ResultBase where TErrorCode : Enum public ImmutableArray Errors => errors ?? throw new InvalidOperationException("Attempt to access the errors of a success result"); /// - /// Returns the errorcode for a failed result with a single error; otherwise throws an exception + /// Returns the error nr for a failed result with a single error; otherwise throws an exception /// - public TErrorCode ErrorCode => Errors.Single().Code; + public TErrorNr ErrorNr => Errors.Single().Nr; /// /// Returns all errors formatted in a single string for a failed result; throws an for a success result @@ -110,14 +112,14 @@ public abstract class ResultBase where TErrorCode : Enum /// Intended for use with (in MVC controllers) or (in minimal api's) /// The enum flag used to identify an error as a validation error /// If the return value is true, receives all errors in a dictionary suitable for serializing into a https://tools.ietf.org/html/rfc7807 based format; otherwise set to null - /// True for a failed result that has the set in the for all errors; false otherwise + /// True for a failed result that has the set in the for all errors; false otherwise [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0001:Simplify Names", Justification = "Full name is necessary to ensure link works independently of global usings")] - public bool TryAsValidationErrors(TErrorCode validationErrorFlag, [NotNullWhen(true)] out Dictionary? validationErrors) + public bool TryAsValidationErrors(TErrorNr validationErrorFlag, [NotNullWhen(true)] out Dictionary? validationErrors) { - if (IsFailed && Errors.All(error => error.Code.HasFlag(validationErrorFlag))) + if (IsFailed && Errors.All(error => error.Nr.HasFlag(validationErrorFlag))) { validationErrors = new(Errors - .GroupBy(error => error.Code, error => error.Message) + .GroupBy(error => error.Nr, error => error.Message) .Select(group => new KeyValuePair(group.Key.ToString(), group.ToArray()))); return true; } @@ -125,6 +127,8 @@ public bool TryAsValidationErrors(TErrorCode validationErrorFlag, [NotNullWhen(t return false; } + public override string ToString() => IsSuccess ? nameof(Result.Ok) : ErrorsText; + protected ResultBase() { } protected ResultBase(Error error) => errors = ImmutableArray.Create(error); protected ResultBase(ImmutableArray errors) => this.errors = errors; @@ -133,9 +137,9 @@ protected ResultBase() { } public NotImplementedException UnhandledErrorException(string? message = null) => new($"{message}Unhandled error(s): " + ErrorsText); [GenerateSerializer, Immutable] - public record Error([property: Id(0)] TErrorCode Code, [property: Id(1)] string Message = "") + public record Error([property: Id(0)] TErrorNr Nr, [property: Id(1)] string Message = "") { - public static implicit operator Error(TErrorCode code) => new(code); - public static implicit operator Error((TErrorCode code, string message) error) => new(error.code, error.message); + public static implicit operator Error(TErrorNr nr) => new(nr); + public static implicit operator Error((TErrorNr nr, string message) error) => new(error.nr, error.message); } } diff --git a/src/Readme.md b/src/Readme.md index 675682a..1118bac 100644 --- a/src/Readme.md +++ b/src/Readme.md @@ -6,4 +6,4 @@ Templates: Docs: see the [repo readme](https://github.com/Applicita/Modern.CSharp.Templates#readme) -[Release Notes](https://github.com/Applicita/Modern.CSharp.Templates/releases/tag/1-0-0-rc2) \ No newline at end of file +[Release Notes](https://github.com/Applicita/Modern.CSharp.Templates/releases/tag/1-0-0) \ No newline at end of file