182 lines
8.6 KiB
C#
182 lines
8.6 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
|
|
{
|
|
/// <summary>
|
|
/// Sets the specified flag on the service message or result, allowing for the categorization of the message based on specific conditions or statuses.
|
|
/// </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 service message instance with the updated flag, facilitating fluent chaining of methods.</returns>
|
|
public static T WithFlag<T>(this T serviceMessage, Enum flag) where T : IServiceMessage
|
|
{
|
|
serviceMessage.Flag = flag;
|
|
return serviceMessage;
|
|
}
|
|
|
|
#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
|
|
}
|
|
} |