Skip to content

Commit

Permalink
Removed dependencies. (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored May 26, 2024
1 parent 88d482f commit d6e248a
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="MediatR.Contracts" Version="2.0.*" />
<PackageReference Include="MediatR.Contracts" Version="2.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
122 changes: 122 additions & 0 deletions src/Fluxera.DomainEvents.Abstractions/Guard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
namespace Fluxera.DomainEvents.Abstractions
{
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 @@ -22,13 +22,12 @@
</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="MediatR" Version="12.2.*" />
<PackageReference Include="MediatR" Version="12.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Fluxera.DomainEvents.MediatR
{
using Fluxera.DomainEvents.Abstractions;
using Fluxera.Guards;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand All @@ -19,7 +18,7 @@ public static class ServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddDomainEvents(this IServiceCollection services)
{
services = Guard.Against.Null(services);
services = Guard.ThrowIfNull(services);

// Register the default domain event dispatcher.
services.AddDomainEventDispatcher<MediatrDomainEventDispatcher>();
Expand All @@ -36,7 +35,7 @@ public static IServiceCollection AddDomainEvents(this IServiceCollection service
public static IServiceCollection AddDomainEventDispatcher<TDispatcher>(this IServiceCollection services)
where TDispatcher : MediatrDomainEventDispatcher
{
services = Guard.Against.Null(services);
services = Guard.ThrowIfNull(services);

services.RemoveAll<IDomainEventDispatcher>();
services.AddScoped<IDomainEventDispatcher, TDispatcher>();
Expand Down
3 changes: 1 addition & 2 deletions src/Fluxera.DomainEvents/Fluxera.DomainEvents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
</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.DependencyInjection" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions src/Fluxera.DomainEvents/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Fluxera.DomainEvents
{
using Fluxera.DomainEvents.Abstractions;
using Fluxera.Guards;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand All @@ -23,7 +22,7 @@ public static class ServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddDomainEvents(this IServiceCollection services)
{
services = Guard.Against.Null(services);
services = Guard.ThrowIfNull(services);

// Register the default domain event dispatcher.
services.AddDomainEventDispatcher<DefaultDomainEventDispatcher>();
Expand All @@ -40,7 +39,7 @@ public static IServiceCollection AddDomainEvents(this IServiceCollection service
public static IServiceCollection AddDomainEventDispatcher<TDispatcher>(this IServiceCollection services)
where TDispatcher : DefaultDomainEventDispatcher
{
services = Guard.Against.Null(services);
services = Guard.ThrowIfNull(services);

services.RemoveAll<IDomainEventDispatcher>();
services.AddScoped<IDomainEventDispatcher, TDispatcher>();
Expand All @@ -56,7 +55,7 @@ public static IServiceCollection AddDomainEventDispatcher<TDispatcher>(this ISer
/// <returns></returns>
public static IServiceCollection AddDomainEventHandler<TDomainEventHandler>(this IServiceCollection services)
{
services = Guard.Against.Null(services);
services = Guard.ThrowIfNull(services);

Type type = typeof(TDomainEventHandler);

Expand Down
5 changes: 0 additions & 5 deletions src/Fluxera.Entity/AggregateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Fluxera.ComponentModel.Annotations;
using Fluxera.DomainEvents.Abstractions;
using JetBrains.Annotations;
#if NET6_0
using Fluxera.Utilities.Extensions;
#endif

/// <summary>
/// A base class for aggregate roots.
Expand All @@ -25,7 +21,6 @@ public abstract class AggregateRoot<TAggregateRoot, TKey> : Entity<TAggregateRoo
/// <summary>
/// The domain events of this entity.
/// </summary>
[Ignore]
[IgnoreDataMember]
public IReadOnlyCollection<IDomainEvent> DomainEvents => this.domainEvents.AsReadOnly();

Expand Down
2 changes: 0 additions & 2 deletions src/Fluxera.Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using Fluxera.ComponentModel.Annotations;
using JetBrains.Annotations;

/// <summary>
Expand Down Expand Up @@ -36,7 +35,6 @@ public abstract class Entity<TEntity, TKey>
/// <summary>
/// Gets a flag, if the entity instance is transient (not stored to the storage).
/// </summary>
[Ignore]
[IgnoreDataMember]
public virtual bool IsTransient
{
Expand Down
24 changes: 24 additions & 0 deletions src/Fluxera.Entity/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if NET6_0
namespace Fluxera.Entity
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

internal static class EnumerableExtensions
{
/// <summary>
/// Converts the enumerable to a read-only collection.
/// </summary>
/// <typeparam name="T">The type of the elements.</typeparam>
/// <param name="enumerable">The collection to write-protect.</param>
/// <returns>A write-protected collection.</returns>
public static IReadOnlyCollection<T> AsReadOnly<T>(this IEnumerable<T> enumerable)
{
enumerable = Guard.ThrowIfNull(enumerable);

return new ReadOnlyCollection<T>(enumerable.ToList());
}
}
}
#endif
5 changes: 1 addition & 4 deletions src/Fluxera.Entity/Fluxera.Entity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fluxera.ComponentModel.Annotations" Version="8.0.*" />
<PackageReference Include="Fluxera.Guards" Version="8.0.*" />
<PackageReference Include="Fluxera.StronglyTypedId" 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.DependencyInjection" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit d6e248a

Please sign in to comment.