98 lines
5.6 KiB
C#
98 lines
5.6 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Contracts.Application;
|
|
|
|
namespace DigitalData.Core.Application
|
|
{
|
|
/// <summary>
|
|
/// Provides a base implementation of <see cref="IServiceBase"/>, offering basic service messaging and result creation functionalities.
|
|
/// </summary>
|
|
public class ServiceBase : IServiceBase
|
|
{
|
|
/// <summary>
|
|
/// Creates a service message with the specified success flag and messages.
|
|
/// </summary>
|
|
/// <param name="isSuccess">Indicates if the operation was successful.</param>
|
|
/// <param name="messages">An array of messages associated with the operation.</param>
|
|
/// <returns>A new instance of <see cref="ServiceMessage"/> reflecting the operation outcome.</returns>
|
|
public virtual IServiceMessage CreateMessage(bool isSuccess, params string[] messages)
|
|
{
|
|
return new ServiceMessage(isSuccess, messages);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a service result containing the provided data, success flag, and messages.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the data included in the result.</typeparam>
|
|
/// <param name="data">The data to include in the result.</param>
|
|
/// <param name="isSuccess">Indicates if the operation was successful.</param>
|
|
/// <param name="messages">An array of messages associated with the operation.</param>
|
|
/// <returns>A new instance of <see cref="ServiceResult{T}"/> with the specified data and outcome.</returns>
|
|
public virtual IServiceResult<T> CreateResult<T>(T? data = default, bool isSuccess = true, params string[] messages)
|
|
{
|
|
return new ServiceResult<T>(data, isSuccess, messages);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a successful service message.
|
|
/// </summary>
|
|
/// <param name="messages">An array of success messages.</param>
|
|
/// <returns>A successful service message.</returns>
|
|
public virtual IServiceMessage Successful(params string[] messages) => CreateMessage(true, messages);
|
|
|
|
/// <summary>
|
|
/// Creates a failed service message.
|
|
/// </summary>
|
|
/// <param name="messages">An array of failure messages.</param>
|
|
/// <returns>A failed service message.</returns>
|
|
public virtual IServiceMessage Failed(params string[] messages) => CreateMessage(false, messages);
|
|
|
|
/// <summary>
|
|
/// Creates a successful service result with the provided data.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of data included in the result.</typeparam>
|
|
/// <param name="data">The data to include in the result.</param>
|
|
/// <param name="messages">An array of success messages.</param>
|
|
/// <returns>A successful service result containing the specified data.</returns>
|
|
public virtual IServiceResult<T> Successful<T>(T data, params string[] messages) => CreateResult(data, true, messages);
|
|
|
|
/// <summary>
|
|
/// Creates a failed service result, optionally including data.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of data the service result can contain.</typeparam>
|
|
/// <param name="data">Optional data to include in the failed result.</param>
|
|
/// <param name="messages">An array of failure messages.</param>
|
|
/// <returns>A failed service result, which may or may not contain the specified data.</returns>
|
|
public virtual IServiceResult<T> Failed<T>(T? data = default, params string[] messages) => CreateResult(data, false, messages);
|
|
|
|
/// <summary>
|
|
/// Creates a failed service result using only failure messages, without explicitly including data.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This method provides a convenient way to create a failed service result when the failure does not pertain to any specific data,
|
|
/// or when the inclusion of data is not necessary to convey the failure reason. The data part of the result will be set to its default value.
|
|
/// </remarks>
|
|
/// <typeparam name="T">The type of data the service result can contain. The result will contain the default value for this type.</typeparam>
|
|
/// <param name="messages">An array of failure messages that provide details about the reasons for the operation's failure.</param>
|
|
/// <returns>A failed service result. The data part of the result will be set to the default value for the specified type,
|
|
/// and it will include the provided failure messages.</returns>
|
|
public virtual IServiceResult<T> Failed<T>(params string[] messages) => Failed<T>(default, messages);
|
|
}
|
|
|
|
public static class AutoMapperExtension
|
|
{
|
|
/// <summary>
|
|
/// Maps a source object to a destination object, or throws an exception if the mapping result is null.
|
|
/// </summary>
|
|
/// <typeparam name="TSource">The source object type.</typeparam>
|
|
/// <typeparam name="TDestination">The destination object type.</typeparam>
|
|
/// <param name="source">The source object to map from.</param>
|
|
/// <returns>The mapped destination object.</returns>
|
|
/// <exception cref="MappingResultNullException">Thrown when the mapping result is null.</exception>
|
|
public static TDestination MapOrThrow<TDestination>(this IMapper mapper, object source)
|
|
{
|
|
return mapper.Map<TDestination>(source) ?? throw new AutoMapperMappingException(
|
|
$"Mapping to {typeof(TDestination).FullName} resulted in a null object. " +
|
|
"Hint: Ensure that the AutoMapper profile configuration for this mapping is correct.");
|
|
}
|
|
}
|
|
} |