Skip to content

Commit af4f722

Browse files
committed
feat: 新增 业务异常、用户友好异常
1 parent a28260d commit af4f722

File tree

6 files changed

+228
-2
lines changed

6 files changed

+228
-2
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using Bing.ExceptionHandling;
4+
using Bing.Logging;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Bing
8+
{
9+
/// <summary>
10+
/// 业务异常
11+
/// </summary>
12+
[Serializable]
13+
public class BusinessException : Exception, IBusinessException, IHasErrorCode, IHasErrorDetails, IHasLogLevel
14+
{
15+
/// <summary>
16+
/// 错误码
17+
/// </summary>
18+
public string Code { get; set; }
19+
20+
/// <summary>
21+
/// 错误详情
22+
/// </summary>
23+
public string Details { get; set; }
24+
25+
/// <summary>
26+
/// 日志级别
27+
/// </summary>
28+
public LogLevel LogLevel { get; set; }
29+
30+
/// <summary>
31+
/// 初始化一个<see cref="BusinessException"/>类型的实例
32+
/// </summary>
33+
/// <param name="code">错误码</param>
34+
/// <param name="message">错误消息</param>
35+
/// <param name="details">错误详情</param>
36+
/// <param name="innerException">内部异常</param>
37+
/// <param name="logLevel">日志级别</param>
38+
public BusinessException(
39+
string code,
40+
string message,
41+
string details = null,
42+
Exception innerException = null,
43+
LogLevel logLevel = LogLevel.Warning)
44+
: base(message, innerException)
45+
{
46+
Code = code;
47+
Details = details;
48+
LogLevel = logLevel;
49+
}
50+
51+
/// <summary>
52+
/// 初始化一个<see cref="BusinessException"/>类型的实例
53+
/// </summary>
54+
/// <param name="serializationInfo">序列化信息</param>
55+
/// <param name="context">流上下文</param>
56+
public BusinessException(SerializationInfo serializationInfo, StreamingContext context)
57+
: base(serializationInfo, context)
58+
{
59+
}
60+
61+
/// <summary>
62+
/// 设置数据
63+
/// </summary>
64+
/// <param name="name">键名</param>
65+
/// <param name="value">值</param>
66+
public BusinessException WithData(string name, object value)
67+
{
68+
Data[name] = value;
69+
return this;
70+
}
71+
}
72+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Reflection;
3+
using Bing.Extensions;
4+
using Microsoft.Extensions.DependencyInjection;
5+
6+
namespace Bing.DependencyInjection
7+
{
8+
/// <summary>
9+
/// 常规注册器基类
10+
/// </summary>
11+
public abstract class ConventionalRegistrarBase : IConventionalRegistrar
12+
{
13+
/// <summary>
14+
/// 添加程序集
15+
/// </summary>
16+
/// <param name="services">服务集合</param>
17+
/// <param name="assembly">程序集</param>
18+
public virtual void AddAssembly(IServiceCollection services, Assembly assembly)
19+
{
20+
}
21+
22+
/// <summary>
23+
/// 添加类型数组
24+
/// </summary>
25+
/// <param name="services">服务集合</param>
26+
/// <param name="types">类型数组</param>
27+
public virtual void AddTypes(IServiceCollection services, params Type[] types)
28+
{
29+
foreach (var type in types)
30+
AddType(services, type);
31+
}
32+
33+
/// <summary>
34+
/// 添加类型
35+
/// </summary>
36+
/// <param name="services">服务集合</param>
37+
/// <param name="type">类型</param>
38+
public abstract void AddType(IServiceCollection services, Type type);
39+
40+
/// <summary>
41+
/// 获取指定类型的 <see cref="DependencyAttribute"/>
42+
/// </summary>
43+
/// <param name="type">类型</param>
44+
protected virtual DependencyAttribute GetDependencyAttributeOrNull(Type type)
45+
{
46+
return type.GetCustomAttribute<DependencyAttribute>(true);
47+
}
48+
49+
/// <summary>
50+
/// 获取指定类型的 <see cref="ServiceLifetime"/> 生命周期类型
51+
/// </summary>
52+
/// <param name="type">依赖注入实现类的类型</param>
53+
/// <param name="dependencyAttribute">依赖注入行为特性</param>
54+
protected virtual ServiceLifetime? GetLifetimeOrNull(Type type, DependencyAttribute dependencyAttribute)
55+
{
56+
return dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromClassHierarchy(type) ?? GetDefaultLifetimeOrNull(type);
57+
}
58+
59+
/// <summary>
60+
/// 通过类层次结构获取服务 <see cref="ServiceLifetime"/> 生命周期类型
61+
/// </summary>
62+
/// <param name="type">依赖注入实现类的类型</param>
63+
protected virtual ServiceLifetime? GetServiceLifetimeFromClassHierarchy(Type type)
64+
{
65+
if (type.IsDeriveClassFrom<ITransientDependency>())
66+
return ServiceLifetime.Transient;
67+
if (type.IsDeriveClassFrom<IScopedDependency>())
68+
return ServiceLifetime.Scoped;
69+
if (type.IsDeriveClassFrom<ISingletonDependency>())
70+
return ServiceLifetime.Singleton;
71+
return null;
72+
}
73+
74+
/// <summary>
75+
/// 获取指定类型的默认 <see cref="ServiceLifetime"/> 生命周期类型
76+
/// </summary>
77+
/// <param name="type">依赖注入实现类的类型</param>
78+
protected virtual ServiceLifetime? GetDefaultLifetimeOrNull(Type type) => null;
79+
}
80+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Reflection;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace Bing.DependencyInjection
6+
{
7+
/// <summary>
8+
/// 常规注册器
9+
/// </summary>
10+
public interface IConventionalRegistrar
11+
{
12+
/// <summary>
13+
/// 添加程序集
14+
/// </summary>
15+
/// <param name="services">服务集合</param>
16+
/// <param name="assembly">程序集</param>
17+
void AddAssembly(IServiceCollection services, Assembly assembly);
18+
19+
/// <summary>
20+
/// 添加类型数组
21+
/// </summary>
22+
/// <param name="services">服务集合</param>
23+
/// <param name="types">类型数组</param>
24+
void AddTypes(IServiceCollection services, params Type[] types);
25+
26+
/// <summary>
27+
/// 添加类型
28+
/// </summary>
29+
/// <param name="services">服务集合</param>
30+
/// <param name="type">类型</param>
31+
void AddType(IServiceCollection services, Type type);
32+
}
33+
}

framework/src/Bing/Bing/Exceptions/IBusinessException.cs renamed to framework/src/Bing/Bing/IBusinessException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Bing.Exceptions
1+
namespace Bing
22
{
33
/// <summary>
44
/// 业务异常

framework/src/Bing/Bing/Exceptions/IUserFriendlyException.cs renamed to framework/src/Bing/Bing/IUserFriendlyException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Bing.Exceptions
1+
namespace Bing
22
{
33
/// <summary>
44
/// 用户友好提示异常
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace Bing
6+
{
7+
/// <summary>
8+
/// 用户友好异常
9+
/// </summary>
10+
[Serializable]
11+
public class UserFriendlyException : BusinessException, IUserFriendlyException
12+
{
13+
/// <summary>
14+
/// 初始化一个<see cref="UserFriendlyException"/>类型的实例
15+
/// </summary>
16+
/// <param name="message">错误消息</param>
17+
/// <param name="code">错误码</param>
18+
/// <param name="details">错误详情</param>
19+
/// <param name="innerException">内部异常</param>
20+
/// <param name="logLevel">日志级别</param>
21+
public UserFriendlyException(
22+
string message,
23+
string code = null,
24+
string details = null,
25+
Exception innerException = null,
26+
LogLevel logLevel = LogLevel.Warning)
27+
: base(code, message, details, innerException, logLevel)
28+
{
29+
}
30+
31+
/// <summary>
32+
/// 初始化一个<see cref="UserFriendlyException"/>类型的实例
33+
/// </summary>
34+
/// <param name="serializationInfo">序列化信息</param>
35+
/// <param name="context">流上下文</param>
36+
public UserFriendlyException(SerializationInfo serializationInfo, StreamingContext context)
37+
: base(serializationInfo, context)
38+
{
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)