private UpdateCollectionMessage CreateUpdateCollectionMessage(DataDestination dataDestination, Sequence sequence)
		{
			UpdateCollectionMessage updateCollectionMessage = new UpdateCollectionMessage();
			updateCollectionMessage.clientId = this.ClientId;
			updateCollectionMessage.updateMode = UpdateCollectionMessage.ServerUpdate;
			// The unique identifier for the collection that was updated. For a collection filled with the 
			// DataService.fill() method this contains an Array of the parameters specified.
			updateCollectionMessage.collectionId = sequence.Parameters;
			updateCollectionMessage.destination = dataDestination.Id;
			updateCollectionMessage.correlationId = this.CorrelationId;
			updateCollectionMessage.messageId = "srv:" + Guid.NewGuid().ToString("D") + ":" + _idCounter.ToString();
			System.Threading.Interlocked.Increment(ref _idCounter);

			return updateCollectionMessage;
		}
		internal void AddClientUpdateCollection(UpdateCollectionMessage updateCollectionMessage)
		{
			_clientUpdateCollectionMessages[updateCollectionMessage.collectionId] = updateCollectionMessage;
		}
		private UpdateCollectionMessage CreateUpdateCollectionMessage(DataMessage dataMessage, Sequence sequence, Identity identity, int position, int updateMode)
		{
			UpdateCollectionMessage updateCollectionMessage = new UpdateCollectionMessage();
			// The unique identifier for the collection that was updated. For a collection filled with the 
			// DataService.fill() method this contains an Array of the parameters specified.
			updateCollectionMessage.collectionId = sequence.Parameters;
			updateCollectionMessage.destination = dataMessage.destination;
			updateCollectionMessage.replace = false;
			updateCollectionMessage.updateMode = updateMode;
			updateCollectionMessage.messageId = "srv:" + Guid.NewGuid().ToString("D") + ":0";
			updateCollectionMessage.correlationId = dataMessage.correlationId;

			UpdateCollectionRange updateCollectionRange = new UpdateCollectionRange();
			// An Array of identity objects that represent which items were either deleted or inserted in the 
			// associated collection starting at the position indicated by the position property
			updateCollectionRange.identities = new object[1];
			//(updateCollectionRange.identities as IList).Add( identity );
			(updateCollectionRange.identities as object[])[0] = identity;
			updateCollectionRange.updateType = UpdateCollectionRange.InsertIntoCollection;
			updateCollectionRange.position = position;
					
			//ArrayList body = new ArrayList();
			//body.Add(updateCollectionRange);
			object[] body = new object[1]; body[0] = updateCollectionRange;
			updateCollectionMessage.body = body;
			return updateCollectionMessage;
		}
		private IMessage UpdateCollection(UpdateCollectionMessage updateCollectionMessage, IList messages)
		{
			IList updateCollectionRanges = updateCollectionMessage.body as IList;
			for(int i = 0; i < updateCollectionRanges.Count; i++)
			{
				UpdateCollectionRange updateCollectionRange = updateCollectionRanges[i] as UpdateCollectionRange;
				for(int j = 0; j < updateCollectionRange.identities.Length; j++)
				{
					string messageId = updateCollectionRange.identities[j] as string;
					Identity identity = null;
					if( messageId != null )
					{
						//Search for previous Create or CreateAndSequence
						//This was a "pending update collection" sent from the client and it must be replaced by the actual Identity
						IMessage refMessage = GetMessage(messages, messageId);
						DataMessage dataMessage = refMessage as DataMessage;
						if( dataMessage != null )
						{
							DataDestination dataDestination = this.Destination as DataDestination;
							identity = Identity.GetIdentity(dataMessage.body, dataDestination);
						}
						//replace with the actual identity
						updateCollectionRange.identities[j] = identity;
					}
					else
					{
                        IDictionary identityMap = updateCollectionRange.identities[j] as IDictionary;
						if( identityMap != null )
							identity = new Identity(identityMap);
					}

					IList fillParameters = updateCollectionMessage.collectionId as IList;

					IAssembler assembler = GetAssembler();
					if( assembler != null )
					{
						if( updateCollectionRange.updateType == UpdateCollectionRange.InsertIntoCollection )
							assembler.AddItemToFill(fillParameters, updateCollectionRange.position + j ,identity);
						if( updateCollectionRange.updateType == UpdateCollectionRange.DeleteFromCollection )
							assembler.RemoveItemFromFill(fillParameters, updateCollectionRange.position ,identity);
					}
				}
			}
			return updateCollectionMessage;
		}
		void ApplyUpdateCollectionMessage(Sequence sequence, UpdateCollectionMessage updateCollectionMessage)
		{
			IList updateCollectionRanges = updateCollectionMessage.body as IList;
			for(int k = 0; k < updateCollectionRanges.Count; k++)
			{
				UpdateCollectionRange updateCollectionRange = updateCollectionRanges[k] as UpdateCollectionRange;
				int insertCount = 0;
				for(int l = 0; l < updateCollectionRange.identities.Length; l++)
				{
					Identity identity = updateCollectionRange.identities[l] as Identity;
					if( identity == null )
					{
                        identity = new Identity(updateCollectionRange.identities[l] as IDictionary);
					}
					if( updateCollectionRange.updateType == UpdateCollectionRange.InsertIntoCollection )
					{
						this.AddIdentityToSequence(sequence, updateCollectionRange.position + insertCount, identity, null);
						insertCount++;
					}
					if( updateCollectionRange.updateType == UpdateCollectionRange.DeleteFromCollection )
						this.RemoveIdentityFromSequence(sequence, identity, updateCollectionRange.position, null);
				}
			}
		}