Adds the base class of all metadata objects

This commit is contained in:
BinaryAlley committed 2024-08-18 14:18:04 +03:00
1 parent 1667036009
commit 1e902e47dc
6 files changed
+109 -4

No files matched your search

@@ -1,4 +1,4 @@
namespace Lumina.Domain.Common.Models;
namespace Lumina.Domain.Common.Models.Core;
/// <summary>
/// Base class for all domain Aggregate Roots.
@@ -1,4 +1,4 @@
namespace Lumina.Domain.Common.Models;
namespace Lumina.Domain.Common.Models.Core;
/// <summary>
/// Base identity class for all domain Aggregate Roots.
@@ -1,4 +1,4 @@
namespace Lumina.Domain.Common.Models;
namespace Lumina.Domain.Common.Models.Core;
/// <summary>
/// Base class for all domain Entities.
@@ -1,4 +1,4 @@
namespace Lumina.Domain.Common.Models;
namespace Lumina.Domain.Common.Models.Core;
/// <summary>
/// Base class for all domain Value Objects.
@@ -0,0 +1,58 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Models.Core;
using Lumina.Domain.Common.Primitives;
#endregion
namespace Lumina.Domain.Common.Models.Metadata;
/// <summary>
/// Base class for all metadata-related value objects.
/// </summary>
public class BaseMetadata : ValueObject
{
#region ==================================================================== PROPERTIES =================================================================================
public string Title { get; }
public Optional<string> Description { get; }
public Optional<DateTime> ReleaseDate { get; }
public IReadOnlyList<string> Tags { get; }
public IReadOnlyList<string> Genres { get; }
public Optional<string> Language { get; }
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Overload C-tor.
/// </summary>
/// <param name="title">The title of the element to which this metadata object belongs to.</param>
/// <param name="releaseDate">The optional release date of the element to which this metadata object belongs to.</param>
/// <param name="description">The optional description of the element to which this metadata object belongs to.</param>
/// <param name="genres">The list of genres of the element to which this metadata object belongs to.</param>
/// <param name="tags">The list of tags of the element to which this metadata object belongs to.</param>
/// <param name="language">The optional language of the element to which this metadata object belongs to.</param>
/// <exception cref="ArgumentNullException"></exception>
public BaseMetadata(string title, Optional<DateTime> releaseDate, Optional<string> description, IEnumerable<string>? genres, IEnumerable<string>? tags, Optional<string> language)
{
Title = title ?? throw new ArgumentNullException(nameof(title));
ReleaseDate = releaseDate;
Description = description;
Genres = genres?.ToList().AsReadOnly() ?? new List<string>().AsReadOnly();
Tags = tags?.ToList().AsReadOnly() ?? new List<string>().AsReadOnly();
Language = language;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <inheritdoc/>
public override IEnumerable<object> GetEqualityComponents()
{
yield return Title;
yield return ReleaseDate;
yield return Description;
foreach (var genre in Genres)
yield return genre;
foreach (var tag in Tags)
yield return tag;
yield return Language;
}
#endregion
}
@@ -0,0 +1,47 @@
namespace Lumina.Domain.Common.Primitives;
/// <summary>
/// Primitive struct for explicitly handling cases when a value might be unknown or not applicable,
/// distinguishing between "not set" and "intentionally empty".
/// </summary>
/// <typeparam name="T">The type of the value represented by this structure.</typeparam>
public readonly struct Optional<T>
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly T? _value;
#endregion
#region ==================================================================== PROPERTIES =================================================================================
public bool HasValue { get; }
public T Value
{
get
{
return HasValue ? _value! : throw new InvalidOperationException("Optional has no value.");
}
}
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Overload C-tor.
/// </summary>
/// <param name="value">The value represented by this structure.</param>
public Optional(T value)
{
_value = value;
HasValue = true;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Custom implementation operator for converting <paramref name="value"/> to an <see cref="Optional{T}"/>.
/// </summary>
/// <param name="value">The value to be converted.</param>
public static implicit operator Optional<T>(T value)
{
return new Optional<T>(value);
}
#endregion
}