refactor(DTO): Hinzufügen des Attributs Obsolete mit der Meldung „Use DigitalData.Core.Exceptions and .Middleware“
This commit is contained in:
parent
e94efc8534
commit
7621d657ac
@ -1,17 +1,17 @@
|
|||||||
namespace DigitalData.Core.Application.Abstraction.DTO
|
namespace DigitalData.Core.Application.Abstraction.DTO;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a base Data Transfer Object (DTO) with an identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TId">The type of the identifier.</typeparam>
|
||||||
|
/// <param name="Id">The identifier of the DTO.</param>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public record BaseDTO<TId>(TId Id) where TId : notnull
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a base Data Transfer Object (DTO) with an identifier.
|
/// Returns the hash code for this instance, based on the identifier.
|
||||||
|
/// This override ensures that the hash code is derived consistently from the identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TId">The type of the identifier.</typeparam>
|
/// <returns>A hash code for the current object, derived from the identifier.</returns>
|
||||||
/// <param name="Id">The identifier of the DTO.</param>
|
public override int GetHashCode() => Id.GetHashCode();
|
||||||
public record BaseDTO<TId>(TId Id) where TId : notnull
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the hash code for this instance, based on the identifier.
|
|
||||||
/// This override ensures that the hash code is derived consistently from the identifier.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A hash code for the current object, derived from the identifier.</returns>
|
|
||||||
public override int GetHashCode() => Id.GetHashCode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -2,32 +2,33 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
|
|
||||||
namespace DigitalData.Core.Application.Abstraction.DTO
|
namespace DigitalData.Core.Application.Abstraction.DTO;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides extension methods for dependency injection.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static class DIExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides extension methods for dependency injection.
|
/// Adds the <see cref="CookieConsentSettings"/> to the service collection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class DIExtensions
|
/// <param name="services">The service collection to add the settings to.</param>
|
||||||
|
/// <returns>The updated service collection.</returns>
|
||||||
|
/// <exception cref="ConfigurationErrorsException">
|
||||||
|
/// Thrown if the 'CookieConsentSettings' section is missing or improperly configured in appsettings.json.
|
||||||
|
/// </exception>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static IServiceCollection AddCookieConsentSettings(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
/// <summary>
|
services.AddSingleton(sp =>
|
||||||
/// Adds the <see cref="CookieConsentSettings"/> to the service collection.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="services">The service collection to add the settings to.</param>
|
|
||||||
/// <returns>The updated service collection.</returns>
|
|
||||||
/// <exception cref="ConfigurationErrorsException">
|
|
||||||
/// Thrown if the 'CookieConsentSettings' section is missing or improperly configured in appsettings.json.
|
|
||||||
/// </exception>
|
|
||||||
public static IServiceCollection AddCookieConsentSettings(this IServiceCollection services)
|
|
||||||
{
|
{
|
||||||
services.AddSingleton(sp =>
|
var configuration = sp.GetRequiredService<IConfiguration>();
|
||||||
{
|
var settings = configuration.GetSection("CookieConsentSettings").Get<CookieConsentSettings>();
|
||||||
var configuration = sp.GetRequiredService<IConfiguration>();
|
return settings is null
|
||||||
var settings = configuration.GetSection("CookieConsentSettings").Get<CookieConsentSettings>();
|
? throw new ConfigurationErrorsException("The 'CookieConsentSettings' section is missing or improperly configured in appsettings.json.")
|
||||||
return settings is null
|
: settings;
|
||||||
? throw new ConfigurationErrorsException("The 'CookieConsentSettings' section is missing or improperly configured in appsettings.json.")
|
});
|
||||||
: settings;
|
return services;
|
||||||
});
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,367 +1,393 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace DigitalData.Core.Application.Abstraction.DTO
|
namespace DigitalData.Core.Application.Abstraction.DTO;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides extension methods for data transfer objects (DTOs).
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static class DTOExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides extension methods for data transfer objects (DTOs).
|
/// Adds a single message to the result, if not null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class DTOExtensions
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the message to.</param>
|
||||||
|
/// <param name="message">The message to add.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Message<T>(this T result, string? message) where T : Result
|
||||||
{
|
{
|
||||||
/// <summary>
|
if(message is not null)
|
||||||
/// Adds a single message to the result, if not null.
|
result.Messages.Add(message);
|
||||||
/// </summary>
|
return result;
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the message to.</param>
|
|
||||||
/// <param name="message">The message to add.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Message<T>(this T result, string? message) where T : Result
|
|
||||||
{
|
|
||||||
if(message is not null)
|
|
||||||
result.Messages.Add(message);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static IEnumerable<T> FilterNull<T>(this IEnumerable<T?> list)
|
|
||||||
{
|
|
||||||
foreach (var item in list)
|
|
||||||
if(item is not null)
|
|
||||||
yield return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds multiple messages to the result, after removing nulls.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the messages to.</param>
|
|
||||||
/// <param name="messages">The messages to add.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Message<T>(this T result, params string?[] messages) where T : Result
|
|
||||||
{
|
|
||||||
result.Messages.AddRange(messages.FilterNull());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a collection of messages to the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the messages to.</param>
|
|
||||||
/// <param name="messages">The collection of messages to add.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Message<T>(this T result, IEnumerable<string?> messages) where T : Result
|
|
||||||
{
|
|
||||||
result.Messages.AddRange(messages.FilterNull());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a notice to the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the notice to.</param>
|
|
||||||
/// <param name="notice">The notice to add.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Notice<T>(this T result, Notice notice) where T : Result
|
|
||||||
{
|
|
||||||
result.Notices.Add(notice);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a collection of notices to the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the notices to.</param>
|
|
||||||
/// <param name="notices">The collection of notices to add.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Notice<T>(this T result, IEnumerable<Notice> notices) where T : Result
|
|
||||||
{
|
|
||||||
result.Notices.AddRange(notices);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds notices with a specific log level and flags to the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the notices to.</param>
|
|
||||||
/// <param name="level">The log level of the notices.</param>
|
|
||||||
/// <param name="flags">The flags associated with the notices.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Notice<T>(this T result, LogLevel level, params Enum[] flags) where T : Result
|
|
||||||
{
|
|
||||||
var notices = flags.Select(flag => new Notice()
|
|
||||||
{
|
|
||||||
Flag = flag,
|
|
||||||
Level = level
|
|
||||||
});
|
|
||||||
result.Notices.AddRange(notices);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a notice with a specific log level, flag, and messages to the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the notice to.</param>
|
|
||||||
/// <param name="level">The log level of the notice.</param>
|
|
||||||
/// <param name="flag">The flag associated with the notice.</param>
|
|
||||||
/// <param name="messages">The messages to add to the notice.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Notice<T>(this T result, LogLevel level, Enum flag, params string?[] messages) where T : Result
|
|
||||||
{
|
|
||||||
result.Notices.Add(new Notice()
|
|
||||||
{
|
|
||||||
Flag = flag,
|
|
||||||
Level = level,
|
|
||||||
Messages = messages.FilterNull().ToList()
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a notice with a specific log level and messages to the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the result.</typeparam>
|
|
||||||
/// <param name="result">The result to add the notice to.</param>
|
|
||||||
/// <param name="level">The log level of the notice.</param>
|
|
||||||
/// <param name="messages">The messages to add to the notice.</param>
|
|
||||||
/// <returns>The updated result.</returns>
|
|
||||||
public static T Notice<T>(this T result, LogLevel level, params string[] messages) where T : Result
|
|
||||||
{
|
|
||||||
result.Notices.Add(new Notice()
|
|
||||||
{
|
|
||||||
Flag = null,
|
|
||||||
Level = level,
|
|
||||||
Messages = messages.FilterNull().ToList()
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if any notice has the specified flag.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="notices">The collection of notices to check.</param>
|
|
||||||
/// <param name="flag">The flag to check for.</param>
|
|
||||||
/// <returns>True if any notice has the specified flag; otherwise, false.</returns>
|
|
||||||
public static bool HasFlag(this IEnumerable<Notice> notices, Enum flag) => notices.Any(n => n.Flag?.ToString() == flag.ToString());
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if any notice has any of the specified flags.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="notices">The collection of notices to check.</param>
|
|
||||||
/// <param name="flags">The flags to check for.</param>
|
|
||||||
/// <returns>True if any notice has any of the specified flags; otherwise, false.</returns>
|
|
||||||
public static bool HasAnyFlag(this IEnumerable<Notice> notices, params Enum[] flags) => flags.Any(f => notices.HasFlag(f));
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Executes a function based on the success or failure of the task result,
|
|
||||||
/// without using result data.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="tResult">The task returning a result to evaluate.</param>
|
|
||||||
/// <param name="Success">The function to execute if the result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static I? Then<I>(this Result result, Func<I> Success)
|
|
||||||
{
|
|
||||||
return result.IsSuccess ? Success() : default;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Executes a function based on the success or failure of the task result,
|
|
||||||
/// using the data in the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="tResult">The task returning a data result to evaluate.</param>
|
|
||||||
/// <param name="Success">The function to execute if the data result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static async Task<I?> ThenAsync<I>(this Result result, Func<Task<I>> SuccessAsync)
|
|
||||||
{
|
|
||||||
return result.IsSuccess ? await SuccessAsync() : default;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Executes a function based on the success or failure of the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="result">The result to evaluate.</param>
|
|
||||||
/// <param name="Success">The function to execute if the result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static I Then<I>(this Result result, Func<I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously executes a function based on the success or failure of the result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="result">The result to evaluate.</param>
|
|
||||||
/// <param name="SuccessAsync">The asynchronous function to execute if the result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static async Task<I> ThenAsync<I>(this Result result, Func<Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Executes a function based on the success or failure of the data result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="result">The data result to evaluate.</param>
|
|
||||||
/// <param name="Success">The function to execute if the data result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static I Then<T, I>(this DataResult<T> result, Func<T, I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously executes a function based on the success or failure of the data result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="result">The data result to evaluate.</param>
|
|
||||||
/// <param name="SuccessAsync">The asynchronous function to execute if the data result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static async Task<I> ThenAsync<T, I>(this DataResult<T> result, Func<T, Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously executes a function based on the success or failure of a task returning a result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="tResult">The task returning a result to evaluate.</param>
|
|
||||||
/// <param name="Success">The function to execute if the result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static async Task<I> ThenAsync<I>(this Task<Result> tResult, Func<I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
Result result = await tResult;
|
|
||||||
return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously executes a function based on the success or failure of a task returning a result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="tResult">The task returning a result to evaluate.</param>
|
|
||||||
/// <param name="SuccessAsync">The asynchronous function to execute if the result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static async Task<I> ThenAsync<I>(this Task<Result> tResult, Func<Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
Result result = await tResult;
|
|
||||||
return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously executes a function based on the success or failure of a task returning a data result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="tResult">The task returning a data result to evaluate.</param>
|
|
||||||
/// <param name="Success">The function to execute if the data result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static async Task<I> ThenAsync<T, I>(this Task<DataResult<T>> tResult, Func<T, I> Success, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
DataResult<T> result = await tResult;
|
|
||||||
return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously executes a function based on the success or failure of a task returning a data result.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
|
||||||
/// <typeparam name="I">The type of the return value.</typeparam>
|
|
||||||
/// <param name="tResult">The task returning a data result to evaluate.</param>
|
|
||||||
/// <param name="SuccessAsync">The asynchronous function to execute if the data result is successful.</param>
|
|
||||||
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
|
||||||
/// <returns>The result of the executed function.</returns>
|
|
||||||
public static async Task<I> ThenAsync<T, I>(this Task<DataResult<T>> tResult, Func<T, Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
|
||||||
{
|
|
||||||
DataResult<T> result = await tResult;
|
|
||||||
return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Joins the values into a single string with optional start, separator, and end strings.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the values.</typeparam>
|
|
||||||
/// <param name="values">The values to join.</param>
|
|
||||||
/// <param name="start">The starting string.</param>
|
|
||||||
/// <param name="separator">The separator string.</param>
|
|
||||||
/// <param name="end">The ending string.</param>
|
|
||||||
/// <returns>The joined string.</returns>
|
|
||||||
public static string Join<T>(this IEnumerable<T> values, string start = "", string seperator = ". ", string end = ".")
|
|
||||||
=> new StringBuilder(start).Append(string.Join(seperator, values)).Append(end).ToString();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Logs the notices using the specified logger.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="logger">The logger to use.</param>
|
|
||||||
/// <param name="notices">The collection of notices to log.</param>
|
|
||||||
/// <param name="start">The starting string for each notice.</param>
|
|
||||||
/// <param name="separator">The separator string for messages in each notice.</param>
|
|
||||||
/// <param name="end">The ending string for each notice.</param>
|
|
||||||
public static void LogNotice(this ILogger logger, IEnumerable<Notice> notices, string start = ": ", string seperator = ". ", string end = ".\n")
|
|
||||||
{
|
|
||||||
foreach(LogLevel level in Enum.GetValues(typeof(LogLevel)))
|
|
||||||
{
|
|
||||||
var logNotices = notices.Where(n => n.Level == level);
|
|
||||||
|
|
||||||
if (!logNotices.Any())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
foreach(Notice notice in logNotices)
|
|
||||||
{
|
|
||||||
if (notice.Flag is not null)
|
|
||||||
sb.Append(notice.Flag);
|
|
||||||
|
|
||||||
if (notice.Messages.Any())
|
|
||||||
sb.Append(start).Append(string.Join(seperator, notice.Messages)).AppendLine(end);
|
|
||||||
else sb.Append(end);
|
|
||||||
}
|
|
||||||
logger.Log(level, sb.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Logs the notices from a result using the specified logger.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="logger">The logger to use.</param>
|
|
||||||
/// <param name="result">The result containing the notices to log.</param>
|
|
||||||
/// <param name="start">The starting string for each notice.</param>
|
|
||||||
/// <param name="separator">The separator string for messages in each notice.</param>
|
|
||||||
/// <param name="end">The ending string for each notice.</param>
|
|
||||||
public static void LogNotice(this ILogger logger, Result result, string start = ": ", string seperator = ". ", string end = ".\n")
|
|
||||||
=> logger.LogNotice(notices: result.Notices, start: start, seperator: seperator, end: end);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the data result is right (true).
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="bResult">The data result to evaluate.</param>
|
|
||||||
/// <returns>True if the data result is true; otherwise, false.</returns>
|
|
||||||
public static bool IsRight(this DataResult<bool> bResult) => bResult.Data;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if the data result is wrong (false).
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="bResult">The data result to evaluate.</param>
|
|
||||||
/// <returns>True if the data result is false; otherwise, false.</returns>
|
|
||||||
public static bool IsWrong(this DataResult<bool> bResult) => !bResult.Data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
internal static IEnumerable<T> FilterNull<T>(this IEnumerable<T?> list)
|
||||||
|
{
|
||||||
|
foreach (var item in list)
|
||||||
|
if(item is not null)
|
||||||
|
yield return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds multiple messages to the result, after removing nulls.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the messages to.</param>
|
||||||
|
/// <param name="messages">The messages to add.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Message<T>(this T result, params string?[] messages) where T : Result
|
||||||
|
{
|
||||||
|
result.Messages.AddRange(messages.FilterNull());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a collection of messages to the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the messages to.</param>
|
||||||
|
/// <param name="messages">The collection of messages to add.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Message<T>(this T result, IEnumerable<string?> messages) where T : Result
|
||||||
|
{
|
||||||
|
result.Messages.AddRange(messages.FilterNull());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a notice to the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the notice to.</param>
|
||||||
|
/// <param name="notice">The notice to add.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Notice<T>(this T result, Notice notice) where T : Result
|
||||||
|
{
|
||||||
|
result.Notices.Add(notice);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a collection of notices to the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the notices to.</param>
|
||||||
|
/// <param name="notices">The collection of notices to add.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Notice<T>(this T result, IEnumerable<Notice> notices) where T : Result
|
||||||
|
{
|
||||||
|
result.Notices.AddRange(notices);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds notices with a specific log level and flags to the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the notices to.</param>
|
||||||
|
/// <param name="level">The log level of the notices.</param>
|
||||||
|
/// <param name="flags">The flags associated with the notices.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Notice<T>(this T result, LogLevel level, params Enum[] flags) where T : Result
|
||||||
|
{
|
||||||
|
var notices = flags.Select(flag => new Notice()
|
||||||
|
{
|
||||||
|
Flag = flag,
|
||||||
|
Level = level
|
||||||
|
});
|
||||||
|
result.Notices.AddRange(notices);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a notice with a specific log level, flag, and messages to the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the notice to.</param>
|
||||||
|
/// <param name="level">The log level of the notice.</param>
|
||||||
|
/// <param name="flag">The flag associated with the notice.</param>
|
||||||
|
/// <param name="messages">The messages to add to the notice.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Notice<T>(this T result, LogLevel level, Enum flag, params string?[] messages) where T : Result
|
||||||
|
{
|
||||||
|
result.Notices.Add(new Notice()
|
||||||
|
{
|
||||||
|
Flag = flag,
|
||||||
|
Level = level,
|
||||||
|
Messages = messages.FilterNull().ToList()
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a notice with a specific log level and messages to the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the result.</typeparam>
|
||||||
|
/// <param name="result">The result to add the notice to.</param>
|
||||||
|
/// <param name="level">The log level of the notice.</param>
|
||||||
|
/// <param name="messages">The messages to add to the notice.</param>
|
||||||
|
/// <returns>The updated result.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static T Notice<T>(this T result, LogLevel level, params string[] messages) where T : Result
|
||||||
|
{
|
||||||
|
result.Notices.Add(new Notice()
|
||||||
|
{
|
||||||
|
Flag = null,
|
||||||
|
Level = level,
|
||||||
|
Messages = messages.FilterNull().ToList()
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if any notice has the specified flag.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="notices">The collection of notices to check.</param>
|
||||||
|
/// <param name="flag">The flag to check for.</param>
|
||||||
|
/// <returns>True if any notice has the specified flag; otherwise, false.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static bool HasFlag(this IEnumerable<Notice> notices, Enum flag) => notices.Any(n => n.Flag?.ToString() == flag.ToString());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if any notice has any of the specified flags.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="notices">The collection of notices to check.</param>
|
||||||
|
/// <param name="flags">The flags to check for.</param>
|
||||||
|
/// <returns>True if any notice has any of the specified flags; otherwise, false.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static bool HasAnyFlag(this IEnumerable<Notice> notices, params Enum[] flags) => flags.Any(f => notices.HasFlag(f));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Executes a function based on the success or failure of the task result,
|
||||||
|
/// without using result data.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="tResult">The task returning a result to evaluate.</param>
|
||||||
|
/// <param name="Success">The function to execute if the result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static I? Then<I>(this Result result, Func<I> Success)
|
||||||
|
{
|
||||||
|
return result.IsSuccess ? Success() : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Executes a function based on the success or failure of the task result,
|
||||||
|
/// using the data in the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="tResult">The task returning a data result to evaluate.</param>
|
||||||
|
/// <param name="Success">The function to execute if the data result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static async Task<I?> ThenAsync<I>(this Result result, Func<Task<I>> SuccessAsync)
|
||||||
|
{
|
||||||
|
return result.IsSuccess ? await SuccessAsync() : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Executes a function based on the success or failure of the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="result">The result to evaluate.</param>
|
||||||
|
/// <param name="Success">The function to execute if the result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static I Then<I>(this Result result, Func<I> Success, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously executes a function based on the success or failure of the result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="result">The result to evaluate.</param>
|
||||||
|
/// <param name="SuccessAsync">The asynchronous function to execute if the result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static async Task<I> ThenAsync<I>(this Result result, Func<Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Executes a function based on the success or failure of the data result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="result">The data result to evaluate.</param>
|
||||||
|
/// <param name="Success">The function to execute if the data result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static I Then<T, I>(this DataResult<T> result, Func<T, I> Success, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously executes a function based on the success or failure of the data result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="result">The data result to evaluate.</param>
|
||||||
|
/// <param name="SuccessAsync">The asynchronous function to execute if the data result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static async Task<I> ThenAsync<T, I>(this DataResult<T> result, Func<T, Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously executes a function based on the success or failure of a task returning a result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="tResult">The task returning a result to evaluate.</param>
|
||||||
|
/// <param name="Success">The function to execute if the result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static async Task<I> ThenAsync<I>(this Task<Result> tResult, Func<I> Success, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
Result result = await tResult;
|
||||||
|
return result.IsSuccess ? Success() : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously executes a function based on the success or failure of a task returning a result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="tResult">The task returning a result to evaluate.</param>
|
||||||
|
/// <param name="SuccessAsync">The asynchronous function to execute if the result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static async Task<I> ThenAsync<I>(this Task<Result> tResult, Func<Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
Result result = await tResult;
|
||||||
|
return result.IsSuccess ? await SuccessAsync() : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously executes a function based on the success or failure of a task returning a data result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="tResult">The task returning a data result to evaluate.</param>
|
||||||
|
/// <param name="Success">The function to execute if the data result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static async Task<I> ThenAsync<T, I>(this Task<DataResult<T>> tResult, Func<T, I> Success, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
DataResult<T> result = await tResult;
|
||||||
|
return result.IsSuccess ? Success(result.Data) : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously executes a function based on the success or failure of a task returning a data result.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the data in the result.</typeparam>
|
||||||
|
/// <typeparam name="I">The type of the return value.</typeparam>
|
||||||
|
/// <param name="tResult">The task returning a data result to evaluate.</param>
|
||||||
|
/// <param name="SuccessAsync">The asynchronous function to execute if the data result is successful.</param>
|
||||||
|
/// <param name="Fail">The function to execute if the data result is a failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static async Task<I> ThenAsync<T, I>(this Task<DataResult<T>> tResult, Func<T, Task<I>> SuccessAsync, Func<List<string>, List<Notice>, I> Fail)
|
||||||
|
{
|
||||||
|
DataResult<T> result = await tResult;
|
||||||
|
return result.IsSuccess ? await SuccessAsync(result.Data) : Fail(result.Messages, result.Notices);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Joins the values into a single string with optional start, separator, and end strings.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the values.</typeparam>
|
||||||
|
/// <param name="values">The values to join.</param>
|
||||||
|
/// <param name="start">The starting string.</param>
|
||||||
|
/// <param name="separator">The separator string.</param>
|
||||||
|
/// <param name="end">The ending string.</param>
|
||||||
|
/// <returns>The joined string.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static string Join<T>(this IEnumerable<T> values, string start = "", string seperator = ". ", string end = ".")
|
||||||
|
=> new StringBuilder(start).Append(string.Join(seperator, values)).Append(end).ToString();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Logs the notices using the specified logger.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger to use.</param>
|
||||||
|
/// <param name="notices">The collection of notices to log.</param>
|
||||||
|
/// <param name="start">The starting string for each notice.</param>
|
||||||
|
/// <param name="separator">The separator string for messages in each notice.</param>
|
||||||
|
/// <param name="end">The ending string for each notice.</param>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static void LogNotice(this ILogger logger, IEnumerable<Notice> notices, string start = ": ", string seperator = ". ", string end = ".\n")
|
||||||
|
{
|
||||||
|
foreach(LogLevel level in Enum.GetValues(typeof(LogLevel)))
|
||||||
|
{
|
||||||
|
var logNotices = notices.Where(n => n.Level == level);
|
||||||
|
|
||||||
|
if (!logNotices.Any())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
foreach(Notice notice in logNotices)
|
||||||
|
{
|
||||||
|
if (notice.Flag is not null)
|
||||||
|
sb.Append(notice.Flag);
|
||||||
|
|
||||||
|
if (notice.Messages.Any())
|
||||||
|
sb.Append(start).Append(string.Join(seperator, notice.Messages)).AppendLine(end);
|
||||||
|
else sb.Append(end);
|
||||||
|
}
|
||||||
|
logger.Log(level, sb.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Logs the notices from a result using the specified logger.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger to use.</param>
|
||||||
|
/// <param name="result">The result containing the notices to log.</param>
|
||||||
|
/// <param name="start">The starting string for each notice.</param>
|
||||||
|
/// <param name="separator">The separator string for messages in each notice.</param>
|
||||||
|
/// <param name="end">The ending string for each notice.</param>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static void LogNotice(this ILogger logger, Result result, string start = ": ", string seperator = ". ", string end = ".\n")
|
||||||
|
=> logger.LogNotice(notices: result.Notices, start: start, seperator: seperator, end: end);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if the data result is right (true).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bResult">The data result to evaluate.</param>
|
||||||
|
/// <returns>True if the data result is true; otherwise, false.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static bool IsRight(this DataResult<bool> bResult) => bResult.Data;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if the data result is wrong (false).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bResult">The data result to evaluate.</param>
|
||||||
|
/// <returns>True if the data result is false; otherwise, false.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static bool IsWrong(this DataResult<bool> bResult) => !bResult.Data;
|
||||||
}
|
}
|
||||||
@ -1,26 +1,28 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace DigitalData.Core.Application.Abstraction.DTO
|
namespace DigitalData.Core.Application.Abstraction.DTO;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a result of an operation that includes data, inheriting from <see cref="Result"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the data included in the result.</typeparam>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public class DataResult<T> : Result
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a result of an operation that includes data, inheriting from <see cref="Result"/>.
|
/// Gets or sets the data included in the result. This property is required.
|
||||||
|
/// It will be ignored during JSON serialization if the value is null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of the data included in the result.</typeparam>
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public class DataResult<T> : Result
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
{
|
public required T Data { get; set; }
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the data included in the result. This property is required.
|
|
||||||
/// It will be ignored during JSON serialization if the value is null.
|
|
||||||
/// </summary>
|
|
||||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
||||||
public required T Data { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts the current <see cref="DataResult{T}"/> to a failed <see cref="DataResult{I}"/>,
|
/// Converts the current <see cref="DataResult{T}"/> to a failed <see cref="DataResult{I}"/>,
|
||||||
/// preserving the messages and notices.
|
/// preserving the messages and notices.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="I">The type of the data in the new failed result.</typeparam>
|
/// <typeparam name="I">The type of the data in the new failed result.</typeparam>
|
||||||
/// <returns>A failed <see cref="DataResult{I}"/> with the current messages and notices.</returns>
|
/// <returns>A failed <see cref="DataResult{I}"/> with the current messages and notices.</returns>
|
||||||
public DataResult<I> ToFail<I>() => Fail<I>().Message(Messages).Notice(Notices);
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
}
|
public DataResult<I> ToFail<I>() => Fail<I>().Message(Messages).Notice(Notices);
|
||||||
}
|
}
|
||||||
@ -1,50 +1,50 @@
|
|||||||
namespace DigitalData.Core.Application.Abstraction.DTO
|
namespace DigitalData.Core.Application.Abstraction.DTO;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines flags that indicate specific types of status or conditions in a service operation.
|
||||||
|
/// These flags help in categorizing and identifying specific circumstances or issues that may arise during execution.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public enum Flag
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines flags that indicate specific types of status or conditions in a service operation.
|
/// Indicates a security breach or vulnerability has been detected during the service operation.
|
||||||
/// These flags help in categorizing and identifying specific circumstances or issues that may arise during execution.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public enum Flag
|
SecurityBreach,
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Indicates a security breach or vulnerability has been detected during the service operation.
|
|
||||||
/// </summary>
|
|
||||||
SecurityBreach,
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates a potential issue with data integrity during the service operation.
|
/// Indicates a potential issue with data integrity during the service operation.
|
||||||
/// This flag is used when data may have been altered, corrupted, or is otherwise unreliable,
|
/// This flag is used when data may have been altered, corrupted, or is otherwise unreliable,
|
||||||
/// which could impact the accuracy or trustworthiness of the operation's results.
|
/// which could impact the accuracy or trustworthiness of the operation's results.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
DataIntegrityIssue,
|
DataIntegrityIssue,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates that either a security breach, a data integrity issue, or both have been detected during the service operation.
|
/// Indicates that either a security breach, a data integrity issue, or both have been detected during the service operation.
|
||||||
/// 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.
|
/// 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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
SecurityBreachOrDataIntegrity,
|
SecurityBreachOrDataIntegrity,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates a possible security breach during the service operation.
|
/// Indicates a possible security breach during the service operation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
PossibleSecurityBreach,
|
PossibleSecurityBreach,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates a possible issue with data integrity during the service operation.
|
/// Indicates a possible issue with data integrity during the service operation.
|
||||||
/// This flag is used when there is a suspicion of data alteration, corruption, or unreliability.
|
/// This flag is used when there is a suspicion of data alteration, corruption, or unreliability.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
PossibleDataIntegrityIssue,
|
PossibleDataIntegrityIssue,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates that either a possible security breach, a possible data integrity issue, or both have been detected during the service operation.
|
/// Indicates that either a possible security breach, a possible data integrity issue, or both have been detected during the service operation.
|
||||||
/// This flag is used when it is uncertain whether the issue is related to security, data integrity, or both.
|
/// This flag is used when it is uncertain whether the issue is related to security, data integrity, or both.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
PossibleSecurityBreachOrDataIntegrity,
|
PossibleSecurityBreachOrDataIntegrity,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates that the requested resource or operation could not be found.
|
/// Indicates that the requested resource or operation could not be found.
|
||||||
/// This flag is used when the specified item or condition does not exist or is unavailable.
|
/// This flag is used when the specified item or condition does not exist or is unavailable.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
NotFound
|
NotFound
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -1,25 +1,28 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace DigitalData.Core.Application.Abstraction.DTO
|
namespace DigitalData.Core.Application.Abstraction.DTO;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a notice for logging purposes, containing a flag, log level, and associated messages.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public class Notice
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a notice for logging purposes, containing a flag, log level, and associated messages.
|
/// Gets or sets an optional flag associated with the notice.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Notice
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
{
|
public Enum? Flag { get; init; } = null;
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets an optional flag associated with the notice.
|
|
||||||
/// </summary>
|
|
||||||
public Enum? Flag { get; init; } = null;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the log level for the notice.
|
/// Gets or sets the log level for the notice.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public LogLevel Level { get; init; } = LogLevel.None;
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public LogLevel Level { get; init; } = LogLevel.None;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a list of messages associated with the notice.
|
/// Gets a list of messages associated with the notice.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<string> Messages { get; init; } = new();
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
}
|
public List<string> Messages { get; init; } = new();
|
||||||
}
|
}
|
||||||
@ -1,97 +1,108 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace DigitalData.Core.Application.Abstraction.DTO
|
namespace DigitalData.Core.Application.Abstraction.DTO;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents the result of an operation, containing information about its success or failure,
|
||||||
|
/// messages for the client, and notices for logging.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public class Result
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents the result of an operation, containing information about its success or failure,
|
/// Gets or sets a value indicating whether the operation was successful.
|
||||||
/// messages for the client, and notices for logging.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Result
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public bool IsSuccess { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether the operation failed.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public bool IsFailed => !IsSuccess;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a list of messages intended for the client.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public List<string> Messages { get; init; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a list of notices intended for logging purposes. This property is ignored during JSON serialization.
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
[JsonIgnore]
|
||||||
|
public List<Notice> Notices = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a <see cref="DataResult{T}"/> with the specified data.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the data.</typeparam>
|
||||||
|
/// <param name="data">The data to include in the result.</param>
|
||||||
|
/// <returns>A new <see cref="DataResult{T}"/> instance.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public DataResult<T> Data<T>(T data) => new()
|
||||||
{
|
{
|
||||||
/// <summary>
|
IsSuccess = IsSuccess,
|
||||||
/// Gets or sets a value indicating whether the operation was successful.
|
Messages = Messages,
|
||||||
/// </summary>
|
Notices = Notices,
|
||||||
public bool IsSuccess { get; set; } = false;
|
Data = data
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether the operation failed.
|
/// Checks if any notice has the specified flag.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsFailed => !IsSuccess;
|
/// <param name="flag">The flag to check.</param>
|
||||||
|
/// <returns>True if any notice has the specified flag; otherwise, false.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public bool HasFlag(Enum flag) => Notices.Any(n => n.Flag?.ToString() == flag.ToString());
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a list of messages intended for the client.
|
/// Checks if any notice has any of the specified flags.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<string> Messages { get; init; } = new();
|
/// <param name="flags">The flags to check.</param>
|
||||||
|
/// <returns>True if any notice has any of the specified flags; otherwise, false.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public bool HasAnyFlag(params Enum[] flags) => flags.Any(HasFlag);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a list of notices intended for logging purposes. This property is ignored during JSON serialization.
|
/// Creates a new successful <see cref="Result"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonIgnore]
|
/// <returns>A new successful <see cref="Result"/>.</returns>
|
||||||
public List<Notice> Notices = new();
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static Result Success() => new() { IsSuccess = true };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a <see cref="DataResult{T}"/> with the specified data.
|
/// Creates a new failed <see cref="Result"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of the data.</typeparam>
|
/// <returns>A new failed <see cref="Result"/>.</returns>
|
||||||
/// <param name="data">The data to include in the result.</param>
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
/// <returns>A new <see cref="DataResult{T}"/> instance.</returns>
|
public static Result Fail() => new() { IsSuccess = false };
|
||||||
public DataResult<T> Data<T>(T data) => new()
|
|
||||||
{
|
|
||||||
IsSuccess = IsSuccess,
|
|
||||||
Messages = Messages,
|
|
||||||
Notices = Notices,
|
|
||||||
Data = data
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if any notice has the specified flag.
|
/// Creates a new successful <see cref="DataResult{T}"/> with the specified data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="flag">The flag to check.</param>
|
/// <typeparam name="T">The type of the data.</typeparam>
|
||||||
/// <returns>True if any notice has the specified flag; otherwise, false.</returns>
|
/// <param name="data">The data to include in the result.</param>
|
||||||
public bool HasFlag(Enum flag) => Notices.Any(n => n.Flag?.ToString() == flag.ToString());
|
/// <returns>A new successful <see cref="DataResult{T}"/> with the specified data.</returns>
|
||||||
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
public static DataResult<T> Success<T>(T data) => new()
|
||||||
|
{
|
||||||
|
IsSuccess = true,
|
||||||
|
Data = data
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if any notice has any of the specified flags.
|
/// Creates a new failed <see cref="DataResult{T}"/> with no data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="flags">The flags to check.</param>
|
/// <typeparam name="T">The type of the data.</typeparam>
|
||||||
/// <returns>True if any notice has any of the specified flags; otherwise, false.</returns>
|
/// <returns>A new failed <see cref="DataResult{T}"/> with no data.</returns>
|
||||||
public bool HasAnyFlag(params Enum[] flags) => flags.Any(HasFlag);
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new successful <see cref="Result"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A new successful <see cref="Result"/>.</returns>
|
|
||||||
public static Result Success() => new() { IsSuccess = true };
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new failed <see cref="Result"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A new failed <see cref="Result"/>.</returns>
|
|
||||||
public static Result Fail() => new() { IsSuccess = false };
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new successful <see cref="DataResult{T}"/> with the specified data.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the data.</typeparam>
|
|
||||||
/// <param name="data">The data to include in the result.</param>
|
|
||||||
/// <returns>A new successful <see cref="DataResult{T}"/> with the specified data.</returns>
|
|
||||||
public static DataResult<T> Success<T>(T data) => new()
|
|
||||||
{
|
|
||||||
IsSuccess = true,
|
|
||||||
Data = data
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new failed <see cref="DataResult{T}"/> with no data.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the data.</typeparam>
|
|
||||||
/// <returns>A new failed <see cref="DataResult{T}"/> with no data.</returns>
|
|
||||||
#pragma warning disable CS8601 // Possible null reference assignment.
|
#pragma warning disable CS8601 // Possible null reference assignment.
|
||||||
public static DataResult<T> Fail<T>() => new()
|
public static DataResult<T> Fail<T>() => new()
|
||||||
{
|
{
|
||||||
IsSuccess = false,
|
IsSuccess = false,
|
||||||
Data = default
|
Data = default
|
||||||
};
|
};
|
||||||
#pragma warning restore CS8601 // Possible null reference assignment.
|
#pragma warning restore CS8601 // Possible null reference assignment.
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user