private static PropertyDiff[] GetPropertyDiffs(string[] propertyNames, IType[] types, object[] currentState, object[] previousState)
        {
            var diffs = new PropertyDiff[propertyNames.Length];

            for (var i = 0; i < diffs.Length; i++)
            {
                var oldValue = previousState == null ? null : previousState[i];
                var newValue = currentState == null ? null : currentState[i];

                // need to handle collections specially
                if (types[i].IsCollectionType && ReferenceEquals(oldValue, newValue))
                {
                    // the collection instance itself has not changed, but perhaps the content has?
                    // if the oldValue is a persistent collection, then we can get a snapshot from
                    // when the collection was initially loaded, and see if it is dirty
                    if (oldValue is IPersistentCollection && NHibernateUtil.IsInitialized(oldValue))
                    {
                        oldValue = GetCollectionSnapshot(oldValue as IPersistentCollection);
                    }
                }

                diffs[i] = new PropertyDiff(propertyNames[i], types[i], oldValue, newValue);
            }
            return(diffs);
        }
Example #2
0
		/// <summary>
		/// Returns a new change record that represents the total of this change
		/// and the previous change.
		/// </summary>
		/// <param name="previousChange"></param>
		/// <returns></returns>
		public ChangeRecord Compound(ChangeRecord previousChange)
		{
			// assume the propertyDiffs array in both objects is aligned
			var resultDiffs = new PropertyDiff[_propertyDiffs.Length];
			for (var i = 0; i < _propertyDiffs.Length; i++)
			{
				resultDiffs[i] = _propertyDiffs[i].Compound(previousChange.PropertyDiffs[i]);
			}

			// return a new change record that represents the accumulation of both changes
			// the resultant ChangeType depends on whether this change Supercedes previousChange, or vice versa
			return new ChangeRecord(_entity, Supercedes(previousChange) ? _changeType : previousChange._changeType, resultDiffs);
		}
Example #3
0
        /// <summary>
        /// Returns a new change record that represents the total of this change
        /// and the previous change.
        /// </summary>
        /// <param name="previousChange"></param>
        /// <returns></returns>
        public ChangeRecord Compound(ChangeRecord previousChange)
        {
            // assume the propertyDiffs array in both objects is aligned
            var resultDiffs = new PropertyDiff[_propertyDiffs.Length];

            for (var i = 0; i < _propertyDiffs.Length; i++)
            {
                resultDiffs[i] = _propertyDiffs[i].Compound(previousChange.PropertyDiffs[i]);
            }

            // return a new change record that represents the accumulation of both changes
            // the resultant ChangeType depends on whether this change Supercedes previousChange, or vice versa
            return(new ChangeRecord(_entity, Supercedes(previousChange) ? _changeType : previousChange._changeType, resultDiffs));
        }
Example #4
0
		/// <summary>
		/// Records that the specified change occured to the specified entity.
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="changeType"></param>
		/// <param name="propertyDiffs"></param>
		internal void RecordChange(Entity entity, EntityChangeType changeType, PropertyDiff[] propertyDiffs)
		{
			var thisChange = new ChangeRecord(entity, changeType, propertyDiffs);

			// check if this entity was already recorded in the list
			// note that reference equality is used because we do not know how the Equals method of the entity may be implemented
			// for example, it may define equality based on a property whose value has changed, which would break this logic
			var prevRecordIndex = _changeRecords.FindIndex(r => ReferenceEquals(r.Entity, entity));
			if (prevRecordIndex > -1)
			{
				// this entity was already marked as changed
				var previousChange = _changeRecords[prevRecordIndex];

				// compound the previous change record with this one
				_changeRecords[prevRecordIndex] = thisChange.Compound(previousChange);
			}
			else
			{
				// record this change in the change set
				_changeRecords.Add(thisChange);
			}
		}
Example #5
0
		public ChangeRecord(Entity entity, EntityChangeType changeType, PropertyDiff[] propertyDiffs)
		{
			_entity = entity;
			_changeType = changeType;
			_propertyDiffs = propertyDiffs;
		}
Example #6
0
 /// <summary>
 /// Returns a new <see cref="PropertyDiff"/> that is the result of adding this change
 /// to <paramref name="previousChange"/>.
 /// </summary>
 /// <param name="previousChange"></param>
 /// <returns></returns>
 /// <remarks>
 /// This operation is not commutative.
 /// </remarks>
 public PropertyDiff Compound(PropertyDiff previousChange)
 {
     return(new PropertyDiff(_propertyName, _hibernateType, previousChange._oldValue, _newValue));
 }
Example #7
0
		/// <summary>
		/// Returns a new <see cref="PropertyDiff"/> that is the result of adding this change
		/// to <paramref name="previousChange"/>.
		/// </summary>
		/// <param name="previousChange"></param>
		/// <returns></returns>
		/// <remarks>
		/// This operation is not commutative.
		/// </remarks>
		public PropertyDiff Compound(PropertyDiff previousChange)
		{
			return new PropertyDiff(_propertyName, _hibernateType, previousChange._oldValue, _newValue);
		}
		private static PropertyDiff[] GetPropertyDiffs(string[] propertyNames, IType[] types, object[] currentState, object[] previousState)
		{
			var diffs = new PropertyDiff[propertyNames.Length];
			for (var i = 0; i < diffs.Length; i++)
			{
				var oldValue = previousState == null ? null : previousState[i];
				var newValue = currentState == null ? null : currentState[i];

				// need to handle collections specially
				if (types[i].IsCollectionType && ReferenceEquals(oldValue, newValue))
				{
					// the collection instance itself has not changed, but perhaps the content has?
					// if the oldValue is a persistent collection, then we can get a snapshot from
					// when the collection was initially loaded, and see if it is dirty
					if (oldValue is IPersistentCollection && NHibernateUtil.IsInitialized(oldValue))
					{
						oldValue = GetCollectionSnapshot(oldValue as IPersistentCollection);
					}
				}

				diffs[i] = new PropertyDiff(propertyNames[i], types[i], oldValue, newValue);
			}
			return diffs;
		}
		private void RecordChange(object domainObject, EntityChangeType changeType, PropertyDiff[] propertyDiffs)
		{
			if(IsChangeSetPublishable((DomainObject)domainObject))
			{
				// update all change trackers
				var entity = (Entity)domainObject;
				foreach (var changeTracker in _changeTrackers)
				{
					changeTracker.RecordChange(entity, changeType, propertyDiffs);
				}
			}
		}