使用 NHibernate 在延迟加载场景中使用 Castle.DynamicProxy 实现 IDataErrorInfo

发布于 2024-10-09 05:16:33 字数 292 浏览 0 评论 0原文

我已经使用 Castle.DynamicProxy IIterceptor 实现了 IDataErrorInfo 接口。我还实现了一个 NHibernate 拦截器,它使用该拦截器实例化我的实体。问题在于延迟加载的实体。这些是使用 nhibernate 配置文件中指定的代理工厂类构造的,它显然不提供 IDataErrorInfo 实现。该代理通过我的拦截器掩盖了 IDataErrorInfo 的底层实现,这导致验证失败。

这个问题有哪些可能的解决方案?

(解决该问题的一种方法是更改​​ nhibernate 使用的默认代理工厂。)

I have implemented IDataErrorInfo interface using Castle.DynamicProxy IIterceptor. I have also implemented a NHibernate interceptor which instantiates my entities using this interceptor. The problem is with lazy loaded entities. These are constructed using a proxy factory class specified in nhibernate config file, which obviously does not provide IDataErrorInfo implementation. This proxies are masking the underlying implementation of IDataErrorInfo by my interceptor which causes the validation to fail.

What are the posible solutions of this problem?

(One way to solve the problem would be to change the default proxy factory which nhibernate uses.)

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

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

发布评论

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

评论(1

夏花。依旧 2024-10-16 05:16:33

一种解决方案是覆盖默认的 Castle 代理工厂并使用 IDataErrorInfo 扩展接口数组。 LazyInitializer 实际上并没有实现这个接口,它只是将调用转发到我们自己的代理实现。然后,DataBindingProxyFactory 必须注册为 NHibernate 代理工厂。

 public class DataBindingProxyFactory : ProxyFactory, IProxyFactoryFactory
  {
    public IProxyValidator ProxyValidator { get { return new DynProxyTypeValidator(); } }

    public IProxyFactory BuildProxyFactory()
    {
      return new DataBindingProxyFactory();
    }

    public bool IsInstrumented(Type entityClass)
    {
      return true;
    }

    public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
    {
      try
      {
        var initializer = new LazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod,
          SetIdentifierMethod, ComponentIdType, session);

        var interfaces =
          new List<Type>(Interfaces){
            typeof (INotifyPropertyChanged),
            typeof (IDataErrorInfo)
          }.ToArray();

        var generatedProxy = IsClassProxy
          ? DefaultProxyGenerator.CreateClassProxy(PersistentClass, interfaces, initializer)
          : DefaultProxyGenerator.CreateInterfaceProxyWithoutTarget(Interfaces[0], interfaces,
            initializer);

        initializer._constructed = true;
        return (INHibernateProxy) generatedProxy;
      }
      catch (Exception e)
      {
        log.Error("Creating a proxy instance failed", e);
        throw new HibernateException("Creating a proxy instance failed", e);
      }
    }
  }

One solution is to override the default Castle proxy factory and extend the interfaces array with IDataErrorInfo. The LazyInitializer does not actualy implement this interface, it only forwards calls to our own proxy implementation. The DataBindingProxyFactory must then be registered as NHibernate proxy factory.

 public class DataBindingProxyFactory : ProxyFactory, IProxyFactoryFactory
  {
    public IProxyValidator ProxyValidator { get { return new DynProxyTypeValidator(); } }

    public IProxyFactory BuildProxyFactory()
    {
      return new DataBindingProxyFactory();
    }

    public bool IsInstrumented(Type entityClass)
    {
      return true;
    }

    public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
    {
      try
      {
        var initializer = new LazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod,
          SetIdentifierMethod, ComponentIdType, session);

        var interfaces =
          new List<Type>(Interfaces){
            typeof (INotifyPropertyChanged),
            typeof (IDataErrorInfo)
          }.ToArray();

        var generatedProxy = IsClassProxy
          ? DefaultProxyGenerator.CreateClassProxy(PersistentClass, interfaces, initializer)
          : DefaultProxyGenerator.CreateInterfaceProxyWithoutTarget(Interfaces[0], interfaces,
            initializer);

        initializer._constructed = true;
        return (INHibernateProxy) generatedProxy;
      }
      catch (Exception e)
      {
        log.Error("Creating a proxy instance failed", e);
        throw new HibernateException("Creating a proxy instance failed", e);
      }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文