Skip to content

Commit

Permalink
Removed dependencies. (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand committed May 26, 2024
1 parent ca4eace commit e793ab2
Show file tree
Hide file tree
Showing 60 changed files with 1,151 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.*" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Fluxera.Extensions.DataManagement.Abstractions\Fluxera.Extensions.DataManagement.Abstractions.csproj" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
</ItemGroup>

</Project>
81 changes: 40 additions & 41 deletions src/Fluxera.Extensions.Caching/DistributedCacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Fluxera.Guards;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Distributed;

Expand All @@ -25,8 +24,8 @@ public static class DistributedCacheExtensions
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<bool?> GetBooleanAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -51,8 +50,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<char?> GetCharAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -77,8 +76,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<decimal?> GetDecimalAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -103,8 +102,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<double?> GetDoubleAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -129,8 +128,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<short?> GetShortAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -155,8 +154,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<int?> GetIntAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -181,8 +180,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<long?> GetLongAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -207,8 +206,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<float?> GetFloatAsync(this IDistributedCache cache, string key)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

byte[] bytes = await cache.GetAsync(key).ConfigureAwait(false);
if(bytes == null)
Expand All @@ -234,8 +233,8 @@ await using(MemoryStream memoryStream = new MemoryStream(bytes))
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<string> GetStringAsync(this IDistributedCache cache, string key, Encoding encoding = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

encoding ??= Encoding.UTF8;

Expand All @@ -255,8 +254,8 @@ public static async Task<string> GetStringAsync(this IDistributedCache cache, st
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static async Task<T> GetAsJsonAsync<T>(this IDistributedCache cache, string key, Encoding encoding = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

encoding ??= Encoding.UTF8;

Expand All @@ -275,8 +274,8 @@ public static async Task<T> GetAsJsonAsync<T>(this IDistributedCache cache, stri
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, bool value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand All @@ -302,8 +301,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, bool value
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, char value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand All @@ -329,8 +328,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, char value
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, decimal value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand All @@ -356,8 +355,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, decimal va
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, double value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand All @@ -383,8 +382,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, double val
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, short value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand All @@ -410,8 +409,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, short valu
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, int value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand All @@ -437,8 +436,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, int value,
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, long value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand All @@ -464,8 +463,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, long value
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, float value, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

options ??= new DistributedCacheEntryOptions();

Expand Down Expand Up @@ -506,8 +505,8 @@ public static Task SetAsync(this IDistributedCache cache, string key, string val
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsync(this IDistributedCache cache, string key, string value, Encoding encoding = null, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

encoding ??= Encoding.UTF8;

Expand Down Expand Up @@ -547,8 +546,8 @@ public static Task SetAsJsonAsync<T>(this IDistributedCache cache, string key, T
/// <exception cref="ArgumentNullException"><paramref name="cache" /> or <paramref name="key" /> is <c>null</c>.</exception>
public static Task SetAsJsonAsync<T>(this IDistributedCache cache, string key, T value, Encoding encoding = null, DistributedCacheEntryOptions options = null)
{
Guard.Against.Null(cache, nameof(cache));
Guard.Against.Null(key, nameof(key));
Guard.ThrowIfNull(cache);
Guard.ThrowIfNull(key);

encoding ??= Encoding.UTF8;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fluxera.Guards" Version="8.0.*" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.*" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.*" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.*" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.*" />
</ItemGroup>

<ItemGroup>
Expand Down
122 changes: 122 additions & 0 deletions src/Fluxera.Extensions.Caching/Guard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
namespace Fluxera.Extensions.Caching
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;

internal static class Guard
{
public static T ThrowIfNull<T>(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
ArgumentNullException.ThrowIfNull(argument, parameterName);

return argument;
}

public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
argument = ThrowIfNull(argument, parameterName);

if(string.IsNullOrEmpty(argument))
{
throw new ArgumentException("Value cannot be empty.", parameterName);
}

return argument;
}

public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null)
{
argument = ThrowIfNull(argument, parameterName);

if(string.IsNullOrWhiteSpace(argument))
{
throw new ArgumentException("Value cannot be whitespace-only.", parameterName);
}

return argument;
}

public static bool ThrowIfFalse(bool argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null, string message = null)
{
if(!argument)
{
throw new ArgumentException(message ?? "Value cannot be false.", parameterName);
}

return true;
}

public static IEnumerable<T> ThrowIfNullOrEmpty<T>(IEnumerable<T> argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
argument = ThrowIfNull(argument, parameterName);

// ReSharper disable PossibleMultipleEnumeration
if(!argument.Any())
{
throw new ArgumentException("Enumerable cannot be empty.", parameterName);
}

return argument;
// ReSharper enable PossibleMultipleEnumeration
}

#if NET7_0_OR_GREATER
public static T ThrowIfNegative<T>(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null)
where T : INumber<T>
{
if(T.IsNegative(argument))
{
throw new ArgumentException("Value cannot be negative.", parameterName);
}

return argument;
}
#endif

#if NET6_0
public static byte ThrowIfNegative(byte argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
if(argument < 0)
{
throw new ArgumentException("Value cannot be negative.", parameterName);
}

return argument;
}

public static short ThrowIfNegative(short argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
if(argument < 0)
{
throw new ArgumentException("Value cannot be negative.", parameterName);
}

return argument;
}

public static int ThrowIfNegative(int argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
if(argument < 0)
{
throw new ArgumentException("Value cannot be negative.", parameterName);
}

return argument;
}

public static long ThrowIfNegative(long argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null)
{
if(argument < 0)
{
throw new ArgumentException("Value cannot be negative.", parameterName);
}

return argument;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
</ItemGroup>

</Project>

Loading

0 comments on commit e793ab2

Please sign in to comment.