1.模块化

本文 基于 abp,Bing,Osharp 模块部分

1.解耦

思考框架思路,最开始应该是解耦。

以前我们写项目直接引用第三方框架,导致如果后续项目升级,以及编写代码会产生大量问题,解耦是核心问题 。

解耦不是为了性能,而是优雅的代码。

 

2.代码实现

复制代码
/// <summary>
    /// 模块
    /// </summary>
    public interface IModule
    {
        /// <summary>
        /// 模块级别。级别越小越先启动
        /// </summary>
        ModuleLevel Level { get; }
        /// <summary>
        /// 模块启动顺序。模块启动的顺序先按级别启动,同一级别内部再按此顺序启动,
        /// 级别默认为0,表示无依赖,需要在同级别有依赖顺序的时候,再重写为>0的顺序值
        /// </summary>
        int Order { get; }
        /// <summary>
        /// 添加依赖模块
        /// </summary>
        /// <param name="dependModuleBuilder"></param>
        void AddDependModule(IDependModuleBuilder dependModuleBuilder);
        /// <summary>
        /// 添加服务
        /// </summary>
        /// <param name="serviceContainer"></param>
        void AddService(IServiceContainer serviceContainer);
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="serviceContext"></param>
        void UseService(IServiceContext serviceContext);

    }
复制代码

解释  

ModuleLevel  对应框架的等级 ,
Order      对应同等级的框架排序
这里会进行ModuleLevel排序,然后进行Order排序,最后依据排序的结果加载框架

1
2
3
4
5
6
7
8
9
10
11
/// <summary>
 /// 依赖模块构建对象
 /// </summary>
 public interface IDependModuleBuilder
 {
     /// <summary>
     /// 添加指定模块
     /// </summary>
     /// <typeparam name="TModule">要添加的模块类型</typeparam>
     IDependModuleBuilder AddDependModule<TModule>() where TModule : IModule;
 }

  

1
IDependModuleBuilder 添加依赖模块抽象接口,判定框架依赖项正确加载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/// <summary>
   /// 服务注册容器
   /// </summary>
   public interface IServiceContainer
   {
       /// <summary>
       /// 注册服务
       /// </summary>
       public IServiceCollection ServiceCollection { get; }
       /// <summary>
       /// 选项管理器
       /// </summary>
       public IOptionsManager OptionsManager { get; }
       /// <summary>
       /// 框架加载启动项
       /// </summary>
       public IBuilderManager BuilderManager { get; }
   }

  

1
IServiceContainer  注册所有服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// <summary>
/// 服务提供上下文
/// </summary>
public interface IServiceContext
{
 
    /// <summary>
    /// 获取请求服务
    /// </summary>
    /// <typeparam name="T">服务类型</typeparam>
    TService GetRequiredService<TService>() where TService : notnull;
 
    /// <summary>
    /// 获取请求服务
    /// </summary>
    /// <param name="serviceType"></param>
    /// <returns></returns>
    object GetRequiredService(Type serviceType);
 
    /// <summary>
    /// 获取请求服务
    /// </summary>
    /// <typeparam name="T">服务类型</typeparam>
    TService? GetService<TService>() where TService : notnull;
 
    /// <summary>
    /// 获取请求服务
    /// </summary>
    /// <param name="serviceType"></param>
    /// <returns></returns>
    object? GetService(Type serviceType);
 
    /// <summary>
    /// 获取请求服务
    /// </summary>
    /// <typeparam name="T">服务类型</typeparam>
    IEnumerable<TService> GetServices<TService>() where TService : notnull;
 
    /// <summary>
    /// 获取请求服务
    /// </summary>
    /// <param name="serviceType"></param>
    /// <returns></returns>
    IEnumerable<object?> GetServices(Type serviceType);
    /// <summary>
    /// 获取选项
    /// </summary>
    /// <typeparam name="TOptions"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    TOptions GetOptions<TOptions>(string? key = null) where TOptions : class;
 
    /// <summary>
    /// 服务提供上下文
    /// </summary>
    /// <returns></returns>
    IServiceContext GetServiceContext();
 
    /// <summary>
    /// 异步任务取消令牌提供程序
    /// </summary>
    /// <returns></returns>
    ICancellationTokenContext GetCancellationTokenContext();
}

  服务获取提供容器,基于ABP ServiceProvider  本质是一个服务内存缓存,保证服务不会重复获取

1
<br><br><br><br>
posted @   最后的鲨掉  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示