diff --git a/Backbone.Tests.ArchUnit/DomainDrivenDesign.cs b/Backbone.Tests.ArchUnit/DomainDrivenDesign.cs new file mode 100644 index 0000000000..6799f2fb5a --- /dev/null +++ b/Backbone.Tests.ArchUnit/DomainDrivenDesign.cs @@ -0,0 +1,28 @@ +using ArchUnitNET.Domain.Extensions; +using ArchUnitNET.Fluent.Conditions; +using ArchUnitNET.xUnit; +using Backbone.BuildingBlocks.Domain; +using static ArchUnitNET.Fluent.ArchRuleDefinition; + +namespace Backbone.Backbone.Tests.ArchUnit; + +public class DomainDrivenDesign +{ + [Fact] + public void EntitiesShouldHaveEmptyDefaultConstructors() + { + Types() + .That().AreAssignableTo(typeof(Entity)) + .Should().FollowCustomCondition((type) => + { + var constructors = type.GetConstructors(); + + if (constructors.All(c => c.Parameters.Count() != 0)) + return new ConditionResult(type, false, "Entity should have a parameterless constructor"); + + return new ConditionResult(type, true); + }, "") + .Because("otherwise the real constructor would be called by EF Core, which would add a domain event to the collection") + .Check(Backbone.ARCHITECTURE); + } +}