You should be able to provide references to objects already in memory by implementing IInterceptor.Instantiate. We did exactly that -- keep a cache of WeakReferences, periodically pruning dead references, to avoid endless hassles with NonUniqueObjectException when we try to reattach entities to new sessions and somehow something else has already put a different instance of the same type & id in the session.
Our initial testing with this approach came out clean, it eliminated the NonUniqueObjectException problem, but we never enabled this logic for production use. We were concerned that some field values might get leftover if we recycle instances this way -- if either NHibernate doesn't set every field on every load, and because we have some non-persisted fields in our entities that might not get cleared.
A better approach would be if the session's 1st level cache implementation could be replaced. This would would not only seem cleaner, but (I think) it would handle the case where a proxy is created. IInterceptor.Instantiate() doesn't get called until a proxy is initialized, so it can't prevent a different proxy instance from getting created.
|