/// <summary> /// Function that initializes the SyncUploadResponseContext and then invokes user interceptor code /// </summary> /// <param name="providerService">SqlSyncProviderService instance</param> private void PrepareAndProcessResponseInterceptor(IAsymmetricProviderService providerService) { if (_configuration.HasResponseInterceptors(this._scopeName, SyncOperations.Upload) || _configuration.HasTypedResponseInterceptors(this._scopeName, SyncOperations.Upload)) { // Init the response SyncOperationContext this.InitResponseOperationContext(); // Set context properties. SyncUploadResponseOperationContext responseContext = (SyncUploadResponseOperationContext)base._operationContext; // Create a list of conflicts List<Conflict> conflicts = new List<Conflict>(_applyChangesResponse.Conflicts.Cast<Conflict>()); // Merge the Conflicts and Errors properties conflicts.AddRange(_applyChangesResponse.Errors.Cast<Conflict>()); if (_configuration.HasResponseInterceptors(this._scopeName, SyncOperations.Upload)) { // Set context's conflicts collection responseContext.Conflicts = conflicts.AsReadOnly(); // Set items that is being responded with a permanent Id. These will be entires from the // _incomingNewInsertEntities that were not conflicts or errors responseContext.OutgoingChanges = _incomingNewInsertEntities.AsReadOnly(); // Fire the response Interceptors if any base.ProcessResponseInterceptors(); } else { // This means we have typed interceptors. We need to fire the interceptor if the type exists // either in the OutgoingChanges collection or in Conflicts collection. When item exists in both // collections then it must be fired only once and must contain both entires. So visit the outoging // group first and keep track of Type already visited. Then go over the Conflicts collection and then // visit types that arent visites yet. List<Type> visitedTypes = new List<Type>(); foreach (IGrouping<Type, IOfflineEntity> grouping in _incomingNewInsertEntities.GroupBy(e => e.GetType())) { if (_configuration.HasTypedResponseInterceptor(this._scopeName, SyncOperations.Upload, grouping.Key)) { visitedTypes.Add(grouping.Key); // Set context's conflicts collection responseContext.Conflicts = conflicts.Where(e => e.LiveEntity.GetType() == grouping.Key).ToList().AsReadOnly(); // Set items that is being responded with a permanent Id. These will be entires from the // _incomingNewInsertEntities that were not conflicts or errors responseContext.OutgoingChanges = grouping.ToList().AsReadOnly(); // Fire the typed response Interceptors if any base.ProcessTypedResponseInterceptors(grouping.Key); } } // Now visit the Conflict collection foreach (IGrouping<Type, Conflict> grouping in conflicts.GroupBy(e => e.LiveEntity.GetType())) { if (_configuration.HasTypedResponseInterceptor(this._scopeName, SyncOperations.Upload, grouping.Key) && !visitedTypes.Contains(grouping.Key)) { // Set context's conflicts collection responseContext.OutgoingChanges = _incomingNewInsertEntities.Where(e => e.GetType() == grouping.Key).ToList().AsReadOnly(); // Set items that is being responded with a permanent Id. These will be entires from the // _incomingNewInsertEntities that were not conflicts or errors responseContext.Conflicts = grouping.ToList().AsReadOnly(); // Fire the typed response Interceptors if any base.ProcessTypedResponseInterceptors(grouping.Key); } } } } }
/// <summary> /// Function that initializes the SyncUploadResponseContext and then invokes user interceptor code /// </summary> /// <param name="providerService">SqlSyncProviderService instance</param> private void PrepareAndProcessResponseInterceptor(IAsymmetricProviderService providerService) { if (_configuration.HasResponseInterceptors(this._scopeName, SyncOperations.Upload) || _configuration.HasTypedResponseInterceptors(this._scopeName, SyncOperations.Upload)) { // Init the response SyncOperationContext this.InitResponseOperationContext(); // Set context properties. SyncUploadResponseOperationContext responseContext = (SyncUploadResponseOperationContext)base._operationContext; // Create a list of conflicts List <Conflict> conflicts = new List <Conflict>(_applyChangesResponse.Conflicts.Cast <Conflict>()); // Merge the Conflicts and Errors properties conflicts.AddRange(_applyChangesResponse.Errors.Cast <Conflict>()); if (_configuration.HasResponseInterceptors(this._scopeName, SyncOperations.Upload)) { // Set context's conflicts collection responseContext.Conflicts = conflicts.AsReadOnly(); // Set items that is being responded with a permanent Id. These will be entires from the // _incomingNewInsertEntities that were not conflicts or errors responseContext.OutgoingChanges = _incomingNewInsertEntities.AsReadOnly(); // Fire the response Interceptors if any base.ProcessResponseInterceptors(); } else { // This means we have typed interceptors. We need to fire the interceptor if the type exists // either in the OutgoingChanges collection or in Conflicts collection. When item exists in both // collections then it must be fired only once and must contain both entires. So visit the outoging // group first and keep track of Type already visited. Then go over the Conflicts collection and then // visit types that arent visites yet. List <Type> visitedTypes = new List <Type>(); foreach (IGrouping <Type, IOfflineEntity> grouping in _incomingNewInsertEntities.GroupBy(e => e.GetType())) { if (_configuration.HasTypedResponseInterceptor(this._scopeName, SyncOperations.Upload, grouping.Key)) { visitedTypes.Add(grouping.Key); // Set context's conflicts collection responseContext.Conflicts = conflicts.Where(e => e.LiveEntity.GetType() == grouping.Key).ToList().AsReadOnly(); // Set items that is being responded with a permanent Id. These will be entires from the // _incomingNewInsertEntities that were not conflicts or errors responseContext.OutgoingChanges = grouping.ToList().AsReadOnly(); // Fire the typed response Interceptors if any base.ProcessTypedResponseInterceptors(grouping.Key); } } // Now visit the Conflict collection foreach (IGrouping <Type, Conflict> grouping in conflicts.GroupBy(e => e.LiveEntity.GetType())) { if (_configuration.HasTypedResponseInterceptor(this._scopeName, SyncOperations.Upload, grouping.Key) && !visitedTypes.Contains(grouping.Key)) { // Set context's conflicts collection responseContext.OutgoingChanges = _incomingNewInsertEntities.Where(e => e.GetType() == grouping.Key).ToList().AsReadOnly(); // Set items that is being responded with a permanent Id. These will be entires from the // _incomingNewInsertEntities that were not conflicts or errors responseContext.Conflicts = grouping.ToList().AsReadOnly(); // Fire the typed response Interceptors if any base.ProcessTypedResponseInterceptors(grouping.Key); } } } } }