Skip to content

Commit

Permalink
Merge branch 'revisions/dotnet6' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuck committed Mar 22, 2023
2 parents 7e29b88 + 57c0cf4 commit e88e34f
Show file tree
Hide file tree
Showing 36 changed files with 556 additions and 568 deletions.
9 changes: 8 additions & 1 deletion YuckQi.Domain.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YuckQi.Domain.Validation",
EndProject
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}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YuckQi.Domain.UnitTests", "test\YuckQi.Domain.UnitTests\YuckQi.Domain.UnitTests.csproj", "{EA0EA1BA-AC4C-4D6C-839F-BF0F60A40346}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -37,13 +39,18 @@ Global
{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
{EA0EA1BA-AC4C-4D6C-839F-BF0F60A40346}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA0EA1BA-AC4C-4D6C-839F-BF0F60A40346}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA0EA1BA-AC4C-4D6C-839F-BF0F60A40346}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA0EA1BA-AC4C-4D6C-839F-BF0F60A40346}.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}
{EA0EA1BA-AC4C-4D6C-839F-BF0F60A40346} = {4012738B-4956-416F-AC49-28E5F00297B3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7B79E244-ED98-4357-89F3-0E9586941E8B}
Expand Down
17 changes: 8 additions & 9 deletions src/YuckQi.Domain.Services/Abstract/ITypeEntityService.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using YuckQi.Domain.Aspects.Abstract;
using YuckQi.Domain.Entities.Abstract;
using YuckQi.Domain.Services.Models;
using YuckQi.Domain.Validation;
using YuckQi.Domain.ValueObjects.Abstract;

namespace YuckQi.Domain.Services.Abstract
namespace YuckQi.Domain.Services.Abstract;

public interface ITypeEntityService<TTypeEntity, in TIdentifier> where TTypeEntity : IEntity<TIdentifier>, IType where TIdentifier : IEquatable<TIdentifier>
{
public interface ITypeEntityService<TTypeEntity, in TKey> where TTypeEntity : IEntity<TKey>, IType where TKey : struct
{
Task<Result<TTypeEntity>> CreateAsync(TTypeEntity entity);
Task<Result<TTypeEntity>> GetAsync(Guid identifier);
Task<Result<TTypeEntity>> GetAsync(TKey key);
Task<Result<TTypeEntity>> ModifyAsync(TTypeEntity entity);
Task<Result<IPage<TTypeEntity>>> SearchAsync(TypeSearchCriteria criteria = null);
}
Task<Result<TTypeEntity>> Create(TTypeEntity entity, CancellationToken cancellationToken = default);
Task<Result<TTypeEntity>> Get(TIdentifier identifier, CancellationToken cancellationToken = default);
Task<Result<TTypeEntity>> Modify(TTypeEntity entity, CancellationToken cancellationToken = default);
Task<Result<IPage<TTypeEntity>>> Search(TypeSearchCriteria? criteria = null, CancellationToken cancellationToken = default);
}
13 changes: 6 additions & 7 deletions src/YuckQi.Domain.Services/Models/TypeSearchCriteria.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using YuckQi.Domain.ValueObjects.Abstract;

namespace YuckQi.Domain.Services.Models
namespace YuckQi.Domain.Services.Models;

public class TypeSearchCriteria : IPage
{
public class TypeSearchCriteria : IPage
{
public String Name { get; set; }
public Int32 PageNumber { get; set; }
public Int32 PageSize { get; set; }
}
public String Name { get; set; }
public Int32 PageNumber { get; set; }
public Int32 PageSize { get; set; }
}
7 changes: 4 additions & 3 deletions src/YuckQi.Domain.Services/YuckQi.Domain.Services.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kevin J Lambert</Authors>
<Version>1.3.1</Version>
<Version>6.2.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Description>A .NET library for bootstrapping a domain services project.</Description>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentValidation;
using FluentValidation.Results;

namespace YuckQi.Domain.Validation.Extensions
namespace YuckQi.Domain.Validation.Extensions;

public static class AbstractValidatorExtensions
{
public static class AbstractValidatorExtensions
public static Result<T> GetResult<T>(this AbstractValidator<T> validator, T item)
{
public static Result<T> GetResult<T>(this AbstractValidator<T> validator, T item, String failedValidationMessageId)
{
var validationResult = validator.Validate(item);
var result = BuildResult(validationResult, item, failedValidationMessageId);
if (validator == null)
throw new ArgumentNullException(nameof(validator));

return result;
}
var validationResult = validator.Validate(item);
var result = BuildResult(validationResult, item);

public static async Task<Result<T>> GetResultAsync<T>(this AbstractValidator<T> validator, T item, String failedValidationMessageId)
{
var validationResult = await validator.ValidateAsync(item);
var result = BuildResult(validationResult, item, failedValidationMessageId);
return result;
}

return result;
}
public static async Task<Result<T>> GetResult<T>(this AbstractValidator<T> validator, T item, CancellationToken cancellationToken)
{
if (validator == null)
throw new ArgumentNullException(nameof(validator));

private static Result<T> BuildResult<T>(ValidationResult validationResult, T item, String failedValidationMessageId)
{
if (validationResult == null)
throw new ArgumentNullException(nameof(validationResult));
var validationResult = await validator.ValidateAsync(item, cancellationToken);
var result = BuildResult(validationResult, item);

if (validationResult.IsValid)
return new Result<T>(item);
return result;
}

return new Result<T>(default, GetResultDetail(validationResult, failedValidationMessageId));
}
private static Result<T> BuildResult<T>(ValidationResult validationResult, T item)
{
if (validationResult == null)
throw new ArgumentNullException(nameof(validationResult));

private static IReadOnlyCollection<ResultDetail> GetResultDetail(ValidationResult result, String failedValidationMessageId)
{
return result?.Errors.Select(t => new ResultDetail(ResultCode.InvalidRequestDetail, new ResultMessage(failedValidationMessageId, t.ErrorMessage), t.PropertyName)).ToList();
}
if (validationResult.IsValid)
return new Result<T>(item);

return new Result<T>(default, GetResultDetail(validationResult));
}

private static IReadOnlyCollection<ResultDetail> GetResultDetail(ValidationResult result)
{
return result.Errors.Select(t => new ResultDetail(new ResultMessage(t.ErrorMessage), ResultCode.InvalidRequestDetail, t.PropertyName)).ToList();
}
}
66 changes: 21 additions & 45 deletions src/YuckQi.Domain.Validation/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,36 @@
using System.Collections.Generic;
using System.Linq;

namespace YuckQi.Domain.Validation
{
public class Result
{
#region Properties

public IReadOnlyCollection<ResultDetail> Detail { get; }

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

#endregion

namespace YuckQi.Domain.Validation;

#region Constructors

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

#endregion
}
public Boolean IsValid => Detail.All(t => t.Type != ResultType.Error);

public class Result<T> : Result
public Result(IReadOnlyCollection<ResultDetail>? detail)
{
#region Properties

public T Payload { get; }

#endregion


#region Constructors

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

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

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

#endregion
public class Result<T> : Result
{
public T? Payload { get; }

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

#region Public Methods
public Result(IReadOnlyCollection<ResultDetail> detail) : base(detail) { }

public static Result<T> ConstraintViolation<TKey>(TKey key, String message = null) where TKey : struct => new Result<T>(ResultDetail.ConstraintViolation<T, TKey>(key, message));
public Result(T? payload, IReadOnlyCollection<ResultDetail>? detail = null) : base(detail)
{
Payload = payload;
}

public Boolean HasResultCode(ResultCode resultCode) => Detail.Any(t => t.Code == resultCode);
public static Result<T> ConstraintViolation<TIdentifier>(TIdentifier identifier, String? message = null) where TIdentifier : IEquatable<TIdentifier> => new(ResultDetail.ConstraintViolation<T, TIdentifier>(identifier, message));

public static Result<T> NotFound<TKey>(TKey key, String message = null) where TKey : struct => new Result<T>(ResultDetail.NotFound<T, TKey>(key, message));
public Boolean HasResultCode(ResultCode resultCode) => Detail.Any(t => t.Code == resultCode);

#endregion
}
public static Result<T> NotFound<TIdentifier>(TIdentifier identifier, String? message = null) where TIdentifier : IEquatable<TIdentifier> => new(ResultDetail.NotFound<T, TIdentifier>(identifier, message));
}
61 changes: 23 additions & 38 deletions src/YuckQi.Domain.Validation/ResultCode.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,35 @@
using System;

namespace YuckQi.Domain.Validation
{
public readonly struct ResultCode
{
#region Private Members

private readonly String _code;

#endregion

namespace YuckQi.Domain.Validation;

#region Implicit Operators

public static implicit operator String(ResultCode resultCode) => resultCode._code;

#endregion


#region Constants
public readonly struct ResultCode
{
private readonly String _code;

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));
public static implicit operator String(ResultCode code) => code._code;

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

public ResultCode() : this(Guid.NewGuid().ToString()) { }

#region Constructors
public ResultCode(String code)
{
_code = code;
}

public ResultCode(String code)
public override Boolean Equals(Object? obj)
{
return obj switch
{
_code = code;
}

#endregion


#region Public Methods

public override Boolean Equals(Object obj) => obj != null && String.Equals(this, (ResultCode) obj);

public override Int32 GetHashCode() => _code.GetHashCode();
ResultCode other => String.Equals(this, other),
String other => String.Equals(this, other),
_ => false
};
}

public override String ToString() => _code;
public override Int32 GetHashCode() => _code.GetHashCode();

#endregion
}
public override String ToString() => _code;
}
Loading

0 comments on commit e88e34f

Please sign in to comment.