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,6 +12,11 @@ namespace Bahla.Domain.Builders
=> !string.IsNullOrEmpty(entity._roleName) && !string.IsNullOrWhiteSpace(entity._roleName); => !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 public sealed class RoleBuilder : IRoleBuilder
{ {
@@ -22,14 +27,8 @@ namespace Bahla.Domain.Builders
internal uint _valueMask; internal uint _valueMask;
public Role Build() public Role Build()
{ => new RoleNameIsNotEmptySpecification()
if (new RoleNameIsNotEmptySpecification() .IsSatisfiedBy(this) ? new Role(this) : throw new EntityBuilderException("Role name cannot be null or empty");
.IsSatisfiedBy(this)) {
return new Role(this);
}
throw new EntityBuilderException("Role name cannot be null or empty");
}
public IRoleBuilder WithIdentifier(Identifier identifier) public IRoleBuilder WithIdentifier(Identifier identifier)
{ {

View File

@@ -3,10 +3,10 @@ using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications 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 IEntitySpecification<T> _left = left;
private readonly ISpecification<T> _right = right; private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity) public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) && !_right.IsSatisfiedBy(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 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 IEntitySpecification<T> _left = left;
private readonly ISpecification<T> _right = right; private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity) public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) && _right.IsSatisfiedBy(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 abstract bool IsSatisfiedBy(T entity);
public ISpecification<T> And(ISpecification<T> other)
=> new AndSpecification<T>(this, other); public IEntitySpecification<T> And(IEntitySpecification<T> specification)
public ISpecification<T> AndNot(ISpecification<T> other) => new AndSpecification(this, specification);
=> new AndNotSpecification<T>(this, other);
public ISpecification<T> Not() public IEntitySpecification<T> AndNot(IEntitySpecification<T> specification)
=> new NotSpecification<T>(this); => new AndNotSpecification(this, specification);
public ISpecification<T> Or(ISpecification<T> other) public IEntitySpecification<T> Not()
=> new OrSpecification<T>(this, other); => new NotSpecification(this);
public ISpecification<T> OrNot(ISpecification<T> other)
=> new OrNotSpecification<T>(this, other); 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 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) public override bool IsSatisfiedBy(T entity)
=> !_other.IsSatisfiedBy(entity); => !_other.IsSatisfiedBy(entity);
} }

View File

@@ -3,10 +3,10 @@ using Bahla.Domain.Specifications.Base;
namespace Bahla.Domain.Specifications 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 IEntitySpecification<T> _left = left;
private readonly ISpecification<T> _right = right; private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity) public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) || !_right.IsSatisfiedBy(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 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 IEntitySpecification<T> _left = left;
private readonly ISpecification<T> _right = right; private readonly IEntitySpecification<T> _right = right;
public override bool IsSatisfiedBy(T entity) public override bool IsSatisfiedBy(T entity)
=> _left.IsSatisfiedBy(entity) || _right.IsSatisfiedBy(entity); => _left.IsSatisfiedBy(entity) || _right.IsSatisfiedBy(entity);