Skip to content

Release/1.0 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Toolbox.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Modular.Monolithic.Architecture.Helper.Domain.Abstractions;

namespace Modular.Monolithic.Architecture.Helper.Domain.Abstractions
/// <summary>
/// Represents the marker interface for auditable entities.
/// </summary>
public interface IAuditableEntity
{
/// <summary>
/// Represents the marker interface for auditable entities.
/// Gets the created on date and time in UTC format.
/// </summary>
public interface IAuditableEntity
{
/// <summary>
/// Gets the created on date and time in UTC format.
/// </summary>
DateTime CreatedOn { get; }
DateTime CreatedOn { get; }

/// <summary>
/// Gets the modified on date and time in UTC format.
/// </summary>
DateTime? ModifiedOn { get; }
}
/// <summary>
/// Gets the modified on date and time in UTC format.
/// </summary>
DateTime? ModifiedOn { get; }
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Modular.Monolithic.Architecture.Helper.Domain.Abstractions;

namespace Modular.Monolithic.Architecture.Helper.Domain.Abstractions
/// <summary>
/// Represents the marker interface for soft-deletable entities.
/// </summary>
public interface ISoftDeletableEntity
{
/// <summary>
/// Represents the marker interface for soft-deletable entities.
/// Gets the date and time in UTC format the entity was deleted on.
/// </summary>
public interface ISoftDeletableEntity
{
/// <summary>
/// Gets the date and time in UTC format the entity was deleted on.
/// </summary>
DateTime? DeletedOn { get; }
DateTime? DeletedOn { get; }

/// <summary>
/// Gets a value indicating whether the entity has been deleted.
/// </summary>
bool Deleted { get; }
}
/// <summary>
/// Gets a value indicating whether the entity has been deleted.
/// </summary>
bool Deleted { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace Modular.Monolithic.Architecture.Helper.Domain.Abstractions;

/// <summary>
/// Represents the id value entities.
/// </summary>
public abstract class TypedIdValueBase : IEquatable<TypedIdValueBase>
{
/// <summary>
/// Gets or sets the entity identifier.
/// </summary>
public Guid Value { get; private set; }

protected TypedIdValueBase(Guid value)
{
if (value == Guid.Empty)
{
throw new InvalidOperationException("Id value cannot be empty!");
}

Value = value;
}

/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(TypedIdValueBase left, TypedIdValueBase right)
{
if (Object.Equals(left, null))
{
return Object.Equals(right, null);
}
else
{
return left.Equals(right);
}
}

public static bool operator !=(TypedIdValueBase left, TypedIdValueBase right) => !(left == right);

/// <inheritdoc />
public bool IsTransient() => Value == default;

/// <inheritdoc />
public override bool Equals(object obj)
{
if (obj is null)
{
return false;
}

return obj is TypedIdValueBase other && Equals(other);
}
// <inheritdoc />
public bool Equals(TypedIdValueBase other) => Value == other?.Value;
/// <inheritdoc />
public override int GetHashCode() => Value.GetHashCode();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using Modular.Monolithic.Architecture.Helper.Domain.Errors;
using Modular.Monolithic.Architecture.Helper.Domain.Errors;

namespace Modular.Monolithic.Architecture.Helper.Domain.BusinessRules
namespace Modular.Monolithic.Architecture.Helper.Domain.BusinessRules;

public interface IBusinessRule
{
public interface IBusinessRule
{
bool IsFail();
Error Error { get; }
}
bool IsFail();
Error Error { get; }
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Modular.Monolithic.Architecture.Helper.Domain.Core;

namespace Modular.Monolithic.Architecture.Helper.Domain.Core
{
/// <summary>
///
/// </summary>
public abstract class AggregateRoot : Entity { }
}
/// <summary>
///
/// </summary>
public abstract class AggregateRoot : Entity { }
171 changes: 47 additions & 124 deletions src/Modular.Monolithic.Architecture.Helper.Domain/Core/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,135 +1,58 @@
using System;
using System.Collections.Generic;
using Modular.Monolithic.Architecture.Helper.Domain.BusinessRules;
using Modular.Monolithic.Architecture.Helper.Domain.BusinessRules;
using Modular.Monolithic.Architecture.Helper.Domain.Events;
using Modular.Monolithic.Architecture.Helper.Domain.Exceptions;

namespace Modular.Monolithic.Architecture.Helper.Domain.Core
namespace Modular.Monolithic.Architecture.Helper.Domain.Core;

/// <summary>
/// Represents the base class that all entities derive from.
/// </summary>
public abstract class Entity
{

private List<IDomainEvent> _domainEvents;

/// <summary>
/// Represents the base class that all entities derive from.
/// Domain events occurred.
/// </summary>
public abstract class Entity : IEquatable<Entity>
public IReadOnlyCollection<IDomainEvent>? DomainEvents => _domainEvents?.AsReadOnly();
/// <summary>
/// Initializes a new instance of the <see cref="Entity"/> class.
/// </summary>
/// <remarks>
/// Required by EF Core.
/// </remarks>
protected Entity()
{

private List<IDomainEvent> _domainEvents;

/// <summary>
/// Domain events occurred.
/// </summary>
public IReadOnlyCollection<IDomainEvent> DomainEvents => _domainEvents?.AsReadOnly();
/// <summary>
/// Gets or sets the entity identifier.
/// </summary>
public Guid Id { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="Entity"/> class.
/// </summary>
/// <param name="id">The entity identifier.</param>
protected Entity(Guid id)
: this()
{
Id = id;
}
/// <summary>
/// Initializes a new instance of the <see cref="Entity"/> class.
/// </summary>
/// <remarks>
/// Required by EF Core.
/// </remarks>
protected Entity()
{
}
/// <summary>
///
/// </summary>
/// <param name="eventItem"></param>
public void AddDomainEvent(IDomainEvent eventItem)
{
_domainEvents = _domainEvents ?? new List<IDomainEvent>();
_domainEvents.Add(eventItem);
}
/// <summary>
///
/// </summary>
/// <param name="eventItem"></param>
public void RemoveDomainEvent(IDomainEvent eventItem)
{
_domainEvents?.Remove(eventItem);
}
/// <summary>
///
/// </summary>
public void ClearDomainEvents()
{
_domainEvents?.Clear();
}
/// <summary>
///
/// </summary>
/// <param name="rule"></param>
/// <exception cref="BusinessRuleValidationException"></exception>
protected void CheckRule(IBusinessRule rule)
{
if (rule.IsFail())
{
throw new BusinessRuleValidationException(rule);
}
}
/// <summary>
///
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(Entity left, Entity right)
{
if (Object.Equals(left, null))
return (Object.Equals(right, null)) ? true : false;
else
return left.Equals(right);
}

public static bool operator !=(Entity left, Entity right)
{
return !(left == right);
}

/// <inheritdoc />
public bool IsTransient()
{
return this.Id == default;
}
/// <inheritdoc />
public bool Equals(Entity other)
{
if (other is null)
{
return false;
}

return ReferenceEquals(this, other) || Id == other.Id;
}
/// <inheritdoc />
public override bool Equals(object obj)
}
/// <summary>
///
/// </summary>
/// <param name="eventItem"></param>
public void AddDomainEvent(IDomainEvent eventItem)
{
_domainEvents ??= [];
_domainEvents.Add(eventItem);
}
/// <summary>
///
/// </summary>
/// <param name="eventItem"></param>
public void RemoveDomainEvent(IDomainEvent eventItem) => _domainEvents?.Remove(eventItem);
/// <summary>
///
/// </summary>
public void ClearDomainEvents() => _domainEvents?.Clear();
/// <summary>
///
/// </summary>
/// <param name="rule"></param>
/// <exception cref="BusinessRuleValidationException"></exception>
protected void CheckRule(IBusinessRule rule)
{
if (rule.IsFail())
{
if (obj == null || !(obj is Entity))
return false;

if (Object.ReferenceEquals(this, obj))
return true;

if (this.GetType() != obj.GetType())
return false;

Entity item = (Entity)obj;

if (item.IsTransient() || this.IsTransient())
return false;
else
return item.Id == this.Id;
throw new BusinessRuleValidationException(rule);
}
/// <inheritdoc />
public override int GetHashCode() => this.Id.GetHashCode() ^ 31;
}
}
Loading
Loading