【愚公系列】2022年03月 .NET架构班 017-ABP vNext 特征模块
一、特征模块
1.设置模块定义
特征模块简单来说就是在同一套系统中为不同的租户提供一些差异化的功能。比如免费用户,提供的是基础功能,VIP用户则会多一些高级功能。
2.设置模块源码
下载地址:
https://github.com/abpframework/abp/tree/dev/modules/FeatureManagement
二、集成特征模块
1.EBusiness.Domain.Shared集成特征模块
1、先在项目中通过Nuget下载
Volo.Abp.FeatureManagement.Domain.Shared
2、然后在EBusinessDomainSharedModule文件上增加
[DependsOn(
typeof(AbpFeatureManagementDomainSharedModule)
)]
public class EBusinessDomainSharedModule : AbpModule
{}
2.EBusiness.Domain集成特征模块
1、先在项目中通过Nuget下载
Volo.Abp.FeatureManagement.Domain
2、然后在EBusinessDomainModule文件上增加
[DependsOn(
typeof(AbpFeatureManagementDomainModule)
)]
public class EBusinessDomainSharedModule : AbpModule
{}
3.EBusiness.EntityFrameworkCore集成特征模块
1、先在项目中通过Nuget下载
Volo.Abp.FeatureManagement.EntityFrameworkCore
2、然后在EBusinessEntityFrameworkCoreModule文件上增加
[DependsOn(
typeof(AbpFeatureManagementEntityFrameworkCoreModule)
)]
public class EBusinessEntityFrameworkCoreModule: AbpModule
{}
3、然后在EBusinessDbContext类上实现IFeatureManagementDbContext上下文
public DbSet<FeatureGrant> FeatureGrants { get; }
4、然后在EBusinessDbContext类上添加
[ReplaceDbContext(typeof(IFeatureManagementDbContext))]
[ConnectionStringName("Default")]
public class EBusinessDbContext :
AbpDbContext<EBusinessDbContext>,
IFeatureManagementDbContext
{
.....
public DbSet<Feature> Features { get; set; }
.....
}
5、然后在OnModelCreating方法中添加
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
/* Include modules to your migration db context */
.....
builder.ConfigureFeatureManagement();
.....
}
7、最后启动YDT.EBusiness.DbMigrator迁移项目,生成特征表
4.EBusiness.Application.Contracts集成特征模块
1、先在项目中通过Nuget下载
Volo.Abp.FeatureManagement.Application.Contracts
2、然后在EBusinessApplicationContractsModule文件上增加
[DependsOn(
typeof(AbpFeatureManagementApplicationContractsModule)
)]
public class EBusinessApplicationContractsModule: AbpModule
{}
5.EBusiness.Application集成特征模块
1、先在项目中通过Nuget下载
Volo.Abp.FeatureManagement.Application.Contracts
2、然后在EBusinessApplicationModule文件上增加
[DependsOn(
typeof(AbpFeatureManagementApplicationModule)
)]
public class EBusinessApplicationContractsModule: AbpModule
{}
6.EBusiness.HttpApi集成特征模块
1、先在项目中通过Nuget下载
Volo.Abp.FeatureManagement.Application.Contracts
2、然后在EBusinessHttpApiModule文件上增加
[DependsOn(
typeof(AbpFeatureManagementHttpApiModule)
)]
public class EBusinessHttpApiModule: AbpModule
{}
三、特征模块使用
1.特征定义
1、先在EBusiness.Domain模块中通过nuget引入
Volo.Abp.Features
2、然后在EBusiness.Domain模块中Features文件夹中创建EBusinessFeatures类
public static class EBusinessFeatures
{
public const string GroupName = "EBusiness";
/// <summary>
/// 邮件特征
/// </summary>
public static class Orders
{
public const string Default = GroupName + ".Orders";
public const string IsEmail = Default + ".IsEmail";
public const string IsSms = Default + ".IsSms"; // 发送短信特征
}
}
3、然后在EBusiness.Domain模块中Features文件夹中创建EBusinessFeatureDefinitionProvider类
实现FeatureDefinitionProvider抽象类
public class EBusinessFeatureDefinitionProvider : FeatureDefinitionProvider
{
public override void Define(IFeatureDefinitionContext context)
{
// 1、特征组
FeatureGroupDefinition featureGroupDefinition = context.AddGroup(EBusinessFeatures.GroupName);
// 2、定义邮件特征
featureGroupDefinition.AddFeature(EBusinessFeatures.Orders.IsEmail,"false");
// 3、定义短信特征
featureGroupDefinition.AddFeature(EBusinessFeatures.Orders.IsSms, "false");
}
}
2.特征管理
1、EBusinessFeatures接口
/// <summary>
/// 电商项目特征
/// </summary>
public class EBusinessFeaturesDto
{
public string IsEmail { set; get; } // 邮件特征
public string IsSms { set; get; } // 短信特征
}
/// <summary>
/// 电商项目特征实现
/// </summary>
public class EBusinessFeatureAppService : EBusinessAppService, IEBusinessFeatureAppService
{
public IFeatureManager _featureManager { set; get; }
public async Task<EBusinessFeaturesDto> GetAsync()
{
EBusinessFeaturesDto eBusinessFeaturesDto = new EBusinessFeaturesDto();
eBusinessFeaturesDto.IsEmail = await _featureManager.GetOrNullForTenantAsync(EBusinessFeatures.Orders.IsEmail, (Guid)CurrentTenant.Id);
eBusinessFeaturesDto.IsSms = await _featureManager.GetOrNullForTenantAsync(EBusinessFeatures.Orders.IsSms, (Guid)CurrentTenant.Id);
return eBusinessFeaturesDto;
}
public async Task UpdateAsync(EBusinessFeaturesUpdateDto input)
{
await _featureManager.SetForTenantAsync((Guid)CurrentTenant.Id, EBusinessFeatures.Orders.IsEmail, input.IsEmail);
await _featureManager.SetForTenantAsync((Guid)CurrentTenant.Id, EBusinessFeatures.Orders.IsSms, input.IsSms);
}
}
2、使用PUT接口添加特征到数据库
3.特征取值
public class OrderAppService : EBusinessAppService, IPayAppService
{
....
....
public async Task<OrderDto> CreateAsync(CreateOrderDto input)
{
// 1、创建订单
Order order = new Order(GuidGenerator.Create());
order = ObjectMapper.Map<CreateOrderDto,Order>(input,order);
// 1、获取用户信息
// Claim[] claims = CurrentUser.GetAllClaims();
order.UserId = CurrentUser.Id.Value;
// 2、保存订单
await _OrderRepository.InsertAsync(order);
// 3.1 发送邮件
string flag = FeatureChecker.GetOrNullAsync(EBusinessFeatures.Orders.IsEmail).Result;
if (flag.Equals("true"))
{
Console.WriteLine("发送邮件");
}
// 3.2 发送短信
string IsSmsflag = FeatureChecker.GetOrNullAsync(EBusinessFeatures.Orders.IsSms).Result;
if (IsSmsflag.Equals("true"))
{
Console.WriteLine("发送短信");
}
// 4、返回订单
return ObjectMapper.Map<Order, OrderDto>(order);
}
}
四、特征模块原理
1.特征定义原理
条件
1、Volo.Abp.Features模块
接口
1、FeatureDefinition
2、FeatureDefinitionProvider
3、FeatureDefinitionManager
说明
1、FeatureDefinition执行定义
2、FeatureDefinitionProvider提供特征。
3、FeatureDefinitionManager 核心执行
2.特征管理原理
条件
1、YDT.EBusiness.Application模块
2、Volo.Abp.FeatureManagement.Domain模块
接口
1、EBusinessFeaturesAppService
2、FeatureManager
3、FeatureManagementProvider (默认GlobalFeatureManagementProvider)
4、FeatureManagementStore
说明
1、EBusinessFeaturesAppService负责接口入口
2、FeatureManager负责客户端调用
3、FeatureManagementProvider负责特征扩展
4、FeatureManagementStore负责特征取值
3.特征取值原理
条件
1、Volo.Abp.Features模块
接口
1、FeatureChecker
2、FeatureValueProviderManager
3、FeatureValueProvider(默认:TenantFeatureValueProvider)
4、FeatureDefinitionManager
说明
1、FeatureChecker负责客户端取值
2、FeatureValueProviderManager负责提供特征扩展管理
3、FeatureValueProvider负责获取特征值
4、FeatureDefinitionManager负责特征定义管理
五、特征模块其他客户端使用
1.EBusiness.Web页面访问特征模块
1、先在EBusiness.Web模块中通过Nuget引入
Volo.Abp.FeatureManagement.Web
2、然后在EBusiness.Web模块中EBusinessWebModule类上添加
[DependsOn(
....
typeof(AbpFeatureManagementWebModule),
....
)]
public class EBusinessWebModule : AbpModule
{}
2.OA系统调用特征模块
1、先在项目中通过Nuget下载
EBusiness.HttpApi.Client
2、然后在OA文件上增加
[DependsOn(
typeof(AbpFeatureManagementHttpApiClientModule)
)]
public class OAModule: AbpModule
{}
- 点赞
- 收藏
- 关注作者
评论(0)