简体   繁体   English

NHibernate,代理和平等

[英]NHibernate, proxies and equality

I'm using NHibernate 3.3.1 and found very interesting problem. 我正在使用NHibernate 3.3.1并发现了非常有趣的问题。 In my domain model I defined two classes: Carriage and CarriageRequest, referencing each other. 在我的域模型中,我定义了两个类:Carriage和CarriageRequest,互相引用。 Because lazy loading is turned on, when I access carriage.CarriageRequest - it points to a proxy object. 因为延迟加载是打开的,所以当我访问carriage.CarriageRequest时 - 它指向一个代理对象。 That is okay. 没关系。 But when I invoke any method, defined in CarriageRequest class, 'this' references to a third object instance. 但是当我调用CarriageRequest类中定义的任何方法时,“this”引用第三个对象实例。

Example: 例:

class CarriageRequest
{
  public virtual void Test(CarriageRequest instance)
  {
    Debug.WriteLine(Object.ReferenceEquals(this, instance)); // prints FALSE
  }
}

class Carriage
{ 
  public virtual CarriageRequest CarriageRequest { get; set; }
}

...
var carriage = session.Get<Carriage>(123);
carriage.CarriageRequest.Test(carriage.CarriageRequest);

So it looks like NHibernate proxies wrap original objects and forward all method invocations to wrapped objects. 所以它看起来像NHibernate代理包装原始对象并将所有方法调用转发给包装对象。 How can I use "==" in this case? 在这种情况下如何使用“==”? I need to do something like this: 我需要做这样的事情:

var shipment = (from sh in Carriage.Shipments where sh.CarriageRequest == this & sh.Warehouse == waybill.Warehouse select sh).FirstOrDefault();

This code is executed inside proxied object, so 'shipment' is always null, because 'sh.CarriageRequest == this' is always false. 此代码代理对象执行,因此'shipment'始终为null,因为'sh.CarriageRequest == this'始终为false。 Because sh.CarriageRequest is a proxy, and this is a wrapped original instance. 因为sh.CarriageRequest是一个代理,这是一个包装的原始实例。

Okay, NHibernate guarantees reference equality for loaded objects within the same session. 好的,NHibernate保证同一会话中加载对象的引用相等。 And if using lazy-loading, it wraps real object with a proxy and every method invocation on a proxy is forwarded to the wrapped object. 如果使用延迟加载,它会使用代理包装真实对象,并且代理上的每个方法调用都会转发到包装对象。 So, inside this object every reference comparison between 'this' and the same object, loaded from session, will fail (because it will be a different, proxy, object). 因此,在此对象中,“this”与从会话加载的同一对象之间的每个引用比较都将失败(因为它将是一个不同的代理对象)。

So, the only way to resolve this issue is to use Equals method or compare by primary keys. 因此,解决此问题的唯一方法是使用Equals方法或按主键进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 NHibernate 中获取正确类型的代理 - Getting proxies of the correct type in NHibernate nhibernate您可以不用代理来延迟加载吗? - nhibernate can you lazyload without proxies? 在NHibernate中与null比较时强制相等运算符 - Force equality operator when comparing to null in NHibernate 使用NHibernate插入或保存时为Object Proxies提供什么值 - What value to give for Object Proxies when Inserting or Saving using NHibernate NHibernate:无法通过无状态会话错误消息获取代理 - NHibernate: proxies cannot be fetched by a stateless session error message 在将Castle Proxies与NHibernate结合使用时,这不会返回相同的参考 - This isn't returning the same reference when using Castle Proxies with NHibernate NHibernate.InvalidProxyTypeException: 以下类型不能用作代理 - NHibernate.InvalidProxyTypeException: The following types may not be used as proxies 使用Nhibernate时,检查C#.Net中的列表相等性是否正常 - Checking of List equality in C# .Net not working when using Nhibernate 使用虚拟属性来支持 NHibernate 代理; ReSharper 警告构造函数中的虚拟成员调用 - Using virtual properties to support NHibernate proxies; ReSharper warns of virtual member call in constructor 使用ReferencesAny时如何配置Fluent NHibernate以返回空值而不是丢失对象的代理 - How to configure Fluent NHibernate to return null instead of proxies for missing objects when using ReferencesAny
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM