Пример #1
0
        /// <summary>
        /// Prepares the save call using a newly generated id.
        /// </summary>
        /// <param name="entity">The entity to be saved </param>
        /// <param name="entityName">The entity-name for the entity to be saved </param>
        /// <param name="anything">Generally cascade-specific information. </param>
        /// <param name="source">The session which is the source of this save event. </param>
        /// <param name="requiresImmediateIdAccess">
        /// does the event context require
        /// access to the identifier immediately after execution of this method (if
        /// not, post-insert style id generators may be postponed if we are outside
        /// a transaction).
        /// </param>
        /// <returns>
        /// The id used to save the entity; may be null depending on the
        /// type of id generator used and the requiresImmediateIdAccess value
        /// </returns>
        protected virtual object SaveWithGeneratedId(object entity, string entityName, object anything, IEventSource source, bool requiresImmediateIdAccess)
        {
            IEntityPersister persister   = source.GetEntityPersister(entityName, entity);
            object           generatedId = persister.IdentifierGenerator.Generate(source, entity);

            if (generatedId == null)
            {
                throw new IdentifierGenerationException("null id generated for:" + entity.GetType());
            }
            else if (generatedId == IdentifierGeneratorFactory.ShortCircuitIndicator)
            {
                return(source.GetIdentifier(entity));
            }
            else if (generatedId == IdentifierGeneratorFactory.PostInsertIndicator)
            {
                return(PerformSave(entity, null, persister, true, anything, source, requiresImmediateIdAccess));
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Format("generated identifier: {0}, using strategy: {1}",
                                            persister.IdentifierType.ToLoggableString(generatedId, source.Factory),
                                            persister.IdentifierGenerator.GetType().FullName));
                }
                return(PerformSave(entity, generatedId, persister, false, anything, source, true));
            }
        }
        /// <summary>
        /// Prepares the save call using a newly generated id.
        /// </summary>
        /// <param name="entity">The entity to be saved </param>
        /// <param name="entityName">The entity-name for the entity to be saved </param>
        /// <param name="anything">Generally cascade-specific information. </param>
        /// <param name="source">The session which is the source of this save event. </param>
        /// <param name="requiresImmediateIdAccess">
        /// does the event context require
        /// access to the identifier immediately after execution of this method (if
        /// not, post-insert style id generators may be postponed if we are outside
        /// a transaction).
        /// </param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
        /// <returns>
        /// The id used to save the entity; may be null depending on the
        /// type of id generator used and the requiresImmediateIdAccess value
        /// </returns>
        protected virtual async Task <object> SaveWithGeneratedIdAsync(object entity, string entityName, object anything, IEventSource source, bool requiresImmediateIdAccess, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            IEntityPersister persister   = source.GetEntityPersister(entityName, entity);
            object           generatedId = await(persister.IdentifierGenerator.GenerateAsync(source, entity, cancellationToken)).ConfigureAwait(false);

            if (generatedId == null)
            {
                throw new IdentifierGenerationException("null id generated for:" + entity.GetType());
            }
            else if (generatedId == IdentifierGeneratorFactory.ShortCircuitIndicator)
            {
                return(source.GetIdentifier(entity));
            }
            else if (generatedId == IdentifierGeneratorFactory.PostInsertIndicator)
            {
                return(await(PerformSaveAsync(entity, null, persister, true, anything, source, requiresImmediateIdAccess, cancellationToken)).ConfigureAwait(false));
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Format("generated identifier: {0}, using strategy: {1}",
                                            persister.IdentifierType.ToLoggableString(generatedId, source.Factory),
                                            persister.IdentifierGenerator.GetType().FullName));
                }
                return(await(PerformSaveAsync(entity, generatedId, persister, false, anything, source, true, cancellationToken)).ConfigureAwait(false));
            }
        }
		/// <summary> 
		/// Prepares the save call using a newly generated id. 
		/// </summary>
		/// <param name="entity">The entity to be saved </param>
		/// <param name="entityName">The entity-name for the entity to be saved </param>
		/// <param name="anything">Generally cascade-specific information. </param>
		/// <param name="source">The session which is the source of this save event. </param>
		/// <param name="requiresImmediateIdAccess">
		/// does the event context require
		/// access to the identifier immediately after execution of this method (if
		/// not, post-insert style id generators may be postponed if we are outside
		/// a transaction). 
		/// </param>
		/// <returns> 
		/// The id used to save the entity; may be null depending on the
		/// type of id generator used and the requiresImmediateIdAccess value
		/// </returns>
		protected virtual object SaveWithGeneratedId(object entity, string entityName, object anything, IEventSource source, bool requiresImmediateIdAccess)
		{
			IEntityPersister persister = source.GetEntityPersister(entityName, entity);
			object generatedId = persister.IdentifierGenerator.Generate(source, entity);
			if (generatedId == null)
			{
				throw new IdentifierGenerationException("null id generated for:" + entity.GetType());
			}
			else if (generatedId == IdentifierGeneratorFactory.ShortCircuitIndicator)
			{
				return source.GetIdentifier(entity);
			}
			else if (generatedId == IdentifierGeneratorFactory.PostInsertIndicator)
			{
				return PerformSave(entity, null, persister, true, anything, source, requiresImmediateIdAccess);
			}
			else
			{
				if (log.IsDebugEnabled)
				{
					log.Debug(string.Format("generated identifier: {0}, using strategy: {1}",
						persister.IdentifierType.ToLoggableString(generatedId, source.Factory),
						persister.IdentifierGenerator.GetType().FullName));
				}
				return PerformSave(entity, generatedId, persister, false, anything, source, true);
			}
		}