Skip to content

Commit 16bc3ae

Browse files
committed
refactor(core): 移除未使用的异常处理和日志扩展
- 删除了 ExceptionNotifierExtensions 类和 LoggerExtensions 类 - 移除了 HealthResult 结构和相关的 IBusHealth 接口成员 - 添加了 EntityHelper 类中的 EntityEquals 方法,用于比较实体对象 - 简化了 ILocalizeErrorMessage 接口,添加了 LocalizeMessage 方法
1 parent bace554 commit 16bc3ae

File tree

7 files changed

+107
-91
lines changed

7 files changed

+107
-91
lines changed

framework/src/Bing.Core/Bing/ExceptionHandling/Extensions.IExceptionNotifier.cs renamed to framework/src/Bing.Core/Bing/ExceptionHandling/ExceptionNotifierExtensions.cs

File renamed without changes.
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
namespace Bing.ExceptionHandling;
1+
using Bing.Localization;
2+
3+
namespace Bing.ExceptionHandling;
24

35
/// <summary>
46
/// 本地化错误消息
57
/// </summary>
68
public interface ILocalizeErrorMessage
79
{
10+
/// <summary>
11+
/// 获取本地化的错误消息。
12+
/// </summary>
13+
/// <param name="context">本地化上下文</param>
14+
/// <returns>返回与当前语言环境匹配的错误消息字符串。</returns>
15+
string LocalizeMessage(LocalizationContext context);
816
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Bing.Helpers;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace Bing.Logging;
5+
6+
/// <summary>
7+
/// 日志级别(<see cref="IHasLogLevel"/>) 扩展
8+
/// </summary>
9+
public static class HasLogLevelExtensions
10+
{
11+
/// <summary>
12+
/// 设置日志级别,并返回该异常
13+
/// </summary>
14+
/// <typeparam name="TException">异常类型,必须实现 <see cref="IHasLogLevel"/> 接口。</typeparam>
15+
/// <param name="exception">要设置日志级别的异常实例。</param>
16+
/// <param name="logLevel">要分配给异常的 <see cref="LogLevel"/>。</param>
17+
/// <returns>返回具有指定日志级别的异常对象。</returns>
18+
/// <exception cref="ArgumentNullException">如果 <paramref name="exception"/> 为空,则抛出异常。</exception>
19+
public static TException WithLogLevel<TException>(this TException exception, LogLevel logLevel)
20+
where TException : IHasLogLevel
21+
{
22+
Check.NotNull(exception, nameof(exception));
23+
exception.LogLevel = logLevel;
24+
return exception;
25+
}
26+
}

framework/src/Bing.Core/Bing/Logging/LoggerExtensions.cs

Lines changed: 0 additions & 79 deletions
This file was deleted.

framework/src/Bing.Core/Bing/Monitoring/Health/HealthResult.cs renamed to framework/src/Bing.Core/Bing/Monitoring/Health/BusHealthResult.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace Bing.Monitoring.Health;
22

33
/// <summary>
4-
/// 业务健康
4+
/// 业务健康结果
55
/// </summary>
6-
public readonly struct HealthResult
6+
public readonly struct BusHealthResult
77
{
88
/// <summary>
99
/// 空只读字典
@@ -12,13 +12,13 @@ public readonly struct HealthResult
1212
private static readonly IReadOnlyDictionary<string, object> _emptyReadOnlyDictionary = new Dictionary<string, object>();
1313

1414
/// <summary>
15-
/// 初始化一个<see cref="HealthResult"/>类型的实例
15+
/// 初始化一个<see cref="BusHealthResult"/>类型的实例
1616
/// </summary>
1717
/// <param name="status">业务健康状态</param>
1818
/// <param name="description">描述</param>
1919
/// <param name="exception">异常</param>
2020
/// <param name="data">数据</param>
21-
private HealthResult(BusHealthStatus status, string description = null, Exception exception = null, IReadOnlyDictionary<string, object> data = null)
21+
private BusHealthResult(BusHealthStatus status, string description = null, Exception exception = null, IReadOnlyDictionary<string, object> data = null)
2222
{
2323
Status = status;
2424
Description = description;
@@ -51,23 +51,23 @@ private HealthResult(BusHealthStatus status, string description = null, Exceptio
5151
/// </summary>
5252
/// <param name="description">描述</param>
5353
/// <param name="data">数据</param>
54-
public static HealthResult Healthy(string description = null, IReadOnlyDictionary<string, object> data = null) => new HealthResult(BusHealthStatus.Healthy, description, null, data);
54+
public static BusHealthResult Healthy(string description = null, IReadOnlyDictionary<string, object> data = null) => new(BusHealthStatus.Healthy, description, null, data);
5555

5656
/// <summary>
5757
/// 降级的
5858
/// </summary>
5959
/// <param name="description">描述</param>
6060
/// <param name="exception">异常</param>
6161
/// <param name="data">数据</param>
62-
public static HealthResult Degraded(string description = null, Exception exception = null, IReadOnlyDictionary<string, object> data = null) =>
63-
new HealthResult(BusHealthStatus.Degraded, description, exception, data);
62+
public static BusHealthResult Degraded(string description = null, Exception exception = null, IReadOnlyDictionary<string, object> data = null) =>
63+
new(BusHealthStatus.Degraded, description, exception, data);
6464

6565
/// <summary>
6666
/// 不良的
6767
/// </summary>
6868
/// <param name="description">描述</param>
6969
/// <param name="exception">异常</param>
7070
/// <param name="data">数据</param>
71-
public static HealthResult Unhealthy(string description = null, Exception exception = null, IReadOnlyDictionary<string, object> data = null) =>
72-
new HealthResult(BusHealthStatus.Unhealthy, description, exception, data);
71+
public static BusHealthResult Unhealthy(string description = null, Exception exception = null, IReadOnlyDictionary<string, object> data = null) =>
72+
new(BusHealthStatus.Unhealthy, description, exception, data);
7373
}

framework/src/Bing.Core/Bing/Monitoring/Health/IBusHealth.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ public interface IBusHealth
99
/// 业务名称
1010
/// </summary>
1111
string Name { get; }
12-
13-
1412
}

framework/src/Bing.Ddd.Domain/Bing/Domain/Entities/EntityHelper.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,69 @@ public static class EntityHelper
1616
/// </summary>
1717
public static Func<Guid> GuidGenerateFunc { get; set; } = Guid.NewGuid;
1818

19+
/// <summary>
20+
/// 判断两个 <see cref="IEntity"/> 实例是否相等。
21+
/// </summary>
22+
/// <param name="entity1">第一个实体对象。</param>
23+
/// <param name="entity2">第二个实体对象。</param>
24+
/// <returns>
25+
/// 如果两个实体对象相等,则返回 <c>true</c>;否则返回 <c>false</c>。
26+
/// </returns>
27+
public static bool EntityEquals(IEntity entity1, IEntity entity2)
28+
{
29+
if (entity1 == null || entity2 == null)
30+
return false;
31+
32+
// 如果引用相同,则直接返回 true
33+
if (ReferenceEquals(entity1, entity2))
34+
return true;
35+
36+
// 如果两个实体类型不兼容,则返回 false
37+
var typeOfEntity1 = entity1.GetType();
38+
var typeOfEntity2 = entity2.GetType();
39+
if (!typeOfEntity1.IsAssignableFrom(typeOfEntity2) && !typeOfEntity2.IsAssignableFrom(typeOfEntity1))
40+
return false;
41+
42+
// 多租户委托检查
43+
44+
// 瞬时对象不视为相等
45+
if (HasDefaultKeys(entity1) && HasDefaultKeys(entity2))
46+
return false;
47+
48+
// 如果键数量不匹配,则不相等
49+
var entity1Keys = entity1.GetKeys();
50+
var entity2Keys = entity2.GetKeys();
51+
if(entity1Keys.Length!=entity2Keys.Length)
52+
return false;
53+
54+
// 逐个比较主键值
55+
for (var i = 0; i < entity1Keys.Length; i++)
56+
{
57+
// 如果 `entity1Key` 为 null,`entity2Key` 也必须为 null,否则不相等
58+
var entity1Key = entity1Keys[i];
59+
var entity2Key = entity2Keys[i];
60+
if (entity1Key == null)
61+
{
62+
if (entity2Key == null)
63+
continue;
64+
return false;
65+
}
66+
67+
// 如果 `entity2Key` 为 null,则不相等
68+
if (entity2Key == null)
69+
return false;
70+
71+
// 如果两个键值都是默认值(如 0、null、Guid.Empty),则继续比较
72+
if (Types.IsDefaultValue(entity1Key) && Types.IsDefaultValue(entity2Key))
73+
return false;
74+
75+
// 进行键值比较,如果不同,则返回 false
76+
if (!entity1Key.Equals(entity2Key))
77+
return false;
78+
}
79+
return true;
80+
}
81+
1982
/// <summary>
2083
/// 判断指定的类型是否实现了 <see cref="IEntity"/> 接口。
2184
/// </summary>

0 commit comments

Comments
 (0)