From: Michael D. <mik...@us...> - 2004-12-09 17:52:25
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Impl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18504/Impl Modified Files: QueryImpl.cs SessionImpl.cs Log Message: Migrated most Proxy items from proxy branch to this one. Have not moved the actual proxy code. Will do that when the serialization bug in DynamicProxy is fixed. Index: SessionImpl.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Impl/SessionImpl.cs,v retrieving revision 1.54 retrieving revision 1.55 diff -C2 -d -r1.54 -r1.55 *** SessionImpl.cs 4 Dec 2004 22:41:21 -0000 1.54 --- SessionImpl.cs 9 Dec 2004 17:52:15 -0000 1.55 *************** *** 49,52 **** --- 49,61 ---- private IDictionary proxiesByKey; //key=Key, value=HibernateProxy + // these are used to serialize the proxiesByKey Dictionary - I was not able to + // have a Hashtable serialize fully by the time that SessionImpl OnDeserialization + // was getting called - I think I'm not completely understanding the sequence of + // deserialization events. When SessionImpl was getting the OnDeserialization called + // the proxies were not fully deserialized. + private ArrayList tmpProxiesKey; + private ArrayList tmpProxiesProxy; + + //IdentityMaps are serializable in NH private IdentityMap entries;//key=Object, value=EntityEntry *************** *** 628,632 **** this.callAfterTransactionCompletionFromDisconnect = info.GetBoolean("callAfterTransactionCompletionFromDisconnect"); this.entitiesByKey = (IDictionary)info.GetValue( "entitiesByKey", typeof(IDictionary) ); ! this.proxiesByKey = (IDictionary)info.GetValue( "proxiesByKey", typeof(IDictionary) ); this.nullifiables = (ISet)info.GetValue( "nullifiables", typeof(ISet) ); this.interceptor = (IInterceptor)info.GetValue( "interceptor", typeof(IInterceptor) ); --- 637,646 ---- this.callAfterTransactionCompletionFromDisconnect = info.GetBoolean("callAfterTransactionCompletionFromDisconnect"); this.entitiesByKey = (IDictionary)info.GetValue( "entitiesByKey", typeof(IDictionary) ); ! ! // we did not actually serializing the IDictionary but instead the proxies in an arraylist ! //this.proxiesByKey = (IDictionary)info.GetValue( "proxiesByKey", typeof(IDictionary) ); ! tmpProxiesKey = (ArrayList)info.GetValue( "tmpProxiesKey", typeof(ArrayList) ); ! tmpProxiesProxy = (ArrayList)info.GetValue( "tmpProxiesProxy", typeof(ArrayList) ); ! this.nullifiables = (ISet)info.GetValue( "nullifiables", typeof(ISet) ); this.interceptor = (IInterceptor)info.GetValue( "interceptor", typeof(IInterceptor) ); *************** *** 650,659 **** void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { ! if ( IsConnected ) throw new InvalidOperationException("Cannot serialize a Session while connected"); ! if ( insertions.Count!=0 || deletions.Count!=0 ) throw new InvalidOperationException("Cannot serialize a Session which has work waiting to be flushed"); - log.Info("serializing session"); - info.AddValue( "factory", factory, typeof(SessionFactoryImpl) ); info.AddValue( "autoClose", autoClose ); --- 664,679 ---- void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { ! log.Info( "writting session to serializer" ); ! ! if ( IsConnected ) ! { ! throw new InvalidOperationException("Cannot serialize a Session while connected"); ! } ! ! if ( insertions.Count!=0 || deletions.Count!=0 ) ! { throw new InvalidOperationException("Cannot serialize a Session which has work waiting to be flushed"); + } info.AddValue( "factory", factory, typeof(SessionFactoryImpl) ); info.AddValue( "autoClose", autoClose ); *************** *** 663,667 **** info.AddValue( "callAfterTransactionCompletionFromDisconnect", callAfterTransactionCompletionFromDisconnect ); info.AddValue( "entitiesByKey", entitiesByKey, typeof(IDictionary) ); ! info.AddValue( "proxiesByKey", proxiesByKey, typeof(IDictionary) ); info.AddValue( "nullifiables", nullifiables, typeof(ISet) ); info.AddValue( "interceptor", interceptor, typeof(IInterceptor) ); --- 683,702 ---- info.AddValue( "callAfterTransactionCompletionFromDisconnect", callAfterTransactionCompletionFromDisconnect ); info.AddValue( "entitiesByKey", entitiesByKey, typeof(IDictionary) ); ! ! // the IDictionary should not be serialized because the objects inside of it are not ! // fully deserialized until after the session is deserialized. Instead use two ArrayList ! // to hold the values because they don't have the deserialization complexities that ! // hashtables do. ! tmpProxiesKey = new ArrayList( proxiesByKey.Count ); ! tmpProxiesProxy = new ArrayList( proxiesByKey.Count ); ! foreach(DictionaryEntry de in proxiesByKey) ! { ! tmpProxiesKey.Add( de.Key ); ! tmpProxiesProxy.Add( de.Value ); ! } ! ! info.AddValue( "tmpProxiesKey", tmpProxiesKey ); ! info.AddValue( "tmpProxiesProxy", tmpProxiesProxy ); ! info.AddValue( "nullifiables", nullifiables, typeof(ISet) ); info.AddValue( "interceptor", interceptor, typeof(IInterceptor) ); *************** *** 682,686 **** void IDeserializationCallback.OnDeserialization(Object sender) { ! log.Info("deserializing session"); // don't need any section for IdentityMaps because .net does not have a problem --- 717,721 ---- void IDeserializationCallback.OnDeserialization(Object sender) { ! log.Info("OnDeserialization of the session."); // don't need any section for IdentityMaps because .net does not have a problem *************** *** 702,721 **** } } ! ! // TODO: figure out why proxies are having problems. The enumerator appears to be throwing ! // a null reference exception when the proxiesByKey.Count==0 ! // foreach(object proxy in proxiesByKey.Values) ! // { ! // object proxy = proxyEnumer.Current; ! // if (proxy is HibernateProxy) ! // { ! // HibernateProxyHelper.GetLazyInitializer(proxy as HibernateProxy).SetSession(this); ! // } ! // else ! // { ! // // the proxy was pruned during the serialization process ! // proxiesByKey.Remove(proxy); ! // } ! // } foreach(EntityEntry e in entries.Values) --- 737,773 ---- } } ! ! // recreate the proxiesByKey hashtable from the two arraylists. ! proxiesByKey = new Hashtable( tmpProxiesKey.Count ); ! for(int i=0; i<tmpProxiesKey.Count; i++) ! { ! proxiesByKey.Add( tmpProxiesKey[i], tmpProxiesProxy[i] ); ! } ! ! // we can't remove an entry from an IDictionary while enumerating so store the ones ! // to remove in this list ! ArrayList keysToRemove = new ArrayList(); ! ! foreach( DictionaryEntry de in proxiesByKey ) ! { ! object key = de.Key; ! object proxy = de.Value; ! ! if (proxy is INHibernateProxy) ! { ! NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy)proxy ).Session = this; ! } ! else ! { ! // the proxy was pruned during the serialization process because ! // the target had been instantiated. ! keysToRemove.Add( key ); ! } ! } ! ! for( int i=0; i<keysToRemove.Count; i++ ) ! { ! proxiesByKey.Remove( keysToRemove[i] ); ! } foreach(EntityEntry e in entries.Values) *************** *** 849,856 **** public LockMode GetCurrentLockMode(object obj) { ! if ( obj is HibernateProxy ) { ! obj = (HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj)).GetImplementation(this); ! if (obj==null) return LockMode.None; } --- 901,908 ---- public LockMode GetCurrentLockMode(object obj) { ! if ( obj is INHibernateProxy ) { ! obj = (NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj)).GetImplementation(this); ! if( obj==null ) return LockMode.None; } *************** *** 973,977 **** { id = GetPersister(theObj).IdentifierGenerator.Generate(this, theObj); ! if( id == (object) IdentifierGeneratorFactory.ShortCircuitIndicator) return GetIdentifier(theObj); //TODO: yick! } catch (Exception ex) --- 1025,1032 ---- { id = GetPersister(theObj).IdentifierGenerator.Generate(this, theObj); ! if( id == (object) IdentifierGeneratorFactory.ShortCircuitIndicator) ! { ! return GetIdentifier(theObj); //TODO: yick! ! } } catch (Exception ex) *************** *** 1047,1051 **** if (old!=null) { ! if ( GetEntry(old).Status==Status.Deleted) { Flush(); --- 1102,1106 ---- if (old!=null) { ! if ( GetEntry(old).Status==Status.Deleted ) { Flush(); *************** *** 1125,1129 **** AddEntry(obj, Status.Loaded, values, id, Versioning.GetVersion(values, persister), LockMode.Write, identityCol, persister); ! if (!identityCol) insertions.Add( new ScheduledInsertion( id, values, obj, persister, this ) ); // cascade-save to collections AFTER the collection owner was saved --- 1180,1187 ---- AddEntry(obj, Status.Loaded, values, id, Versioning.GetVersion(values, persister), LockMode.Write, identityCol, persister); ! if (!identityCol) ! { ! insertions.Add( new ScheduledInsertion( id, values, obj, persister, this ) ); ! } // cascade-save to collections AFTER the collection owner was saved *************** *** 1143,1148 **** private void ReassociateProxy(Object value) { ! HibernateProxy proxy = (HibernateProxy) value; ! LazyInitializer li = HibernateProxyHelper.GetLazyInitializer(proxy); ReassociateProxy(li, proxy); } --- 1201,1206 ---- private void ReassociateProxy(Object value) { ! INHibernateProxy proxy = (INHibernateProxy) value; ! LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer(proxy); ReassociateProxy(li, proxy); } *************** *** 1150,1157 **** private object UnproxyAndReassociate(object maybeProxy) { ! if ( maybeProxy is HibernateProxy ) { ! HibernateProxy proxy = (HibernateProxy) maybeProxy; ! LazyInitializer li = HibernateProxyHelper.GetLazyInitializer(proxy); ReassociateProxy(li, proxy); return li.GetImplementation(); //initialize + unwrap the object --- 1208,1215 ---- private object UnproxyAndReassociate(object maybeProxy) { ! if ( maybeProxy is INHibernateProxy ) { ! INHibernateProxy proxy = (INHibernateProxy) maybeProxy; ! LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer(proxy); ReassociateProxy(li, proxy); return li.GetImplementation(); //initialize + unwrap the object *************** *** 1168,1172 **** /// <param name="li"></param> /// <param name="proxy"></param> ! private void ReassociateProxy(LazyInitializer li, HibernateProxy proxy) { if ( li.Session!=this ) --- 1226,1230 ---- /// <param name="li"></param> /// <param name="proxy"></param> ! private void ReassociateProxy(LazyInitializer li, INHibernateProxy proxy) { if ( li.Session!=this ) *************** *** 1174,1179 **** IClassPersister persister = GetPersister( li.PersistentClass ); Key key = new Key( li.Identifier, persister ); ! if ( !proxiesByKey.Contains(key) ) proxiesByKey[key] = proxy; // any earlier proxy takes precedence ! HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) proxy ).SetSession(this); } } --- 1232,1240 ---- IClassPersister persister = GetPersister( li.PersistentClass ); Key key = new Key( li.Identifier, persister ); ! if ( !proxiesByKey.Contains(key) ) ! { ! proxiesByKey[key] = proxy; // any earlier proxy takes precedence ! } ! NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) proxy ).Session = this; } } *************** *** 1239,1246 **** private bool IsUnsaved(object obj, bool earlyInsert, object self) { ! if ( obj is HibernateProxy ) { ! // if its an uninitialized proxy, it can't be transietn ! LazyInitializer li = HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj ); if ( li.GetImplementation(this)==null ) { --- 1300,1307 ---- private bool IsUnsaved(object obj, bool earlyInsert, object self) { ! if ( obj is INHibernateProxy ) { ! // if its an uninitialized proxy, it can't be transient ! LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj ); if ( li.GetImplementation(this)==null ) { *************** *** 1714,1722 **** if (obj==null) throw new NullReferenceException("attempted to update null"); ! if ( obj is HibernateProxy ) { ! object pid = HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj ).Identifier; ! if( !id.Equals(pid) ) throw new HibernateException("The given proxy had a different identifier value to the given identifier: " + pid + "!=" + id); } --- 1775,1785 ---- if (obj==null) throw new NullReferenceException("attempted to update null"); ! if ( obj is INHibernateProxy ) { ! object pid = NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj ).Identifier; ! if( !id.Equals( pid ) ) ! { throw new HibernateException("The given proxy had a different identifier value to the given identifier: " + pid + "!=" + id); + } } *************** *** 2108,2113 **** else { ! //TODO: Get the proxy - there is some CGLIB code here ! proxy = null; proxiesByKey[key] = proxy; --- 2171,2182 ---- else { ! IProxyGenerator generator = ProxyGeneratorFactory.GetProxyGenerator(); ! proxy = generator.GetProxy( ! p.MappedClass ! , p.ConcreteProxyClass ! , p.ProxyInterfaces ! , p.ProxyIdentifierProperty ! , key.Identifier ! , this ); proxiesByKey[key] = proxy; *************** *** 2309,2322 **** // return new uninitailzed proxy - //TODO: commented this out so we could get all of the test running - this basically makes - // the proxy of a class be ignored - which is fine until we have it working. if ( persister.HasProxy ) { ! proxy = null; //TODO: Create the proxy ! // this is the spot that is causing the problems with FooBarTest.FetchInitializedCollection ! // when the following code "Assert.IsTrue( baz.fooBag.Count==2 );" is being executed. This ! // is causing a null value to be returned when a "Proxied" version of the class is expected. ! // So the method ThrowObjectNotFound is throwing an exception because it is given a null object ! // - hence the error looks like it can't find a row in the DB. } proxiesByKey[key] = proxy; --- 2378,2385 ---- // return new uninitailzed proxy if ( persister.HasProxy ) { ! IProxyGenerator generator = ProxyGeneratorFactory.GetProxyGenerator(); ! proxy = generator.GetProxy( clazz, persister.ConcreteProxyClass, persister.ProxyInterfaces, persister.ProxyIdentifierProperty, id, this ); } proxiesByKey[key] = proxy; *************** *** 2737,2741 **** Status status = entry.Status; ! if (status != Status.Loading && status != Status.Gone) { object obj = me.Key; --- 2800,2804 ---- Status status = entry.Status; ! if( status!=Status.Loading && status!=Status.Gone ) { object obj = me.Key; *************** *** 2760,2764 **** object[] values; ! if ( status==Status.Deleted) { //grab its state saved at deletion --- 2823,2827 ---- object[] values; ! if( status==Status.Deleted ) { //grab its state saved at deletion *************** *** 2787,2791 **** int[] dirtyProperties = interceptor.FindDirty(obj, entry.Id, values, entry.LoadedState, persister.PropertyNames, types); ! if ( dirtyProperties==null ) { // interceptor returned null, so do the dirtycheck ourself, if possible --- 2850,2854 ---- int[] dirtyProperties = interceptor.FindDirty(obj, entry.Id, values, entry.LoadedState, persister.PropertyNames, types); ! if( dirtyProperties==null ) { // interceptor returned null, so do the dirtycheck ourself, if possible *************** *** 2830,2834 **** obj, entry.Id, values, entry.LoadedState, persister.PropertyNames, types); ! //no we might need to recalculate the dirtyProperties array if(intercepted && !cannotDirtyCheck && !interceptorHandledDirtyCheck) { --- 2893,2897 ---- obj, entry.Id, values, entry.LoadedState, persister.PropertyNames, types); ! //now we might need to recalculate the dirtyProperties array if(intercepted && !cannotDirtyCheck && !interceptorHandledDirtyCheck) { *************** *** 2940,2947 **** public object GetIdentifier(object obj) { ! if (obj is HibernateProxy) { ! LazyInitializer li = HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj ); ! if ( li.Session!=this ) throw new TransientObjectException("The proxy was not associated with this session"); return li.Identifier; } --- 3003,3013 ---- public object GetIdentifier(object obj) { ! if (obj is INHibernateProxy) { ! LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj ); ! if ( li.Session!=this ) ! { ! throw new TransientObjectException("The proxy was not associated with this session"); ! } return li.Identifier; } *************** *** 2962,2968 **** public object GetEntityIdentifier(object obj) { ! if (obj is HibernateProxy) { ! return HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj ).Identifier; } else --- 3028,3034 ---- public object GetEntityIdentifier(object obj) { ! if( obj is INHibernateProxy ) { ! return NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj ).Identifier; } else *************** *** 2975,2979 **** public bool IsSaved(object obj) { ! if(obj is HibernateProxy) return true; EntityEntry entry = GetEntry(obj); --- 3041,3045 ---- public bool IsSaved(object obj) { ! if(obj is INHibernateProxy) return true; EntityEntry entry = GetEntry(obj); *************** *** 3003,3009 **** if (obj==null) return null; ! if (obj is HibernateProxy) { ! return HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj ).Identifier; } else --- 3069,3075 ---- if (obj==null) return null; ! if (obj is INHibernateProxy) { ! return NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj ).Identifier; } else *************** *** 4049,4055 **** public bool Contains(object obj) { ! if (obj is HibernateProxy) { ! return HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj ).Session==this; } else --- 4115,4121 ---- public bool Contains(object obj) { ! if (obj is INHibernateProxy) { ! return NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj ).Session==this; } else *************** *** 4066,4072 **** public void Evict(object obj) { ! if (obj is HibernateProxy) { ! LazyInitializer li = HibernateProxyHelper.GetLazyInitializer( (HibernateProxy) obj ); object id = li.Identifier; IClassPersister persister = GetPersister( li.PersistentClass ); --- 4132,4138 ---- public void Evict(object obj) { ! if (obj is INHibernateProxy) { ! LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer( (INHibernateProxy) obj ); object id = li.Identifier; IClassPersister persister = GetPersister( li.PersistentClass ); Index: QueryImpl.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Impl/QueryImpl.cs,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** QueryImpl.cs 4 Nov 2004 04:46:13 -0000 1.16 --- QueryImpl.cs 9 Dec 2004 17:52:15 -0000 1.17 *************** *** 163,170 **** return this; } ! public IQuery SetEntity(int position, object val) { ! SetParameter(position, val, NHibernate.Entity( HibernateProxyHelper.GetClass(val))); return this; } public IQuery SetEnum(int position, System.Enum val) { SetParameter(position, val, NHibernate.Enum( val.GetType() ) ); --- 163,173 ---- return this; } ! ! public IQuery SetEntity(int position, object val) ! { ! SetParameter( position, val, NHibernate.Entity( NHibernateProxyHelper.GetClass( val ) ) ); return this; } + public IQuery SetEnum(int position, System.Enum val) { SetParameter(position, val, NHibernate.Enum( val.GetType() ) ); *************** *** 236,240 **** } public IQuery SetEntity(string name, object val) { ! SetParameter(name, val, NHibernate.Entity( HibernateProxyHelper.GetClass( val ) ) ); return this; } --- 239,243 ---- } public IQuery SetEntity(string name, object val) { ! SetParameter(name, val, NHibernate.Entity( NHibernateProxyHelper.GetClass( val ) ) ); return this; } *************** *** 255,259 **** private IType GuessType(object param) { ! System.Type clazz = HibernateProxyHelper.GetClass(param); return GuessType(clazz); } --- 258,262 ---- private IType GuessType(object param) { ! System.Type clazz = NHibernateProxyHelper.GetClass(param); return GuessType(clazz); } |