[英]Fluent NHibernate how to override mappings for an abstract base class
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. 我想对所有
Types Of AuditedEntity
都执行此Types Of AuditedEntity
,但是正如我们告诉FH忽略基本摘要一样,代码不会受到攻击。 i dont want to do this for all my entities and then have someone forget when they add a new typeof<AuditedEntity>
我不想对我的所有实体执行此操作,然后在添加新的
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 这篇文章看起来很有希望,但该方法已被弃用
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.