public INHibernateProxy GetProxy(object id, ISessionImplementor session)
    {
        INHibernateProxy proxy;
        try
        {
            object generatedProxy;
            LazyInitializer initializer = new LazyInitializer(_entityName, _persistentClass, id, _getIdentifierMethod, _setIdentifierMethod, _componentIdType, session);
            IInterceptor[] interceptors = new IInterceptor[] { initializer };

            object[] args;
            if (_isClassProxy)
            {
                args = new object[] { interceptors };
            }
            else
            {
                args = new object[] { interceptors, new object() };
            }
            generatedProxy = Activator.CreateInstance(_proxyType, args);

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

        return proxy;
    }
示例#2
0
		public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
		{
			try
			{
				var initializer = new LazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod, SetIdentifierMethod,
				                                      ComponentIdType, session);

				object proxyInstance = IsClassProxy ? factory.CreateProxy(PersistentClass, initializer, Interfaces)
					: factory.CreateProxy(Interfaces[0], initializer, Interfaces);

				return (INHibernateProxy) proxyInstance;
			}
			catch (Exception ex)
			{
				log.Error("Creating a proxy instance failed", ex);
				throw new HibernateException("Creating a proxy instance failed", ex);
			}
		}
		/// <summary>
		/// associate a proxy that was instantiated by another session with this session
		/// </summary>
		/// <param name="li"></param>
		/// <param name="proxy"></param>
		private void ReassociateProxy( LazyInitializer li, INHibernateProxy proxy )
		{
			if( li.Session != this )
			{
				IClassPersister persister = GetClassPersister( li.PersistentClass );
				Key key = new Key( li.Identifier, persister );
				if( !proxiesByKey.Contains( key ) )
				{
					proxiesByKey[ key ] = proxy; // any earlier proxy takes precedence 
				}
				NHibernateProxyHelper.GetLazyInitializer( proxy ).Session = this;
			}
		}