feat: base domain

This commit is contained in:
Kylnic28
2024-10-08 20:54:40 +02:00
parent 17a7e651c8
commit 6fba038bfb
9 changed files with 60 additions and 50 deletions

View File

@@ -12,9 +12,14 @@ namespace Bahla.Domain.Builders
=> !string.IsNullOrEmpty(entity._roleName) && !string.IsNullOrWhiteSpace(entity._roleName);
}
internal sealed class RoleValueMaskIsNotZeroSpecification : CompositeSpecification<RoleBuilder>
{
public override bool IsSatisfiedBy(RoleBuilder entity)
=> entity._valueMask != 0;
}
public sealed class RoleBuilder : IRoleBuilder
{
{
internal Identifier _identifier = Identifier.Generate;
internal string _roleName = string.Empty;
@@ -22,14 +27,8 @@ namespace Bahla.Domain.Builders
internal uint _valueMask;
public Role Build()
{
if (new RoleNameIsNotEmptySpecification()
.IsSatisfiedBy(this)) {
return new Role(this);
}
throw new EntityBuilderException("Role name cannot be null or empty");
}
=> new RoleNameIsNotEmptySpecification()
.IsSatisfiedBy(this) ? new Role(this) : throw new EntityBuilderException("Role name cannot be null or empty");
public IRoleBuilder WithIdentifier(Identifier identifier)
{

View File

@@ -3,10 +3,10 @@ using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications
{
public sealed class AndNotSpecification<T>(ISpecification<T> left, ISpecification<T> right) : CompositeSpecification<T> where T : class
public sealed class AndNotSpecification<T>(IEntitySpecification<T> left, IEntitySpecification<T> right) : CompositeSpecification<T> where T : IEntity
{
private readonly ISpecification<T> _left = left;
private readonly ISpecification<T> _right = right;
private readonly IEntitySpecification<T> _left = left;
private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) && !_right.IsSatisfiedBy(entity);

View File

@@ -1,11 +1,12 @@
using Bahla.Domain.Specifications.Base;
using Bahla.Domain.Entities.Base;
using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications
{
public sealed class AndSpecification<T>(ISpecification<T> left, ISpecification<T> right) : CompositeSpecification<T> where T : class
public sealed class AndSpecification<T>(IEntitySpecification<T> left, IEntitySpecification<T> right) : CompositeSpecification<T> where T : IEntity
{
private readonly ISpecification<T> _left = left;
private readonly ISpecification<T> _right = right;
private readonly IEntitySpecification<T> _left = left;
private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) && _right.IsSatisfiedBy(entity);
}

View File

@@ -1,17 +1,24 @@
namespace Bahla.Domain.Specifications.Base
using Bahla.Domain.Entities.Base;
using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications
{
public abstract class CompositeSpecification<T> : ISpecification<T> where T : class
public abstract class CompositeSpecification<T> : IEntitySpecification<T> where T : IEntity
{
public abstract bool IsSatisfiedBy(T entity);
public ISpecification<T> And(ISpecification<T> other)
=> new AndSpecification<T>(this, other);
public ISpecification<T> AndNot(ISpecification<T> other)
=> new AndNotSpecification<T>(this, other);
public ISpecification<T> Not()
=> new NotSpecification<T>(this);
public ISpecification<T> Or(ISpecification<T> other)
=> new OrSpecification<T>(this, other);
public ISpecification<T> OrNot(ISpecification<T> other)
=> new OrNotSpecification<T>(this, other);
public IEntitySpecification<T> And(IEntitySpecification<T> specification)
=> new AndSpecification(this, specification);
public IEntitySpecification<T> AndNot(IEntitySpecification<T> specification)
=> new AndNotSpecification(this, specification);
public IEntitySpecification<T> Not()
=> new NotSpecification(this);
public IEntitySpecification<T> Or(IEntitySpecification<T> specification)
=> new OrSpecification(this, specification);
IEntitySpecification<T> OrNot(IEntitySpecification<T> specification)
=> new OrNotSpecification(this, specification);
}
}

View File

@@ -0,0 +1,15 @@
using Bahla.Domain.Entities.Base;
namespace Bahla.Domain.Specifications.Base
{
public interface IEntitySpecification<T> where T : IEntity
{
bool IsSatisfiedBy(T entity);
IEntitySpecification<T> And(IEntitySpecification<T> other);
IEntitySpecification<T> AndNot(IEntitySpecification<T> other);
IEntitySpecification<T> Or(IEntitySpecification<T> other);
IEntitySpecification<T> OrNot(IEntitySpecification<T> other);
IEntitySpecification<T> Not();
}
}

View File

@@ -1,13 +0,0 @@
namespace Bahla.Domain.Specifications.Base
{
public interface ISpecification<T> where T : class
{
bool IsSatisfiedBy(T entity);
ISpecification<T> And(ISpecification<T> other);
ISpecification<T> AndNot(ISpecification<T> other);
ISpecification<T> Or(ISpecification<T> other);
ISpecification<T> OrNot(ISpecification<T> other);
ISpecification<T> Not();
}
}

View File

@@ -3,9 +3,9 @@ using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications
{
public sealed class NotSpecification<T>(ISpecification<T> other) : CompositeSpecification<T> where T : class
public sealed class NotSpecification<T>(IEntitySpecification<T> other) : CompositeSpecification<T> where T : IEntity
{
private readonly ISpecification<T> _other = other;
private readonly IEntitySpecification<T> _other = other;
public override bool IsSatisfiedBy(T entity)
=> !_other.IsSatisfiedBy(entity);
}

View File

@@ -3,10 +3,10 @@ using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications
{
public sealed class OrNotSpecification<T>(ISpecification<T> left, ISpecification<T> right) : CompositeSpecification<T> where T : class
public sealed class OrNotSpecification<T>(IEntitySpecification<T> left, IEntitySpecification<T> right) : CompositeSpecification<T> where T : IEntity
{
private readonly ISpecification<T> _left = left;
private readonly ISpecification<T> _right = right;
private readonly IEntitySpecification<T> _left = left;
private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) || !_right.IsSatisfiedBy(entity);

View File

@@ -1,11 +1,12 @@
using Bahla.Domain.Specifications.Base;
using Bahla.Domain.Entities.Base;
using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications
{
public sealed class OrSpecification<T>(ISpecification<T> left, ISpecification<T> right) : CompositeSpecification<T> where T : class
public sealed class OrSpecification<T>(IEntitySpecification<T> left, IEntitySpecification<T> right) : CompositeSpecification<T> where T : IEntity
{
private readonly ISpecification<T> _left = left;
private readonly ISpecification<T> _right = right;
private readonly IEntitySpecification<T> _left = left;
private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) || _right.IsSatisfiedBy(entity);