private async Task <List <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> > > Synchronize(
            ITotalProgressLogger totalProgress,
            IEnumerable <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> > knownEntityRelations,
            Dictionary <TAtypeEntityId, TAtypeEntityVersion> newAVersions,
            Dictionary <TBtypeEntityId, TBtypeEntityVersion> newBVersions,
            EntityContainer entityContainer,
            ISynchronizationLogger logger,
            TContext synchronizationContext)
        {
            var entitySyncStates = new EntitySyncStateContainer <TAtypeEntityId, TAtypeEntityVersion, TAtypeEntity, TBtypeEntityId, TBtypeEntityVersion, TBtypeEntity>();

            var aDeltaLogInfo = new VersionDeltaLoginInformation();
            var bDeltaLogInfo = new VersionDeltaLoginInformation();

            foreach (var knownEntityRelationData in knownEntityRelations)
            {
                TAtypeEntityVersion newAVersion;
                TBtypeEntityVersion newBVersion;

                var newAVersionAvailable = newAVersions.TryGetValue(knownEntityRelationData.AtypeId, out newAVersion);
                var newBVersionAvailable = newBVersions.TryGetValue(knownEntityRelationData.BtypeId, out newBVersion);

                if (newAVersionAvailable)
                {
                    newAVersions.Remove(knownEntityRelationData.AtypeId);
                }

                if (newBVersionAvailable)
                {
                    newBVersions.Remove(knownEntityRelationData.BtypeId);
                }

                var entitySyncState = CreateInitialSyncState(knownEntityRelationData, newAVersionAvailable, newAVersion, newBVersionAvailable, newBVersion, aDeltaLogInfo, bDeltaLogInfo);

                entitySyncStates.Add(entitySyncState);
            }

            HashSet <TAtypeEntityId> aEntitesToLoad = new HashSet <TAtypeEntityId>();
            HashSet <TBtypeEntityId> bEntitesToLoad = new HashSet <TBtypeEntityId>();

            entitySyncStates.Execute(s => s.AddRequiredEntitiesToLoad(aEntitesToLoad.Add, bEntitesToLoad.Add));

            if (newAVersions.Count > 0 && newBVersions.Count > 0)
            {
                foreach (var newA in newAVersions)
                {
                    aEntitesToLoad.Add(newA.Key);
                }

                foreach (var newB in newBVersions)
                {
                    bEntitesToLoad.Add(newB.Key);
                }

                await entityContainer.FillIfEmpty(aEntitesToLoad, bEntitesToLoad, synchronizationContext);

                var newAtypeEntities = GetSubSet(entityContainer.AEntities, newAVersions.Keys, _atypeIdComparer);
                var newBtypeEntities = GetSubSet(entityContainer.BEntities, newBVersions.Keys, _btypeIdComparer);

                var matchingEntites = _initialEntityMatcher.FindMatchingEntities(
                    _entityRelationDataFactory,
                    newAtypeEntities,
                    newBtypeEntities,
                    newAVersions,
                    newBVersions);

                foreach (var knownEntityRelationData in matchingEntites)
                {
                    newAVersions.Remove(knownEntityRelationData.AtypeId);
                    newBVersions.Remove(knownEntityRelationData.BtypeId);
                    var entitySyncState = _initialSyncStateCreationStrategy.CreateFor_Unchanged_Unchanged(knownEntityRelationData);
                    entitySyncStates.Add(entitySyncState);
                }

                foreach (var newA in newAVersions)
                {
                    var syncState = _initialSyncStateCreationStrategy.CreateFor_Added_NotExisting(newA.Key, newA.Value);
                    entitySyncStates.Add(syncState);
                }

                foreach (var newB in newBVersions)
                {
                    var syncState = _initialSyncStateCreationStrategy.CreateFor_NotExisting_Added(newB.Key, newB.Value);
                    entitySyncStates.Add(syncState);
                }
            }
            else
            {
                foreach (var newA in newAVersions)
                {
                    var syncState = _initialSyncStateCreationStrategy.CreateFor_Added_NotExisting(newA.Key, newA.Value);
                    syncState.AddRequiredEntitiesToLoad(aEntitesToLoad.Add, bEntitesToLoad.Add);
                    entitySyncStates.Add(syncState);
                }

                foreach (var newB in newBVersions)
                {
                    var syncState = _initialSyncStateCreationStrategy.CreateFor_NotExisting_Added(newB.Key, newB.Value);
                    syncState.AddRequiredEntitiesToLoad(aEntitesToLoad.Add, bEntitesToLoad.Add);
                    entitySyncStates.Add(syncState);
                }

                await entityContainer.FillIfEmpty(aEntitesToLoad, bEntitesToLoad, synchronizationContext);
            }

            // all the leftovers in newAVersions and newBVersions must be the added ones
            aDeltaLogInfo.IncAdded(newAVersions.Count);
            bDeltaLogInfo.IncAdded(newBVersions.Count);
            s_logger.InfoFormat("Atype delta: {0}", aDeltaLogInfo);
            s_logger.InfoFormat("Btype delta: {0}", bDeltaLogInfo);
            logger.LogDeltas(aDeltaLogInfo, bDeltaLogInfo);


            entitySyncStates.DoTransition(s => s.FetchRequiredEntities(entityContainer.AEntities, entityContainer.BEntities));
            entitySyncStates.DoTransition(s => s.Resolve());

            // since resolve may change to an new state, required entities have to be fetched again.
            // an state is allowed only to resolve to another state, if the following states requires equal or less entities!
            entitySyncStates.DoTransition(s => s.FetchRequiredEntities(entityContainer.AEntities, entityContainer.BEntities));

            var aJobs = new JobList <TAtypeEntity, TAtypeEntityId, TAtypeEntityVersion>();
            var bJobs = new JobList <TBtypeEntity, TBtypeEntityId, TBtypeEntityVersion>();

            entitySyncStates.Execute(s => s.AddSyncronizationJob(aJobs, bJobs, logger.CreateEntitySynchronizationLogger()));

            using (var progress = totalProgress.StartProcessing(aJobs.TotalJobCount + bJobs.TotalJobCount))
            {
                await _atypeWriteRepository.PerformOperations(aJobs.CreateJobs, aJobs.UpdateJobs, aJobs.DeleteJobs, progress, synchronizationContext);

                await _btypeWriteRepository.PerformOperations(bJobs.CreateJobs, bJobs.UpdateJobs, bJobs.DeleteJobs, progress, synchronizationContext);
            }

            entitySyncStates.DoTransition(s => s.NotifyJobExecuted());

            var newEntityRelations = new List <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> >();

            entitySyncStates.Execute(s => s.AddNewRelationNoThrow(newEntityRelations.Add));

            entitySyncStates.Dispose();


            return(newEntityRelations);
        }
        public async Task <bool> Synchronize()
        {
            s_logger.InfoFormat("Entered. Syncstrategy '{0}' with Atype='{1}' and Btype='{2}'", _initialSyncStateCreationStrategy.GetType().Name, typeof(TAtypeEntity).Name, typeof(TBtypeEntity).Name);


            try
            {
                using (var totalProgress = _totalProgressFactory.Create())
                {
                    var cachedData = _entityRelationDataAccess.LoadEntityRelationData();

                    var aVersionsTask = _atypeRepository.GetVersions();
                    var bVersionsTask = _btypeRepository.GetVersions();

                    var aVersions = await aVersionsTask;
                    var bVersions = await bVersionsTask;

                    var atypeRepositoryVersions = CreateDictionary(
                        aVersions,
                        _atypeIdComparer);

                    var btypeRepositoryVersions = CreateDictionary(
                        bVersions,
                        _btypeIdComparer);

                    IReadOnlyDictionary <TAtypeEntityId, TAtypeEntity> aEntities = null;
                    IReadOnlyDictionary <TBtypeEntityId, TBtypeEntity> bEntities = null;

                    try
                    {
                        if (cachedData == null)
                        {
                            s_logger.Info("Did not find entity caches. Performing initial population");

                            totalProgress.NotifyLoadCount(atypeRepositoryVersions.Count, btypeRepositoryVersions.Count);

                            using (totalProgress.StartARepositoryLoad())
                            {
                                aEntities = CreateDictionary(
                                    await _atypeRepository.Get(atypeRepositoryVersions.Keys),
                                    _atypeIdComparer);
                            }

                            using (totalProgress.StartBRepositoryLoad())
                            {
                                bEntities = CreateDictionary(
                                    await _btypeRepository.Get(btypeRepositoryVersions.Keys),
                                    _btypeIdComparer);
                            }

                            cachedData = _initialEntityMatcher.FindMatchingEntities(
                                _entityRelationDataFactory,
                                aEntities,
                                bEntities,
                                atypeRepositoryVersions,
                                btypeRepositoryVersions);
                        }

                        var entitySyncStates = new EntitySyncStateContainer <TAtypeEntityId, TAtypeEntityVersion, TAtypeEntity, TBtypeEntityId, TBtypeEntityVersion, TBtypeEntity>();

                        var aDeltaLogInfo = new VersionDeltaLoginInformation();
                        var bDeltaLogInfo = new VersionDeltaLoginInformation();

                        foreach (var cachedEntityData in cachedData)
                        {
                            TAtypeEntityVersion repositoryAVersion;
                            TBtypeEntityVersion repositoryBVersion;

                            var repositoryAVersionAvailable = atypeRepositoryVersions.TryGetValue(cachedEntityData.AtypeId, out repositoryAVersion);
                            var repositoryBVersionAvailable = btypeRepositoryVersions.TryGetValue(cachedEntityData.BtypeId, out repositoryBVersion);

                            if (repositoryAVersionAvailable)
                            {
                                atypeRepositoryVersions.Remove(cachedEntityData.AtypeId);
                            }

                            if (repositoryBVersionAvailable)
                            {
                                btypeRepositoryVersions.Remove(cachedEntityData.BtypeId);
                            }

                            var entitySyncState = CreateInitialSyncState(cachedEntityData, repositoryAVersionAvailable, repositoryAVersion, repositoryBVersionAvailable, repositoryBVersion, aDeltaLogInfo, bDeltaLogInfo);

                            entitySyncStates.Add(entitySyncState);
                        }

                        aDeltaLogInfo.IncAdded(atypeRepositoryVersions.Count);
                        bDeltaLogInfo.IncAdded(btypeRepositoryVersions.Count);

                        s_logger.InfoFormat("Atype delta: {0}", aDeltaLogInfo);
                        s_logger.InfoFormat("Btype delta: {0}", bDeltaLogInfo);

                        foreach (var newA in atypeRepositoryVersions)
                        {
                            entitySyncStates.Add(_initialSyncStateCreationStrategy.CreateFor_Added_NotExisting(newA.Key, newA.Value));
                        }

                        foreach (var newB in btypeRepositoryVersions)
                        {
                            entitySyncStates.Add(_initialSyncStateCreationStrategy.CreateFor_NotExisting_Added(newB.Key, newB.Value));
                        }

                        HashSet <TAtypeEntityId> aEntitesToLoad = new HashSet <TAtypeEntityId>();
                        HashSet <TBtypeEntityId> bEntitesToLoad = new HashSet <TBtypeEntityId>();

                        entitySyncStates.Execute(s => s.AddRequiredEntitiesToLoad(aEntitesToLoad.Add, bEntitesToLoad.Add));

                        if (aEntities == null && bEntities == null)
                        {
                            totalProgress.NotifyLoadCount(aEntitesToLoad.Count, bEntitesToLoad.Count);
                            using (totalProgress.StartARepositoryLoad())
                            {
                                aEntities = CreateDictionary(
                                    await _atypeRepository.Get(aEntitesToLoad),
                                    _atypeIdComparer);
                            }

                            using (totalProgress.StartBRepositoryLoad())
                            {
                                bEntities = CreateDictionary(
                                    await _btypeRepository.Get(bEntitesToLoad),
                                    _btypeIdComparer);
                            }
                        }

                        entitySyncStates.DoTransition(s => s.FetchRequiredEntities(aEntities, bEntities));
                        entitySyncStates.DoTransition(s => s.Resolve());

                        // since resolve may change to an new state, required entities have to be fetched again.
                        // an state is allowed only to resolve to another state, if the following states requires equal or less entities!
                        entitySyncStates.DoTransition(s => s.FetchRequiredEntities(aEntities, bEntities));

                        using (var progress = totalProgress.StartProcessing(entitySyncStates.Count))
                        {
                            await entitySyncStates.DoTransition(
                                async s =>
                            {
                                var nextState = await s.PerformSyncActionNoThrow();
                                progress.Increase();
                                return(nextState);
                            });
                        }

                        var newData = new List <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> >();

                        entitySyncStates.Execute(s => s.AddNewRelationNoThrow(newData.Add));

                        entitySyncStates.Dispose();

                        _entityRelationDataAccess.SaveEntityRelationData(newData);
                    }
                    finally
                    {
                        _atypeRepository.Cleanup(aEntities);
                        _btypeRepository.Cleanup(bEntities);
                    }
                }
            }
            catch (Exception x)
            {
                _exceptionLogger.LogException(x, s_logger);
                return(false);
            }

            s_logger.DebugFormat("Exiting.");
            return(true);
        }