209 lines
11 KiB
C#
209 lines
11 KiB
C#
using DigitalData.Core.Contracts.Application;
|
|
|
|
namespace DigitalData.Core.Application
|
|
{
|
|
/// <summary>
|
|
/// Provides extension methods for IServiceMessage to enhance the usability of service messages
|
|
/// by allowing easy addition of various types of messages including client, trace, debug, information,
|
|
/// warning, error, and critical messages. These methods support both direct messages and enum-based keys
|
|
/// for messages, facilitating localized or custom-formatted messages.
|
|
/// </summary>
|
|
public static class ServiceMessageExtensions
|
|
{
|
|
#region Flag
|
|
/// <summary>
|
|
/// Sets the specified flag on the service message, allowing for detailed categorization of the message based on specific conditions or statuses.
|
|
/// This method supports fluent chaining by returning the modified service message instance.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of IServiceMessage.</typeparam>
|
|
/// <param name="serviceMessage">The service message instance to modify.</param>
|
|
/// <param name="flag">The flag to set, indicating a specific condition or status.</param>
|
|
/// <returns>The modified service message instance, facilitating fluent chaining of methods.</returns>
|
|
public static T WithFlag<T>(this T serviceMessage, Enum flag) where T : IServiceMessage
|
|
{
|
|
serviceMessage.Flags.Add(flag);
|
|
return serviceMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if the service message has a flag indicating a security breach.
|
|
/// This is useful for identifying messages related to security concerns for prompt and specific handling.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message instance to check.</param>
|
|
/// <returns>True if the service message contains the security breach flag; otherwise, false.</returns>
|
|
public static bool HasSecurityBreachFlag(this IServiceMessage serviceMessage) => serviceMessage.HasFlag(Flag.SecurityBreach);
|
|
|
|
/// <summary>
|
|
/// Checks if the service message has a flag indicating a data integrity issue.
|
|
/// This method is essential for operations where data accuracy and integrity are critical.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message instance to check.</param>
|
|
/// <returns>True if the service message contains the data integrity issue flag; otherwise, false.</returns>
|
|
public static bool HasDataIntegrityIssueFlag(this IServiceMessage serviceMessage) => serviceMessage.HasFlag(Flag.DataIntegrityIssue);
|
|
|
|
/// <summary>
|
|
/// Checks if the service message has a flag indicating either a security breach or a data integrity issue, or both.
|
|
/// This flag is critical when it is uncertain whether the problem pertains to security or data integrity, advising that data integrity checks should be prioritized.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message instance to check.</param>
|
|
/// <returns>True if the service message contains the flag for either or both issues; otherwise, false.</returns>
|
|
public static bool HasSecurityBreachOrDataIntegrityFlag(this IServiceMessage serviceMessage) => serviceMessage.HasFlag(Flag.SecurityBreachOrDataIntegrity);
|
|
#endregion
|
|
|
|
#region ClientMessage
|
|
/// <summary>
|
|
/// Adds a single client message to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="message">The message to add.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithClientMessage<T>(this T serviceMessage, string message) where T : IServiceMessage
|
|
{
|
|
serviceMessage.ClientMessages.Add(message);
|
|
return serviceMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a client message based on an enumeration key to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="messageKey">The enum key representing the message.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithClientMessageKey<T>(this T serviceMessage, Enum messageKey) where T : IServiceMessage
|
|
{
|
|
var message = serviceMessage.KeyTranslator(messageKey.ToString());
|
|
return serviceMessage.WithClientMessage(message);
|
|
}
|
|
#endregion
|
|
|
|
#region TraceMessages
|
|
/// <summary>
|
|
/// Adds a trace message to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="message">The trace message to add.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithTraceMessage<T>(this T serviceMessage, string message) where T : IServiceMessage
|
|
{
|
|
serviceMessage.TraceMessages.Add(message);
|
|
return serviceMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a trace message based on an enumeration key to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="messageKey">The enum key representing the trace message.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithTraceMessageKey<T>(this T serviceMessage, Enum messageKey) where T : IServiceMessage
|
|
{
|
|
var message = serviceMessage.KeyTranslator(messageKey.ToString());
|
|
return serviceMessage.WithTraceMessage(message);
|
|
}
|
|
#endregion
|
|
|
|
#region DebugMessages
|
|
/// <summary>
|
|
/// Adds a debug message to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="message">The debug message to add.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithDebugMessage<T>(this T serviceMessage, string message) where T : IServiceMessage
|
|
{
|
|
serviceMessage.DebugMessages.Add(message);
|
|
return serviceMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a debug message based on an enumeration key to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="messageKey">The enum key representing the debug message.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithDebugMessageKey<T>(this T serviceMessage, Enum messageKey) where T : IServiceMessage
|
|
{
|
|
var message = serviceMessage.KeyTranslator(messageKey.ToString());
|
|
return serviceMessage.WithDebugMessage(message);
|
|
}
|
|
#endregion
|
|
|
|
#region WarningMessages
|
|
/// <summary>
|
|
/// Adds a warning message to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="message">The warning message to add.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithWarningMessage<T>(this T serviceMessage, string message) where T : IServiceMessage
|
|
{
|
|
serviceMessage.WarningMessages.Add(message);
|
|
return serviceMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a warning message based on an enumeration key to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="messageKey">The enum key representing the warning message.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithWarningMessageKey<T>(this T serviceMessage, Enum messageKey) where T : IServiceMessage
|
|
{
|
|
var message = serviceMessage.KeyTranslator(messageKey.ToString());
|
|
return serviceMessage.WithWarningMessage(message);
|
|
}
|
|
#endregion
|
|
|
|
#region ErrorMessages
|
|
/// <summary>
|
|
/// Adds an error message to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="message">The error message to add.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithErrorMessage<T>(this T serviceMessage, string message) where T : IServiceMessage
|
|
{
|
|
serviceMessage.ErrorMessages.Add(message);
|
|
return serviceMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds an error message based on an enumeration key to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="messageKey">The enum key representing the error message.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithErrorMessageKey<T>(this T serviceMessage, Enum messageKey) where T : IServiceMessage
|
|
{
|
|
var message = serviceMessage.KeyTranslator(messageKey.ToString());
|
|
return serviceMessage.WithErrorMessage(message);
|
|
}
|
|
#endregion
|
|
|
|
#region CriticalMessages
|
|
/// <summary>
|
|
/// Adds a critical message to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="message">The critical message to add.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithCriticalMessage<T>(this T serviceMessage, string message) where T : IServiceMessage
|
|
{
|
|
serviceMessage.CriticalMessages.Add(message);
|
|
return serviceMessage;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a critical message based on an enumeration key to the service message.
|
|
/// </summary>
|
|
/// <param name="serviceMessage">The service message to modify.</param>
|
|
/// <param name="messageKey">The enum key representing the critical message.</param>
|
|
/// <returns>The modified service message instance.</returns>
|
|
public static T WithCriticalMessageKey<T>(this T serviceMessage, Enum messageKey) where T : IServiceMessage
|
|
{
|
|
var message = serviceMessage.KeyTranslator(messageKey.ToString());
|
|
return serviceMessage.WithCriticalMessage(message);
|
|
}
|
|
#endregion
|
|
}
|
|
} |