Skip to content

Commit

Permalink
Remove aggregate root as a programming concept (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Feb 5, 2025
1 parent 11f0510 commit 36f8f32
Show file tree
Hide file tree
Showing 37 changed files with 88 additions and 197 deletions.
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
next-version: 9.0.0
next-version: 9.1.0
6 changes: 5 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(CurrentYear)' == ''">
<CurrentYear>$([System.DateTime]::UtcNow.Year)</CurrentYear>
</PropertyGroup>

<PropertyGroup>
<Company>Fluxera Software Development GmbH</Company>
<Product>Fluxera Software Foundation</Product>
<Copyright>Copyright © 2014-2024 Fluxera Software Development GmbH. All rights reserved.</Copyright>
<Copyright>Copyright © 2014-$(CurrentYear) Fluxera Software Development GmbH. All rights reserved.</Copyright>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="6.0.5">
<PackageReference Include="GitVersion.MsBuild" Version="6.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="6.0.5">
<PackageReference Include="GitVersion.MsBuild" Version="6.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions src/Fluxera.DomainEvents/Fluxera.DomainEvents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="6.0.5">
<PackageReference Include="GitVersion.MsBuild" Version="6.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
46 changes: 0 additions & 46 deletions src/Fluxera.Entity/AggregateRoot.cs

This file was deleted.

3 changes: 1 addition & 2 deletions src/Fluxera.Entity/DomainSignatureAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
/// An attribute to indicate which property(s) describe the unique signature of an entity.
/// </summary>
/// <remarks>
/// This is intended for use with <see cref="Entity{TEntity,TKey}" /> and
/// <see cref="AggregateRoot{TAggregateRoot, TKey}" />.
/// This is intended for use with <see cref="Entity{TEntity,TKey}" /> types.
/// </remarks>
[PublicAPI]
[AttributeUsage(AttributeTargets.Property)]
Expand Down
27 changes: 27 additions & 0 deletions src/Fluxera.Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using Fluxera.DomainEvents.Abstractions;
using JetBrains.Annotations;

/// <summary>
Expand All @@ -27,13 +28,22 @@ public abstract class Entity<TEntity, TKey>
/// </remarks>
private const int HashMultiplier = 37;

private readonly IList<IDomainEvent> domainEvents = [];

/// <summary>
/// The unique ID of the entity.
/// </summary>
[Key]
[JsonPropertyOrder(int.MinValue)]
public virtual TKey ID { get; set; }

/// <summary>
/// The domain events of this entity.
/// </summary>
[JsonIgnore]
[IgnoreDataMember]
public IReadOnlyCollection<IDomainEvent> DomainEvents => this.domainEvents.AsReadOnly();

/// <summary>
/// Gets a flag, if the entity instance is transient (not stored to the storage).
/// </summary>
Expand All @@ -57,6 +67,23 @@ public virtual bool IsTransient
}
}

/// <summary>
/// Adds the given event to the list of raised domain events.
/// </summary>
/// <param name="domainEvent"></param>
public void RaiseDomainEvent(IDomainEvent domainEvent)
{
this.domainEvents.Add(domainEvent);
}

/// <summary>
/// Clears the list of raised domain events.
/// </summary>
public void ClearDomainEvents()
{
this.domainEvents.Clear();
}

/// <summary>
/// Checks two instances of <see cref="Entity{TEntity,TKey}" /> are equal.
/// </summary>
Expand Down
20 changes: 1 addition & 19 deletions src/Fluxera.Entity/EntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,9 @@ public static bool IsEntity(this Type type)
}
}

/// <summary>
/// Checks if the given type is a <see cref="AggregateRoot{TAggregateRoot, TKey}" />.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns><c>true</c> if the type is an aggregate root; <c>false</c> otherwise.</returns>
public static bool IsAggregateRoot(this Type type)
{
try
{
bool isSubclassOf = type.IsSubclassOfRawGeneric(typeof(AggregateRoot<,>));
return isSubclassOf && !type.IsInterface && !type.IsAbstract;
}
catch
{
return false;
}
}

private static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic)
{
while((toCheck != null) && (toCheck != typeof(object)))
while(toCheck != null && toCheck != typeof(object))
{
Type cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if(generic == cur)
Expand Down
24 changes: 0 additions & 24 deletions src/Fluxera.Entity/EnumerableExtensions.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Fluxera.Entity/Fluxera.Entity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="6.0.5">
<PackageReference Include="GitVersion.MsBuild" Version="6.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
46 changes: 0 additions & 46 deletions src/Fluxera.Entity/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if NET7_0_OR_GREATER
using System.Numerics;
#endif
using System.Runtime.CompilerServices;
using JetBrains.Annotations;

Expand Down Expand Up @@ -66,7 +64,6 @@ public static IEnumerable<T> ThrowIfNullOrEmpty<T>(IEnumerable<T> argument, [Inv
// 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>
{
Expand All @@ -77,48 +74,5 @@ public static T ThrowIfNegative<T>(T argument, [InvokerParameterName] [CallerArg

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
}
}
6 changes: 5 additions & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(CurrentYear)' == ''">
<CurrentYear>$([System.DateTime]::UtcNow.Year)</CurrentYear>
</PropertyGroup>

<PropertyGroup>
<Company>Fluxera Software Development GmbH</Company>
<Product>Fluxera Software Foundation</Product>
<Copyright>Copyright © 2014-2024 Fluxera Software Development GmbH. All rights reserved.</Copyright>
<Copyright>Copyright © 2014-$(CurrentYear) Fluxera Software Development GmbH. All rights reserved.</Copyright>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion tests/Fluxera.DomainEvents.UnitTests/DomainEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using FluentAssertions;
using Fluxera.DomainEvents.Abstractions;
using Fluxera.DomainEvents.MediatR;
using Fluxera.DomainEvents.UnitTests.EmployeeAggregate;
using Fluxera.DomainEvents.UnitTests.Employees;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Fluxera.DomainEvents.UnitTests.EmployeeAggregate
namespace Fluxera.DomainEvents.UnitTests.Employees
{
using System.Threading;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Fluxera.DomainEvents.UnitTests.EmployeeAggregate
namespace Fluxera.DomainEvents.UnitTests.Employees
{
using System.Collections.Generic;
using Fluxera.Entity;
using Fluxera.Guards;
using JetBrains.Annotations;

[PublicAPI]
public class Employee : AggregateRoot<Employee, EmployeeId>
public class Employee : Entity<Employee, EmployeeId>
{
[DomainSignature]
public string Name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Fluxera.DomainEvents.UnitTests.EmployeeAggregate
namespace Fluxera.DomainEvents.UnitTests.Employees
{
using System;
using Fluxera.Entity;
using JetBrains.Annotations;

[PublicAPI]
public class EmployeeGuid : AggregateRoot<EmployeeGuid, Guid>
public class EmployeeGuid : Entity<EmployeeGuid, Guid>
{
[DomainSignature]
public string Name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Fluxera.Entity.UnitTests.EmployeeAggregate
namespace Fluxera.DomainEvents.UnitTests.Employees
{
using Fluxera.StronglyTypedId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Fluxera.DomainEvents.UnitTests.EmployeeAggregate
namespace Fluxera.DomainEvents.UnitTests.Employees
{
using System.Collections.Generic;
using Fluxera.Entity;
using JetBrains.Annotations;

[PublicAPI]
public class EmployeeStringId : AggregateRoot<EmployeeStringId, string>
public class EmployeeStringId : Entity<EmployeeStringId, string>
{
[DomainSignature]
public string Name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Fluxera.DomainEvents.UnitTests.EmployeeAggregate
namespace Fluxera.DomainEvents.UnitTests.Employees
{
using Fluxera.Entity;
using JetBrains.Annotations;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Fluxera.Entity.UnitTests.EmployeeAggregate
namespace Fluxera.DomainEvents.UnitTests.Employees
{
using System.Collections.Generic;
using Fluxera.DomainEvents.Abstractions;
Expand Down
Loading

0 comments on commit 36f8f32

Please sign in to comment.