using AutoMapper;
using DigitalData.Core.Contracts.Application;
namespace DigitalData.Core.Application
{
///
/// Provides a base implementation of , offering basic service messaging and result creation functionalities.
///
public class ServiceBase : IServiceBase
{
///
/// Creates a service message with the specified success flag and messages.
///
/// Indicates if the operation was successful.
/// An array of messages associated with the operation.
/// A new instance of reflecting the operation outcome.
public virtual IServiceMessage CreateMessage(bool isSuccess, params string[] messages)
{
return new ServiceMessage(isSuccess, messages);
}
///
/// Creates a service result containing the provided data, success flag, and messages.
///
/// The type of the data included in the result.
/// The data to include in the result.
/// Indicates if the operation was successful.
/// An array of messages associated with the operation.
/// A new instance of with the specified data and outcome.
public virtual IServiceResult CreateResult(T? data = default, bool isSuccess = true, params string[] messages)
{
return new ServiceResult(data, isSuccess, messages);
}
///
/// Creates a successful service message.
///
/// An array of success messages.
/// A successful service message.
public virtual IServiceMessage Successful(params string[] messages) => CreateMessage(true, messages);
///
/// Creates a failed service message.
///
/// An array of failure messages.
/// A failed service message.
public virtual IServiceMessage Failed(params string[] messages) => CreateMessage(false, messages);
///
/// Creates a successful service result with the provided data.
///
/// The type of data included in the result.
/// The data to include in the result.
/// An array of success messages.
/// A successful service result containing the specified data.
public virtual IServiceResult Successful(T data, params string[] messages) => CreateResult(data, true, messages);
///
/// Creates a failed service result, optionally including data.
///
/// The type of data the service result can contain.
/// Optional data to include in the failed result.
/// An array of failure messages.
/// A failed service result, which may or may not contain the specified data.
public virtual IServiceResult Failed(T? data = default, params string[] messages) => CreateResult(data, false, messages);
///
/// Creates a failed service result using only failure messages, without explicitly including data.
///
///
/// 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.
///
/// The type of data the service result can contain. The result will contain the default value for this type.
/// An array of failure messages that provide details about the reasons for the operation's failure.
/// 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.
public virtual IServiceResult Failed(params string[] messages) => Failed(default, messages);
}
public static class AutoMapperExtension
{
///
/// Maps a source object to a destination object, or throws an exception if the mapping result is null.
///
/// The source object type.
/// The destination object type.
/// The source object to map from.
/// The mapped destination object.
/// Thrown when the mapping result is null.
public static TDestination MapOrThrow(this IMapper mapper, object source)
{
return mapper.Map(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.");
}
}
}