使用 IInterceptor 从 nhibernate 映射过滤实体时出现问题
我有一组实现接口的实体:
public interface ILocalised
{
Culture Culture { get; }
}
由于许多复杂的原因,我需要过滤从数据库返回后不具有正确区域性的实体(即我无法使用过滤器)。我立即想到的是创建一个拦截器来过滤任何不具有正确文化的实体,例如
public class LocalisationInterceptor : EmptyInterceptor
{
public override object Instantiate(string clazz, NHibernate.EntityMode entityMode, object id)
{
var entity = base.Instantiate(clazz, entityMode, id); //Returns null already
if ((entity is ILocalised) && false == IsValidCulture((ILocalised)entity))
{
return null;
}
return base.Instantiate(clazz, entityMode, id);
}
public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
{
if((entity is ILocalised) && false == IsValidCulture((ILocalised)entity))
{
entity = null;
return false;
}
return base.OnLoad(entity, id, state, propertyNames, types);
}
private bool IsValidCulture(ILocalised localisedEntity)
{
return localisedEntity.Culture == Culture.En;
}
}
但是到目前为止,无论我尝试覆盖它的什么方法都将始终返回该实体。
有谁知道如何防止某些实体加载到拦截器或任何其他解决方案中?
I have a set of entities that implement an interface:
public interface ILocalised
{
Culture Culture { get; }
}
For lots of complicated reasons I need to filter entities which do not have the correct culture after they are returned back from the DB (i.e. I can't use a Filter). My immediate thought was to create an interceptor that would filter any entities that did not have the correct culture, e.g.
public class LocalisationInterceptor : EmptyInterceptor
{
public override object Instantiate(string clazz, NHibernate.EntityMode entityMode, object id)
{
var entity = base.Instantiate(clazz, entityMode, id); //Returns null already
if ((entity is ILocalised) && false == IsValidCulture((ILocalised)entity))
{
return null;
}
return base.Instantiate(clazz, entityMode, id);
}
public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
{
if((entity is ILocalised) && false == IsValidCulture((ILocalised)entity))
{
entity = null;
return false;
}
return base.OnLoad(entity, id, state, propertyNames, types);
}
private bool IsValidCulture(ILocalised localisedEntity)
{
return localisedEntity.Culture == Culture.En;
}
}
However so far, what ever method I try to override it will always return the entity.
Does anyone have any ideas how to prevent certain entities from being loaded in an interceptor or any other solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是使用存储库来结束会话。在Repository.Search方法中,当返回结果时,进行过滤。所以基本上你在 nHibernate 完成后在你自己的代码中进行过滤。
One way is to wrap up the Session using say a Repository. In the Repository.Search method, when the results are returned, do the filtering. So basically you are doing the filtering in your own code after nHibernate is done.