145 lines
7.0 KiB
C#
145 lines
7.0 KiB
C#
using DigitalData.Core.Contracts.Application;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace DigitalData.Core.Application
|
|
{
|
|
/// <summary>
|
|
/// Represents the outcome of a service operation, encapsulating the success or failure status,
|
|
/// and any associated messages. It also supports optional translation of message keys for localization purposes.
|
|
/// </summary>
|
|
public class ServiceMessage : IServiceMessage
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the ServiceMessage class.
|
|
/// </summary>
|
|
public ServiceMessage()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the ServiceMessage class, specifying the success status.
|
|
/// Optionally, a function for translating message keys can be provided.
|
|
/// If a translation function is provided, it will be used for both string and enum message keys.
|
|
/// </summary>
|
|
/// <param name="isSuccess">Indicates whether the service operation was successful.</param>
|
|
/// If provided, it will also be adapted for translating enum keys by converting them to strings first.</param>
|
|
[Obsolete("Deprecated: initialize objects using object initializer.")]
|
|
public ServiceMessage(bool isSuccess)
|
|
{
|
|
IsSuccess = isSuccess;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the ServiceMessage class with specified success status, and messages.
|
|
/// </summary>
|
|
/// <param name="isSuccess">Indicates whether the service operation was successful.</param>
|
|
/// <param name="messages">An array of messages related to the operation's outcome.</param>
|
|
[Obsolete("Deprecated: initialize objects using object initializer.")]
|
|
public ServiceMessage(bool isSuccess, params string[] messages)
|
|
{
|
|
IsSuccess = isSuccess;
|
|
Messages = messages.ToList<string>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the service operation was successful.
|
|
/// </summary>
|
|
public bool IsSuccess { get; set; } = false;
|
|
|
|
/// <summary>
|
|
/// Represents the list of flags that indicate specific types of statuses or conditions in a service operation.
|
|
/// These flags help in categorizing the state of the operation more granularly, allowing for multiple conditions to be represented simultaneously.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ICollection<Enum> Flags { get; } = new List<Enum>();
|
|
|
|
/// <summary>
|
|
/// Checks if any of the current service message's flags matches the specified flag based on their string representations.
|
|
/// This method is useful for conditional logic where the exact string representation of the flag values is crucial.
|
|
/// </summary>
|
|
/// <param name="flag">The flag to check against the current service message's flags.</param>
|
|
/// <returns>true if a flag with a matching string representation exists; otherwise, false.</returns>
|
|
public bool HasFlag(Enum flag) => Flags.Any(f => f.ToString() == flag.ToString());
|
|
|
|
/// <summary>
|
|
/// [Obsolete("Deprecated: Use ClientMessages instead.")]
|
|
/// Gets a collection of messages associated with the service operation. These messages can be error descriptions,
|
|
/// success notifications, or other relevant information related to the operation's outcome.
|
|
/// </summary>
|
|
[Obsolete("Deprecated: Use ClientMessages instead.")]
|
|
public ICollection<string> Messages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Gets a collection of messages intended for client display. This replaces the deprecated 'Messages' property.
|
|
/// </summary>
|
|
public ICollection<string> ClientMessages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Gets a collection of messages used for tracing program execution at a fine-grained level. These are typically voluminous and detailed.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ICollection<string> TraceMessages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Gets a collection of messages helpful for debugging during development. These messages are often diagnostic.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ICollection<string> DebugMessages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Gets a collection of informational messages, less critical than warnings, generally used for non-critical notifications.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ICollection<string> InformationMessages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Gets a collection of messages indicating potential issues that are not necessarily errors, but which may require attention.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ICollection<string> WarningMessages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Gets a collection of error messages indicating failures or problems within the service.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ICollection<string> ErrorMessages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// Gets a collection of messages indicating critical issues that require immediate attention.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public ICollection<string> CriticalMessages { get; init; } = new List<string>();
|
|
|
|
/// <summary>
|
|
/// A function that translates a message key from a string to its localized or transformed representation.
|
|
/// This property allows for custom translation logic to be applied based on the application's needs.
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
public Func<string, string> KeyTranslator { get; init; } = key => key;
|
|
|
|
/// <summary>
|
|
/// Adds a new message to the collection of messages associated with the service operation.
|
|
/// </summary>
|
|
/// <param name="message">The message to add.</param>
|
|
/// <returns>The current instance of ServiceMessage, allowing for method chaining.</returns>
|
|
[Obsolete("Deprecated: Use ClientMessages instead.")]
|
|
public IServiceMessage WithMessage(string message)
|
|
{
|
|
Messages.Add(message);
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a message corresponding to the specified message key to the collection of messages associated with the service operation.
|
|
/// This method uses the string representation of the enum value as the message.
|
|
/// </summary>
|
|
/// <param name="messageKey">The enum value representing the message key.</param>
|
|
/// <returns>The current instance of ServiceMessage, allowing for method chaining.</returns>
|
|
[Obsolete("Deprecated: Use ClientMessages instead.")]
|
|
public IServiceMessage WithMessageKey(Enum messageKey)
|
|
{
|
|
Messages.Add(messageKey.ToString());
|
|
return this;
|
|
}
|
|
}
|
|
} |