使用 IInterceptor 从 nhibernate 映射过滤实体时出现问题

发布于 2024-09-28 22:43:53 字数 1247 浏览 2 评论 0原文

我有一组实现接口的实体:

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 技术交流群。

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

发布评论

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

评论(1

遗忘曾经 2024-10-05 22:43:53

一种方法是使用存储库来结束会话。在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文