using DigitalData.Core.Contracts.Application;
namespace DigitalData.Core.Application
{
///
/// 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.
///
public static class ServiceMessageExtensions
{
#region Flag
///
/// Sets the specified flag on the service message or result, allowing for the categorization of the message based on specific conditions or statuses.
///
/// The type of IServiceMessage.
/// The service message instance to modify.
/// The flag to set, indicating a specific condition or status.
/// The service message instance with the updated flag, facilitating fluent chaining of methods.
public static T WithFlag(this T serviceMessage, Enum flag) where T : IServiceMessage
{
serviceMessage.Flags.Add(flag);
return serviceMessage;
}
///
/// Determines whether the service message has a flag indicating a security breach.
///
/// The service message instance to check.
/// True if the service message has the security breach flag; otherwise, false.
public static bool HasSecurityBreachFlag(this IServiceMessage serviceMessage) => serviceMessage.HasFlag(Flag.SecurityBreach);
///
/// Determines whether the service message has a flag indicating a data integrity issue.
///
/// The service message instance to check.
/// True if the service message has the data integrity issue flag; otherwise, false.
public static bool HasDataIntegrityIssueFlag(this IServiceMessage serviceMessage) => serviceMessage.HasFlag(Flag.DataIntegrityIssue);
///
/// Determines whether the service message has a flag indicating either a security breach or a data integrity issue, or both.
/// This flag is used when it is not sure whether the problem is security or data integrity. In this case, data integrity should be checked first.
///
/// The service message instance to check.
/// True if the service message has the flag indicating either or both issues; otherwise, false.
public static bool HasSecurityBreachOrDataIntegrityFlag(this IServiceMessage serviceMessage) => serviceMessage.HasFlag(Flag.SecurityBreachOrDataIntegrity);
#endregion
#region ClientMessage
///
/// Adds a single client message to the service message.
///
/// The service message to modify.
/// The message to add.
/// The modified service message instance.
public static T WithClientMessage(this T serviceMessage, string message) where T : IServiceMessage
{
serviceMessage.ClientMessages.Add(message);
return serviceMessage;
}
///
/// Adds a client message based on an enumeration key to the service message.
///
/// The service message to modify.
/// The enum key representing the message.
/// The modified service message instance.
public static T WithClientMessageKey(this T serviceMessage, Enum messageKey) where T : IServiceMessage
{
var message = serviceMessage.KeyTranslator(messageKey.ToString());
return serviceMessage.WithClientMessage(message);
}
#endregion
#region TraceMessages
///
/// Adds a trace message to the service message.
///
/// The service message to modify.
/// The trace message to add.
/// The modified service message instance.
public static T WithTraceMessage(this T serviceMessage, string message) where T : IServiceMessage
{
serviceMessage.TraceMessages.Add(message);
return serviceMessage;
}
///
/// Adds a trace message based on an enumeration key to the service message.
///
/// The service message to modify.
/// The enum key representing the trace message.
/// The modified service message instance.
public static T WithTraceMessageKey(this T serviceMessage, Enum messageKey) where T : IServiceMessage
{
var message = serviceMessage.KeyTranslator(messageKey.ToString());
return serviceMessage.WithTraceMessage(message);
}
#endregion
#region DebugMessages
///
/// Adds a debug message to the service message.
///
/// The service message to modify.
/// The debug message to add.
/// The modified service message instance.
public static T WithDebugMessage(this T serviceMessage, string message) where T : IServiceMessage
{
serviceMessage.DebugMessages.Add(message);
return serviceMessage;
}
///
/// Adds a debug message based on an enumeration key to the service message.
///
/// The service message to modify.
/// The enum key representing the debug message.
/// The modified service message instance.
public static T WithDebugMessageKey(this T serviceMessage, Enum messageKey) where T : IServiceMessage
{
var message = serviceMessage.KeyTranslator(messageKey.ToString());
return serviceMessage.WithDebugMessage(message);
}
#endregion
#region WarningMessages
///
/// Adds a warning message to the service message.
///
/// The service message to modify.
/// The warning message to add.
/// The modified service message instance.
public static T WithWarningMessage(this T serviceMessage, string message) where T : IServiceMessage
{
serviceMessage.WarningMessages.Add(message);
return serviceMessage;
}
///
/// Adds a warning message based on an enumeration key to the service message.
///
/// The service message to modify.
/// The enum key representing the warning message.
/// The modified service message instance.
public static T WithWarningMessageKey(this T serviceMessage, Enum messageKey) where T : IServiceMessage
{
var message = serviceMessage.KeyTranslator(messageKey.ToString());
return serviceMessage.WithWarningMessage(message);
}
#endregion
#region ErrorMessages
///
/// Adds an error message to the service message.
///
/// The service message to modify.
/// The error message to add.
/// The modified service message instance.
public static T WithErrorMessage(this T serviceMessage, string message) where T : IServiceMessage
{
serviceMessage.ErrorMessages.Add(message);
return serviceMessage;
}
///
/// Adds an error message based on an enumeration key to the service message.
///
/// The service message to modify.
/// The enum key representing the error message.
/// The modified service message instance.
public static T WithErrorMessageKey(this T serviceMessage, Enum messageKey) where T : IServiceMessage
{
var message = serviceMessage.KeyTranslator(messageKey.ToString());
return serviceMessage.WithErrorMessage(message);
}
#endregion
#region CriticalMessages
///
/// Adds a critical message to the service message.
///
/// The service message to modify.
/// The critical message to add.
/// The modified service message instance.
public static T WithCriticalMessage(this T serviceMessage, string message) where T : IServiceMessage
{
serviceMessage.CriticalMessages.Add(message);
return serviceMessage;
}
///
/// Adds a critical message based on an enumeration key to the service message.
///
/// The service message to modify.
/// The enum key representing the critical message.
/// The modified service message instance.
public static T WithCriticalMessageKey(this T serviceMessage, Enum messageKey) where T : IServiceMessage
{
var message = serviceMessage.KeyTranslator(messageKey.ToString());
return serviceMessage.WithCriticalMessage(message);
}
#endregion
}
}