示例#1
0
 public SyncEntityNameActor(IQuery query,
                            ITenantProvider tenantProvider,
                            IBulkRepository <EntityName> bulkRepository,
                            IEqualityComparerFactory equalityComparerFactory)
 {
     _query               = query;
     _tenantProvider      = tenantProvider;
     _bulkRepository      = bulkRepository;
     _dataChangesDetector = new TwoPhaseDataChangesDetector <EntityName>(equalityComparerFactory);
 }
        public IReadOnlyCollection <IEvent> ExecuteCommands(IReadOnlyCollection <ICommand> commands)
        {
            // db accessors
            var syncDataObjectCommands = commands.OfType <ISyncDataObjectCommand>().ToList();

            if (syncDataObjectCommands.Count != 0)
            {
                foreach (var accessor in CreateAccessors(_query))
                {
                    var specification = accessor.GetFindSpecification(syncDataObjectCommands);

                    var dataChangesDetector = new TwoPhaseDataChangesDetector <EntityName>(
                        spec => accessor.GetSource().WhereMatched(spec),
                        spec => _query.For <EntityName>().WhereMatched(spec),
                        _identityComparer,
                        _completeComparer);

                    var changes = dataChangesDetector.DetectChanges(specification);
                    _bulkRepository.Delete(changes.Complement);
                    _bulkRepository.Create(changes.Difference);
                    _bulkRepository.Update(changes.Intersection);
                }
            }

            // memory accessors
            var replaceDataObjectCommands = commands.OfType <IReplaceDataObjectCommand>().ToList();

            if (replaceDataObjectCommands.Count != 0)
            {
                foreach (var accessor in CreateMemoryAccessors())
                {
                    var specification = accessor.GetFindSpecification(replaceDataObjectCommands);

                    var dataChangesDetector = new TwoPhaseDataChangesDetector <EntityName>(
                        _ => accessor.GetDataObjects(replaceDataObjectCommands),
                        spec => _query.For <EntityName>().WhereMatched(spec),
                        _identityComparer,
                        _completeComparer);

                    var changes = dataChangesDetector.DetectChanges(specification);
                    _bulkRepository.Delete(changes.Complement);
                    _bulkRepository.Create(changes.Difference);
                    _bulkRepository.Update(changes.Intersection);
                }
            }

            return(Array.Empty <IEvent>());
        }