Fluent NHibernate 如何重写抽象基类的映射

发布于 2024-12-04 10:21:04 字数 616 浏览 7 评论 0原文

我想对我的所有 Types Of AuditedEntity 执行此操作,但正如我们告诉 FH 忽略基本摘要一样,代码不会受到影响。我不想对我的所有实体执行此操作,然后让某人在添加新的 typeof

public abstract class AuditedEntity : Entity ...

public class AuditedEntityMappings : IAutoMappingOverride<AuditedEntity>
  {
    public void Override(AutoMapping<AuditedEntity> mapping)
    {
      mapping.Where("DeletedById is null");
    }
  }

这篇文章看起来很有希望,但该方法已被弃用

I want to do this for all my Types Of AuditedEntity, but as we've told FH to ignore base abstracts, the code isnt getting hit. i dont want to do this for all my entities and then have someone forget when they add a new typeof<AuditedEntity>

public abstract class AuditedEntity : Entity ...

public class AuditedEntityMappings : IAutoMappingOverride<AuditedEntity>
  {
    public void Override(AutoMapping<AuditedEntity> mapping)
    {
      mapping.Where("DeletedById is null");
    }
  }

This post looked promising but that method is deprecated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海少女心 2024-12-11 10:21:04

感谢对一些相当复杂的表达式的一些帮助,导致了以下扩展:

var model = 
  AutoMap.AssemblyOf<AuditedEntity>(new AutomappingConfiguration(databaseName))
   .HideDeletedEntities();

...

public static class ReflectiveEnumerator
  {
    public static IEnumerable<Type> GetSubTypesOf<T>() where T : class
    {
      return Assembly.GetAssembly(typeof (T)).GetTypes()
        .Where(myType => myType.IsClass && 
          !myType.IsAbstract && 
          myType.IsSubclassOf(typeof (T)));
    }
  }

  public static class AutoPersistenceModelExtensions
  {
    public static AutoPersistenceModel HideDeletedEntities(this AutoPersistenceModel model)
    {
      var types = ReflectiveEnumerator.GetSubTypesOf<AuditedEntity>();

      foreach (var t in types)
      {
        var mapped = typeof(AutoMapping<>).MakeGenericType(t);

        var p = Expression.Parameter(mapped, "m");
        var expression = Expression.Lambda(Expression.GetActionType(mapped),
                                           Expression.Call(p, mapped.GetMethod("Where"),
                                                           Expression.Constant("DeletedById is null")), p);

        typeof(AutoPersistenceModel).GetMethod("Override").MakeGenericMethod(t)
          .Invoke(model, new object[] { expression.Compile() });
      }
      return model;
    }
  }

Got it working thanks to some help on some reasonably complex expressions leading to the following extensions:

var model = 
  AutoMap.AssemblyOf<AuditedEntity>(new AutomappingConfiguration(databaseName))
   .HideDeletedEntities();

...

public static class ReflectiveEnumerator
  {
    public static IEnumerable<Type> GetSubTypesOf<T>() where T : class
    {
      return Assembly.GetAssembly(typeof (T)).GetTypes()
        .Where(myType => myType.IsClass && 
          !myType.IsAbstract && 
          myType.IsSubclassOf(typeof (T)));
    }
  }

  public static class AutoPersistenceModelExtensions
  {
    public static AutoPersistenceModel HideDeletedEntities(this AutoPersistenceModel model)
    {
      var types = ReflectiveEnumerator.GetSubTypesOf<AuditedEntity>();

      foreach (var t in types)
      {
        var mapped = typeof(AutoMapping<>).MakeGenericType(t);

        var p = Expression.Parameter(mapped, "m");
        var expression = Expression.Lambda(Expression.GetActionType(mapped),
                                           Expression.Call(p, mapped.GetMethod("Where"),
                                                           Expression.Constant("DeletedById is null")), p);

        typeof(AutoPersistenceModel).GetMethod("Override").MakeGenericMethod(t)
          .Invoke(model, new object[] { expression.Compile() });
      }
      return model;
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文