Skip to content
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
oskogstad committed Nov 23, 2023
commit 15a5c10d90f19b1343f744bd777174f949493be1
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,27 @@ insert_final_newline = true

[*.cs]

# Identifiers should not contain underscores
dotnet_diagnostic.CA1707.severity = none

# Identifiers should not contain type names
dotnet_diagnostic.CA1720.severity = none

# Specify IFormatProvider
dotnet_diagnostic.CA1305.severity = none

# Identifiers should not match keywords
dotnet_diagnostic.CA1716.severity = none

# Identifiers should not have incorrect suffix
dotnet_diagnostic.CA1711.severity = none

# Style faults should be warnings
dotnet_analyzer_diagnostic.category-Style.severity = warning

#
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity

# Enforce file scoped namespaces
csharp_style_namespace_declarations = file_scoped:suggestion
# Place 'using' directives outside of namespaces
Expand Down
2 changes: 0 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4" PrivateAssets="all" />
</ItemGroup>

<!--
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Comment thread
oskogstad marked this conversation as resolved.
Outdated
</PropertyGroup>
-->

<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ public static IServiceCollection AddApplication(this IServiceCollection services
.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>))
.AddTransient(typeof(IPipelineBehavior<,>), typeof(DomainContextBehaviour<,>));

if (environment.IsDevelopment())
{
var localDeveloperSettings = configuration.GetLocalDevelopmentSettings();
services.Decorate<IUserService, LocalDevelopmentUserServiceDecorator>(
predicate:
localDeveloperSettings.UseLocalDevelopmentUser ||
localDeveloperSettings.UseLocalDevelopmentResourceRegister);
}
if (!environment.IsDevelopment()) return services;

var localDeveloperSettings = configuration.GetLocalDevelopmentSettings();
services.Decorate<IUserService, LocalDevelopmentUserServiceDecorator>(
predicate:
localDeveloperSettings.UseLocalDevelopmentUser ||
localDeveloperSettings.UseLocalDevelopmentResourceRegister);

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Digdir.Domain.Dialogporten.Application;
public sealed class ApplicationSettings
{
public const string ConfigurationSectionName = "Application";

public required DialogportenSettings Dialogporten { get; init; }
}

Expand All @@ -31,4 +31,4 @@ public DialogportenSettingsValidator()
{
RuleFor(x => x.BaseUri).NotEmpty().IsValidUri();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal sealed class DomainContextBehaviour<TRequest, TResponse> : IPipelineBeh

public DomainContextBehaviour(IDomainContext domainContext)
{

_domainContext = domainContext ?? throw new ArgumentNullException(nameof(domainContext));
}

Expand All @@ -38,7 +38,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
{
response = await next();
}
catch (DomainException ex)
catch (DomainException ex)
{
if (OneOfExtensions.TryConvertToOneOf(new DomainError(ex.Errors), out response))
{
Expand All @@ -55,11 +55,8 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe

var domainFailures = _domainContext.Pop();

if (OneOfExtensions.TryConvertToOneOf(new DomainError(domainFailures), out response))
{
return response;
}

throw new DomainException(domainFailures);
return OneOfExtensions.TryConvertToOneOf(new DomainError(domainFailures), out response)
? response
: throw new DomainException(domainFailures);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal static class Expressions
{
internal static Expression<Func<Localization, bool>> LocalizedSearchExpression(string? search, string? cultureCode)
{
return localization =>
(cultureCode == null || localization.CultureCode == cultureCode) &&
return localization =>
(cultureCode == null || localization.CultureCode == cultureCode) &&
EF.Functions.ILike(localization.Value, $"%{search}%");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static bool TryGetOrgNumber(this ClaimsPrincipal claimsPrincipal, [NotNul
}

if (!consumerClaimJson.TryGetValue(AuthorityClaim, out var authority) ||
!string.Equals(authority, AuthorityValue, StringComparison.InvariantCultureIgnoreCase))
!string.Equals(authority, AuthorityValue, StringComparison.InvariantCultureIgnoreCase)) // TODO: Check with B&M
{
return false;
}
Expand All @@ -54,4 +54,4 @@ public static bool TryGetOrgNumber(this ClaimsPrincipal claimsPrincipal, [NotNul

internal static bool TryGetOrgNumber(this IUser user, [NotNullWhen(true)] out string? orgNumber) =>
user.GetPrincipal().TryGetOrgNumber(out orgNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.Enumerable;

internal static class DeleteDelegate
{
#pragma warning disable IDE0060
// ReSharper disable once MethodOverloadWithOptionalParameter
public static Task NoOp<TDestination>(IEnumerable<TDestination> deletables, CancellationToken cancellationToken = default) => Task.CompletedTask;
public static void NoOp<TDestination>(IEnumerable<TDestination> deletables) { /* No operation by design */ }
#pragma warning restore IDE0060

}

internal static class MergeExtensions
Expand Down Expand Up @@ -130,17 +134,17 @@ private static async Task DeleteAsync<TDestination, TSource>(
return;
}

var deleates = destinations
var delegates = destinations
.Except(updateSets.Select(x => x.Destination))
.ToList();

if (deleates.Count == 0)
if (delegates.Count == 0)
{
return;
}

await delete(deleates, cancellationToken);
foreach (var item in deleates)
await delete(delegates, cancellationToken);
foreach (var item in delegates)
{
destinations.Remove(item);
}
Expand Down Expand Up @@ -190,17 +194,17 @@ private static void Delete<TDestination, TSource>(
return;
}

var deleates = destinations
var delegates = destinations
.Except(updateSets.Select(x => x.Destination))
.ToList();

if (deleates.Count == 0)
if (delegates.Count == 0)
{
return;
}

delete(deleates);
foreach (var item in deleates)
delete(delegates);
foreach (var item in delegates)
{
destinations.Remove(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.Enumerable;

internal readonly struct UpdateSet<TDestination, TSource>
internal readonly struct UpdateSet<TDestination, TSource>
{
public TDestination Destination { get; init; }
public TSource Source { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;

internal static class FluentValidation_LocalizationDto_Extensions
internal static class FluentValidationLocalizationDtoExtensions
{
private static readonly string[] AllowedTags = new[] { "p", "a", "br", "em", "strong", "ul", "ol", "li" };
private static readonly string ContainsValidHttpError =
private static readonly string[] AllowedTags = { "p", "a", "br", "em", "strong", "ul", "ol", "li" };
private static readonly string ContainsValidHttpError =
$"{{PropertyName}} contains unsupported html. The following tags are supported: " +
$"[{string.Join(",", AllowedTags.Select(x => '<' + x + '>'))}]. Tag atributes " +
$"[{string.Join(",", AllowedTags.Select(x => '<' + x + '>'))}]. Tag attributes " +
$"are not supported except for on '<a>' which must contain a 'href' starting " +
$"with 'https://'.";

Expand Down Expand Up @@ -55,16 +55,16 @@ private static bool HtmlAgilityPackCheck(this string html)

private static bool IsAnchorTag(this HtmlNode node)
{
const string AnchorTag = "a";
return node.Name == AnchorTag;
const string anchorTag = "a";
return node.Name == anchorTag;
}

private static bool IsValidAnchorTag(this HtmlNode node)
{
const string Https = "https://";
const string Href = "href";
const string https = "https://";
const string href = "href";
return node.Attributes.Count == 1 &&
node.Attributes[Href] is not null &&
node.Attributes[Href].Value.StartsWith(Https);
node.Attributes[href] is not null &&
node.Attributes[href].Value.StartsWith(https, StringComparison.InvariantCultureIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Digdir.Domain.Dialogporten.Application.Common.Extensions.FluentValidation;

public static class FluentValidation_Uri_Extensions
public static class FluentValidationUriExtensions
{
public static IRuleBuilderOptions<T, TUri> IsValidUri<T, TUri>(this IRuleBuilder<T, TUri> ruleBuilder)
where TUri : Uri?
{
return ruleBuilder
.Must(uri => uri is null || uri.IsWellFormedOriginalString())
.WithMessage("'{PropertyName}' is not a well formated URI.");
.WithMessage("'{PropertyName}' is not a well formatted URI.");
}

public static IRuleBuilderOptions<T, TUri> MaximumLength<T, TUri>(this IRuleBuilder<T, TUri> ruleBuilder, int maximumLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ private static IServiceCollection Replace<TService, TImplementation>(
}

public static IServiceCollection Decorate<TService, TDecorator>(this IServiceCollection services, bool predicate)
where TDecorator : TService =>
predicate
? services.Decorate<TService, TDecorator>()
where TDecorator : TService =>
predicate
? services.Decorate<TService, TDecorator>()
: services;

public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services, bool predicate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ public async Task<bool> CurrentUserIsOwner(string serviceResource, CancellationT
return resourceIds.Contains(serviceResource);
}

public Task<IReadOnlyCollection<string>> GetCurrentUserResourceIds(CancellationToken cancellationToken)
{
if (!_user.TryGetOrgNumber(out var orgNumber))
{
throw new UnreachableException();
}

return _resourceRegistry.GetResourceIds(orgNumber, cancellationToken);
}
public Task<IReadOnlyCollection<string>> GetCurrentUserResourceIds(CancellationToken cancellationToken) =>
!_user.TryGetOrgNumber(out var orgNumber)
? throw new UnreachableException()
: _resourceRegistry.GetResourceIds(orgNumber, cancellationToken);
}

internal sealed class LocalDevelopmentUserServiceDecorator : IUserService
Expand All @@ -50,9 +45,9 @@ public LocalDevelopmentUserServiceDecorator(IUserService userService)
_userService = userService ?? throw new ArgumentNullException(nameof(userService));
}

public Task<bool> CurrentUserIsOwner(string serviceResource, CancellationToken cancellationToken) =>
public Task<bool> CurrentUserIsOwner(string serviceResource, CancellationToken cancellationToken) =>
Task.FromResult(true);

public Task<IReadOnlyCollection<string>> GetCurrentUserResourceIds(CancellationToken cancellationToken) =>
public Task<IReadOnlyCollection<string>> GetCurrentUserResourceIds(CancellationToken cancellationToken) =>
_userService.GetCurrentUserResourceIds(cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public static bool TryCalculateControlDigit(ReadOnlySpan<char> number, int[] wei
}

var sum = 0;
for (int i = 0; i < digits.Length; i++)
for (var i = 0; i < digits.Length; i++)
{
sum += digits[i] * weights[i];
}
controlDigit = Mod11Number - sum % Mod11Number;
controlDigit = Mod11Number - (sum % Mod11Number);
return true;
}

Expand All @@ -31,11 +31,11 @@ private static int[] ExtractDigits(this ReadOnlySpan<char> number)
var result = new int[number.Length];
var index = 0;

for (int i = 0; i < number.Length; i++)
foreach (var character in number)
{
if (char.IsDigit(number[i]))
if (char.IsDigit(character))
{
result[index++] = number[i] - '0';
result[index++] = character - '0';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Digdir.Domain.Dialogporten.Application.Common.Pagination;

public class PaginationParameter<TOrderDefinition, TTarget>
public class PaginationParameter<TOrderDefinition, TTarget>
where TOrderDefinition : IOrderDefinition<TTarget>
{
private readonly int _limit = PaginationConstants.DefaultLimit;
Expand All @@ -30,7 +30,7 @@ internal sealed class PaginationParameterValidator<TOrderDefinition, TTarget> :
{
public PaginationParameterValidator()
{
RuleFor(x => x.Limit).InclusiveBetween(PaginationConstants.MinLimit,PaginationConstants.MaxLimit);
RuleFor(x => x.Limit).InclusiveBetween(PaginationConstants.MinLimit, PaginationConstants.MaxLimit);
RuleFor(x => x.ContinuationToken)
.Must((paginationParameter, continuationTokenSet, ctx) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static bool TryParse(string? value, [NotNullWhen(true)] out OrderSet<TOrd
string s => s,
DateTimeOffset d => d.UtcDateTime.ToString("o"),
DateTime d => d.ToString("o"),
object o => o.ToString(),
var o => o.ToString(),
};
return $"{x.Key.ToLowerInvariant()}{PaginationConstants.ContinuationTokenDelimiter}{value}";
});
Expand Down Expand Up @@ -117,9 +117,6 @@ public bool Equals(Order<TTarget>? x, Order<TTarget>? y)
return x.Key == y.Key;
}

public int GetHashCode([DisallowNull] Order<TTarget> obj)
{
return obj.Key.GetHashCode();
}
public int GetHashCode([DisallowNull] Order<TTarget> obj) => obj.Key.GetHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public interface IDialogDbContext
/// <para>True if the property is unmodified or the predicate returns true.</para>
/// </returns>
bool MustWhenModified<TEntity, TProperty>(
TEntity entity,
Expression<Func<TEntity, TProperty>> propertyExpression,
Func<TProperty, bool> predicate)
TEntity entity,
Expression<Func<TEntity, TProperty>> propertyExpression,
Func<TProperty, bool> predicate)
where TEntity : class;
Task<List<Guid>> GetExistingIds<TEntity>(
IEnumerable<TEntity> entities,
CancellationToken cancellationToken)
IEnumerable<TEntity> entities,
CancellationToken cancellationToken)
where TEntity : class, IIdentifiableEntity;
bool TrySetOriginalETag<TEntity>(TEntity entity, Guid? etag) where TEntity : class, IVersionableEntity;
}
Loading