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排序,最后依据排序的结果加载框架
/// <summary>
/// 依赖模块构建对象
/// </summary>
public interface IDependModuleBuilder
{
/// <summary>
/// 添加指定模块
/// </summary>
/// <typeparam name="TModule">要添加的模块类型</typeparam>
IDependModuleBuilder AddDependModule<TModule>() where TModule : IModule;
}
IDependModuleBuilder 添加依赖模块抽象接口,判定框架依赖项正确加载
/// <summary>
/// 服务注册容器
/// </summary>
public interface IServiceContainer
{
/// <summary>
/// 注册服务
/// </summary>
public IServiceCollection ServiceCollection { get; }
/// <summary>
/// 选项管理器
/// </summary>
public IOptionsManager OptionsManager { get; }
/// <summary>
/// 框架加载启动项
/// </summary>
public IBuilderManager BuilderManager { get; }
}
IServiceContainer 注册所有服务
/// <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 本质是一个服务内存缓存,保证服务不会重复获取
我不是代码的生产者,只是代码的搬运工
浙公网安备 33010602011771号