- Neue DTO-Extension-Methoden hinzugefügt, um die Verarbeitung und Zuweisung von Nachrichten und Benachrichtigungen in Ergebnisobjekten zu vereinfachen. - Lokalisierungsunterstützung in der API-Schicht implementiert, einschließlich Cookie-basierter Lokalisierung und Konfiguration unterstützter Kulturen. - Die Integration von StringLocalizer in die API-Schicht wurde durchgeführt, um eine nahtlose Mehrsprachigkeit zu ermöglichen. - Fehlerbehandlung für fehlende Konfigurationseinstellungen verbessert. Die Änderungen verbessern die Flexibilität und Wartbarkeit des Codes und unterstützen eine effizientere Internationalisierung der Anwendung.
110 lines
3.7 KiB
C#
110 lines
3.7 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Text;
|
|
|
|
namespace DigitalData.Core.DTO
|
|
{
|
|
public static class DTOExtensions
|
|
{
|
|
public static T Message<T>(this T result, string message) where T : Result
|
|
{
|
|
result.Messages.Add(message);
|
|
return result;
|
|
}
|
|
|
|
public static T Message<T>(this T result, params string[] messages) where T : Result
|
|
{
|
|
result.Messages.AddRange(messages);
|
|
return result;
|
|
}
|
|
|
|
public static T Notice<T>(this T result, Notice notice) where T : Result
|
|
{
|
|
result.Notices.Add(notice);
|
|
return result;
|
|
}
|
|
|
|
public static T Notice<T>(this T result, params Notice[] notices) where T : Result
|
|
{
|
|
result.Notices.AddRange(notices);
|
|
return result;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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.ToList()
|
|
});
|
|
return result;
|
|
}
|
|
|
|
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.ToList()
|
|
});
|
|
return result;
|
|
}
|
|
|
|
public static I Then<I>(this Result result, Func<I> Try, Func<List<string>, List<Notice>, I> Catch)
|
|
{
|
|
return result.IsSuccess ? Try() : Catch(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static I Then<T, I>(this DataResult<T> result, Func<T, I> Try, Func<List<string>, List<Notice>, I> Catch)
|
|
{
|
|
return result.IsSuccess ? Try(result.Data) : Catch(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> Then<I>(this Task<Result> tResult, Func<I> Try, Func<List<string>, List<Notice>, I> Catch)
|
|
{
|
|
Result result = await tResult;
|
|
return result.IsSuccess ? Try() : Catch(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static async Task<I> Then<T, I>(this Task<DataResult<T>> tResult, Func<T, I> Try, Func<List<string>, List<Notice>, I> Catch)
|
|
{
|
|
DataResult<T> result = await tResult;
|
|
return result.IsSuccess ? Try(result.Data) : Catch(result.Messages, result.Notices);
|
|
}
|
|
|
|
public static void LogNotice<T>(this ILogger<T> 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());
|
|
}
|
|
}
|
|
}
|
|
} |