Skip to content

Commit

Permalink
Support for constraint violation and not found standard error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Jan 20, 2022
1 parent dd5790d commit 302bed0
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.vs/
*bin/
*obj/
*.user
20 changes: 17 additions & 3 deletions YuckQi.Domain.sln
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
# Visual Studio Version 17
VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YuckQi.Domain", "src\YuckQi.Domain\YuckQi.Domain.csproj", "{D1B3FB54-0212-41AE-AB29-30AD97269375}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YuckQi.Domain.Validation", "src\YuckQi.Domain.Validation\YuckQi.Domain.Validation.csproj", "{A620A320-F2E9-43A7-858C-9BAF1D76CD8E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YuckQi.Domain.Services", "src\YuckQi.Domain.Services\YuckQi.Domain.Services.csproj", "{0F24D228-C35A-4DB0-BD64-3E33A3D02B02}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YuckQi.Domain.Services", "src\YuckQi.Domain.Services\YuckQi.Domain.Services.csproj", "{0F24D228-C35A-4DB0-BD64-3E33A3D02B02}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YuckQi.Domain.Validation.UnitTests", "test\YuckQi.Domain.Validation.UnitTests\YuckQi.Domain.Validation.UnitTests.csproj", "{153AC057-A111-4A2D-950B-8FB331A1124E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5B0AF740-A98C-402C-BE34-FD3E95384BA3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Unit Tests", "Unit Tests", "{4012738B-4956-416F-AC49-28E5F00297B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -27,10 +33,18 @@ Global
{0F24D228-C35A-4DB0-BD64-3E33A3D02B02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F24D228-C35A-4DB0-BD64-3E33A3D02B02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F24D228-C35A-4DB0-BD64-3E33A3D02B02}.Release|Any CPU.Build.0 = Release|Any CPU
{153AC057-A111-4A2D-950B-8FB331A1124E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{153AC057-A111-4A2D-950B-8FB331A1124E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{153AC057-A111-4A2D-950B-8FB331A1124E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{153AC057-A111-4A2D-950B-8FB331A1124E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{153AC057-A111-4A2D-950B-8FB331A1124E} = {4012738B-4956-416F-AC49-28E5F00297B3}
{4012738B-4956-416F-AC49-28E5F00297B3} = {5B0AF740-A98C-402C-BE34-FD3E95384BA3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7B79E244-ED98-4357-89F3-0E9586941E8B}
EndGlobalSection
Expand Down
1 change: 1 addition & 0 deletions YuckQi.Domain.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_LOGICAL_NOT_OP/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LINES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
4 changes: 2 additions & 2 deletions src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>0.6.0</Version>
<Version>1.1.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain services project.</Description>
</PropertyGroup>
Expand Down
19 changes: 16 additions & 3 deletions src/YuckQi.Domain.Validation/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Result

public IReadOnlyCollection<ResultDetail> Detail { get; }

public Boolean IsValid => Detail == null || Detail.All(t => t.Type != ResultType.Error);
public Boolean IsValid => Detail != null && Detail.Any(t => t.Type == ResultType.Error);

#endregion

Expand All @@ -19,7 +19,7 @@ public class Result

public Result(IReadOnlyCollection<ResultDetail> detail)
{
Detail = detail;
Detail = detail ?? Array.Empty<ResultDetail>();
}

#endregion
Expand All @@ -36,13 +36,26 @@ public class Result<T> : Result

#region Constructors

public Result(ResultDetail detail) : base(new List<ResultDetail> {detail}) { }
public Result(ResultDetail detail) : base(new List<ResultDetail> { detail }) { }

public Result(T payload, IReadOnlyCollection<ResultDetail> detail = null) : base(detail)
{
Payload = payload;
}

#endregion


#region Public Methods

public static Result<T> ConstraintViolation<TKey>(TKey key, String message = null) where TKey : struct => new Result<T>(ResultDetail.ConstraintViolation<T, TKey>(key, message));

public Boolean IsConstraintConflict => Detail.Any(t => String.Equals(t.Code, ResultCode.ConstraintViolation));

public Boolean IsNotFound => Detail.Any(t => String.Equals(t.Code, ResultCode.NotFound));

public static Result<T> NotFound<TKey>(TKey key, String message = null) where TKey : struct => new Result<T>(ResultDetail.NotFound<T, TKey>(key, message));

#endregion
}
}
2 changes: 2 additions & 0 deletions src/YuckQi.Domain.Validation/ResultCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public readonly struct ResultCode

#region Constants

public static readonly ResultCode ConstraintViolation = new ResultCode(nameof(ConstraintViolation));
public static readonly ResultCode InvalidRequestDetail = new ResultCode(nameof(InvalidRequestDetail));
public static readonly ResultCode NotFound = new ResultCode(nameof(NotFound));

#endregion

Expand Down
8 changes: 8 additions & 0 deletions src/YuckQi.Domain.Validation/ResultDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public class ResultDetail
#endregion


#region Constants

public static ResultDetail ConstraintViolation<T, TKey>(TKey key, String message = null) where TKey : struct => new ResultDetail(ResultCode.ConstraintViolation, ResultMessage.ConstraintConflict<T, TKey>(key, message));
public static ResultDetail NotFound<T, TKey>(TKey key, String message = null) where TKey : struct => new ResultDetail(ResultCode.NotFound, ResultMessage.NotFound<T, TKey>(key, message));

#endregion


#region Properties

public ResultMessage Message { get; }
Expand Down
8 changes: 8 additions & 0 deletions src/YuckQi.Domain.Validation/ResultMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ public ResultMessage(String id, String text = null)
}

#endregion


#region Public Methods

public static ResultMessage ConstraintConflict<T, TKey>(TKey key, String message = null) where TKey : struct => new ResultMessage($"{ResultCode.ConstraintViolation.GetHashCode()}", message ?? $"'{typeof(T).Name}' '{key}' already exists.");
public static ResultMessage NotFound<T, TKey>(TKey key, String message = null) where TKey : struct => new ResultMessage($"{ResultCode.NotFound.GetHashCode()}", message ?? $"'{typeof(T).Name}' '{key}' could not be found.");

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library providing domain validation fundamentals.</Description>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/YuckQi.Domain/YuckQi.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain model project.</Description>
</PropertyGroup>
Expand Down
20 changes: 20 additions & 0 deletions test/YuckQi.Domain.Validation.UnitTests/ResultCodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;

namespace YuckQi.Domain.Validation.UnitTests;

public class ResultCodeTests
{
[SetUp]
public void Setup() { }

[Test]
public void ResultCode_WithSameCode_HasSameHashCode()
{
var a = new ResultCode("test");
var hashCodeA = a.GetHashCode();
var b = new ResultCode("test");
var hashCodeB = b.GetHashCode();

Assert.AreEqual(hashCodeA, hashCodeB);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\YuckQi.Domain.Validation\YuckQi.Domain.Validation.csproj" />
</ItemGroup>

</Project>

0 comments on commit 302bed0

Please sign in to comment.