为了账号安全,请及时绑定邮箱和手机立即绑定

实现接口的实体的通用配置

实现接口的实体的通用配置

C#
阿晨1998 2021-11-21 15:13:03
假设我有一些界面,例如:public interface ISoftDeletable{    bool IsActive { get; set }}我有很多实体实现了它:public class Entity1 : ISoftDeletable{    public int Id { get; set }    public bool IsActive { get; set; }}public class Entity2 : ISoftDeletable{    public int Id { get; set }    public bool IsActive { get; set; }}在OnModelCreating:protected override void OnModelCreating(ModelBuilder modelBuilder){    modelBuilder.Entity<Entity1>().Property(e => e.IsActive).HasDefaultValue(true);    modelBuilder.Entity<Entity2>().Property(e => e.IsActive).HasDefaultValue(true);}有什么方法可以重构它,以便我可以HasDefaultValue为所有实体进行设置,ISoftDeletable而不是像上面那样做?我可能可以使用每个实体的默认构造函数解决这个特定情况,IsActive = true甚至创建一个基本抽象类,但我不太喜欢它。有没有更好的办法?
查看完整描述

2 回答

?
慕虎7371278

TA贡献1802条经验 获得超4个赞

我在这里找到了一些答案:GetEntityTypes: configure entity properties using the generic version of .Property<TEntity> in EF Core


除了上面的评论之外,还有一种方法可以不用为每个实体调用它。这可能可以重构为一些扩展方法,正如 Erndob 在我的问题下的评论所提到的。


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())

    {

        if (typeof(ISoftDeletable).IsAssignableFrom(entityType.ClrType))

        {

            modelBuilder.Entity(entityType.ClrType).Property<bool>(nameof(ISoftDeletable.IsActive)).HasDefaultValue(true);

        }

    }

}

解决方案是使用ModelBuilder.Model.GetEntityTypes()和查找可从ISoftDeletable.


在我看来,这比手动配置它甚至创建一个抽象IEntityTypeConfiguration<>类要好得多,因为您不必记住对所有ISoftDeletable类都使用它。


更干净的外观:


public static class ModelBuilderExtensions

{

    public static ModelBuilder EntitiesOfType<T>(this ModelBuilder modelBuilder,

        Action<EntityTypeBuilder> buildAction) where T : class

    {

        return modelBuilder.EntitiesOfType(typeof(T), buildAction);

    }


    public static ModelBuilder EntitiesOfType(this ModelBuilder modelBuilder, Type type,

        Action<EntityTypeBuilder> buildAction)

    {

        foreach (var entityType in modelBuilder.Model.GetEntityTypes())

            if (type.IsAssignableFrom(entityType.ClrType))

                buildAction(modelBuilder.Entity(entityType.ClrType));


        return modelBuilder;

    }

}

并且OnModelCreating:


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    modelBuilder.EntitiesOfType<ISoftDeletable>(builder =>

    {

        builder.Property<bool>(nameof(ISoftDeletable.IsActive)).HasDefaultValue(true);


        // query filters :)

        var param = Expression.Parameter(builder.Metadata.ClrType, "p");

        var body = Expression.Equal(Expression.Property(param, nameof(ISoftDeletable.IsActive)), Expression.Constant(true));

        builder.HasQueryFilter(Expression.Lambda(body, param));

    });

}


查看完整回答
反对 回复 2021-11-21
?
白板的微信

TA贡献1883条经验 获得超3个赞

我想做类似的事情,但使用IEntityTypeConfiguration界面来保存我的通用配置。我最终不得不使用反射,但它有效:


Interface:


public interface IHasDisplayId

{

    Guid DisplayId { get; }

}

EntityTypeConfig:


public class HasDisplayIdEntityTypeConfiguration<T> : IEntityTypeConfiguration<T> where T : class, IHasDisplayId

{

    public void Configure(EntityTypeBuilder<T> builder)

    {

        builder.Property(e => e.DisplayId).IsRequired();

        builder.HasIndex(e => e.DisplayId);

    }

}

Extension method:


public static ModelBuilder ApplyConfiguration<T>(this ModelBuilder modelBuilder, Type configurationType, Type entityType)

{

    if (typeof(T).IsAssignableFrom(entityType))

    {

        // Build IEntityTypeConfiguration type with generic type parameter

        var configurationGenericType = configurationType.MakeGenericType(entityType);

        // Create an instance of the IEntityTypeConfiguration implementation

        var configuration = Activator.CreateInstance(configurationGenericType);

        // Get the ApplyConfiguration method of ModelBuilder via reflection

        var applyEntityConfigurationMethod = typeof(ModelBuilder)

            .GetMethods()

            .Single(e => e.Name == nameof(ModelBuilder.ApplyConfiguration)

                         && e.ContainsGenericParameters

                         && e.GetParameters().SingleOrDefault()?.ParameterType.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>));

        // Create a generic ApplyConfiguration method with our entity type

        var target = applyEntityConfigurationMethod.MakeGenericMethod(entityType);

        // Invoke ApplyConfiguration, passing our IEntityTypeConfiguration instance

        target.Invoke(modelBuilder, new[] { configuration });

    }


    return modelBuilder;

}

Usage:


protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())

    {

        modelBuilder.ApplyConfiguration<IHasDisplayId>(typeof(HasDisplayIdEntityTypeConfiguration<>), entityType.ClrType);

    }

}


查看完整回答
反对 回复 2021-11-21
  • 2 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信