defaultupdateeventlistener.cs

来自「NHibernate NET开发者所需的」· CS 代码 · 共 52 行

CS
52
字号
using System;
using NHibernate.Engine;
using NHibernate.Persister.Entity;

namespace NHibernate.Event.Default
{
	/// <summary> An event handler for update() events</summary>
	[Serializable]
	public class DefaultUpdateEventListener : DefaultSaveOrUpdateEventListener
	{
		protected override object PerformSaveOrUpdate(SaveOrUpdateEvent @event)
		{
			// this implementation is supposed to tolerate incorrect unsaved-value
			// mappings, for the purpose of backward-compatibility
			EntityEntry entry = @event.Session.PersistenceContext.GetEntry(@event.Entity);
			if (entry != null)
			{
				if (entry.Status == Status.Deleted)
				{
					throw new ObjectDeletedException("deleted instance passed to update()", null, @event.EntityName);
				}
				else
				{
					return EntityIsPersistent(@event);
				}
			}
			else
			{
				EntityIsDetached(@event);
				return null;
			}
		}

		/// <summary> 
		/// If the user specified an id, assign it to the instance and use that, 
		/// otherwise use the id already assigned to the instance
		/// </summary>
		protected override object GetUpdateId(object entity, IEntityPersister persister, object requestedId, EntityMode entityMode)
		{
			if (requestedId == null)
			{
				return base.GetUpdateId(entity, persister, requestedId, entityMode);
			}
			else
			{
				persister.SetIdentifier(entity, requestedId, entityMode);
				return requestedId;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?