Data模块

一、模块定义

1、DataSeedOptions的属性DataSeedContributorList

 只有实现IDataSeedContributor,自动增加列表里面去

2、配置DbConnectionOptions

    public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            Configure<DbConnectionOptions>(configuration);

            context.Services.AddSingleton(typeof(IDataFilter<>), typeof(DataFilter<>));
        }

二、数据软删除,扩展字典

ISoftDelete

IHasExtraProperties

三、连接字符串解析,及实现(结合多租户)

  public interface IConnectionStringResolver
    {
        [NotNull]
        string Resolve(string connectionStringName = null);
    }

四、种子数据写入

   public interface IDataSeeder
    {
        Task SeedAsync(DataSeedContext context);
    }

护展方法

   public static Task SeedAsync(this IDataSeeder seeder, Guid? tenantId = null)
        {
            return seeder.SeedAsync(new DataSeedContext(tenantId));
        }

在实现类,调用IDataSeedContributor的实现类,SeedAsync方法

 [UnitOfWork]
        public virtual async Task SeedAsync(DataSeedContext context)
        {
            using (var scope = ServiceScopeFactory.CreateScope())
            {
                foreach (var contributorType in Options.Contributors)
                {
                    var contributor = (IDataSeedContributor) scope
                        .ServiceProvider
                        .GetRequiredService(contributorType);

                    await contributor.SeedAsync(context);
                }
            }
        }

DataSeedContext,构造函数要求写入租户Id(必须),自定义属性 Dictionary<string, object> Properties { get; }

   public DataSeedContext(Guid? tenantId = null)
        {
            TenantId = tenantId;
            Properties = new Dictionary<string, object>();
        }
 public interface IDataSeedContributor
    {
        Task SeedAsync(DataSeedContext context);
    }

  见具体实现方法

    [UnitOfWork]
        public virtual async Task<IdentityDataSeedResult> SeedAsync(
            string adminEmail,
            string adminPassword,
            Guid? tenantId = null)
        {
            Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
            Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));

            var result = new IdentityDataSeedResult();

            //"admin" user
            const string adminUserName = "admin";
            var adminUser = await _userRepository.FindByNormalizedUserNameAsync(
                _lookupNormalizer.Normalize(adminUserName)
            );

            if (adminUser != null)
            {
                return result;
            }

            adminUser = new IdentityUser(
                _guidGenerator.Create(),
                adminUserName,
                adminEmail,
                tenantId
            )
            {
                Name = adminUserName
            };

            (await _userManager.CreateAsync(adminUser, adminPassword)).CheckErrors();
            result.CreatedAdminUser = true;

            //"admin" role
            const string adminRoleName = "admin";
            var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName));
            if (adminRole == null)
            {
                adminRole = new IdentityRole(
                    _guidGenerator.Create(),
                    adminRoleName,
                    tenantId
                )
                {
                    IsStatic = true,
                    IsPublic = true
                };

                (await _roleManager.CreateAsync(adminRole)).CheckErrors();
                result.CreatedAdminRole = true;
            }

            (await _userManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors();

            return result;
        }

五、IDataFilter

获取到DataFilter<T>实例,由于初始化,根据AbpDataFilterOption存储State,若没特定(查不到),默认是true的,若指定根据指定值

也可以临时指定设置Disposeable方法,执行完回归原来的值。

DataFilterState,存储着每个类型是否作数据筛选,默认是True的,它的IsEnable若没有

DataFilterOptions

   public interface IDataFilter<TFilter>
        where TFilter : class
    {
        IDisposable Enable();

        IDisposable Disable();

        bool IsEnabled { get; }
    }
原文地址:https://www.cnblogs.com/cloudsu/p/11190664.html