Developer 02 124fe0787b Implementiere WithMessage und WithMessageKey Methoden in IServiceMessage
Methoden hinzugefügt, um Nachrichten direkt oder über Enum-Schlüssel hinzuzufügen, unterstützt Methodenverkettung in IServiceMessage.
2024-04-18 09:45:15 +02:00

56 lines
2.4 KiB
C#

using DigitalData.Core.Contracts.Application;
namespace DigitalData.Core.Application
{
/// <summary>
/// Represents the outcome of a service operation, encapsulating the success or failure status,
/// and any associated messages.
/// </summary>
public class ServiceMessage : IServiceMessage
{
/// <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="data">The data associated with a successful operation.</param>
/// <param name="messages">An array of messages related to the operation's outcome.</param>
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; }
/// <summary>
/// Gets or sets a collection of messages associated with the service operation.
/// </summary>
public List<string> Messages { get; set; } = new List<string>();
/// <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>
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>
public IServiceMessage WithMessageKey(Enum messageKey)
{
Messages.Add(messageKey.ToString());
return this;
}
}
}