Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

定义新接口IEntityBatchConfiguration,用于接管实体注册时的批量配置 #212

Closed
gmf520 opened this issue Mar 17, 2021 · 0 comments
Labels
Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Milestone

Comments

@gmf520
Copy link
Member

gmf520 commented Mar 17, 2021

您的功能请求与现有问题有关吗?请描述

实体注册时,有些实体配置或者实体属性配置的需求比较统一,例如:

  • 所有实体添加一定规范的表前缀
  • 所有实体的DateTime属性希望在存数据库时转换为UTC时间
  • 希望生成数据库时将属性注释添加到表字段的描述信息中
  • 其他的一些需求
    如果逐个写代码配置,很繁琐,需要统一配置

描述您想要的解决方案

定义新接口IEntityBatchConfiguration来接管这些配置

    /// <summary>
    /// 定义实体的批量配置功能
    /// </summary>
    [MultipleDependency]
    public interface IEntityBatchConfiguration
    {
        /// <summary>
        /// 配置指定的<see cref="IMutableEntityType"/>
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        /// <param name="mutableEntityType">实体的<see cref="IMutableEntityType"/>类型</param>
        void Configure(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType);
    }

实现示例:表前缀 TableNamePrefixConfiguration

    /// <summary>
    /// 表名前缀配置
    /// </summary>
    public class TableNamePrefixConfiguration : IEntityBatchConfiguration
    {
        /// <summary>
        /// 配置指定的<see cref="IMutableEntityType"/>
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        /// <param name="mutableEntityType">实体的<see cref="IMutableEntityType"/>类型</param>
        public void Configure(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
        {
            string prefix = GetTableNamePrefix(mutableEntityType.ClrType);
            if (prefix.IsNullOrEmpty())
            {
                return;
            }

            string tableName = $"{prefix}_{mutableEntityType.GetTableName()}";
            modelBuilder.Entity(mutableEntityType.ClrType).ToTable(tableName);
        }

        /// <summary>
        /// 从实体类型获取表名前缀
        /// </summary>
        /// <param name="entityType">实体类型</param>
        /// <returns></returns>
        protected virtual string GetTableNamePrefix(Type entityType)
        {
            TableNamePrefixAttribute attribute = entityType.GetAttribute<TableNamePrefixAttribute>();
            return attribute?.Prefix;
        }
    }

应用

services.AddSingleton<IEntityBatchConfiguration, TableNamePrefixConfiguration>();
@gmf520 gmf520 added the Feature 🔨 新功能,新特性 label Mar 17, 2021
@gmf520 gmf520 added this to the v5.0.4 milestone Mar 17, 2021
@gmf520 gmf520 changed the title 定义新接口IMutableEntityTypeConfiguration,用于接管实体注册时的批量配置 定义新接口IEntityBatchConfiguration,用于接管实体注册时的批量配置 Mar 17, 2021
@gmf520 gmf520 added the Finished ✔️ 实现并完工 label Mar 17, 2021
@gmf520 gmf520 closed this as completed Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Projects
None yet
Development

No branches or pull requests

1 participant