public MergeResult <TDataObject> GetChanges(IReadOnlyCollection <ICommand> commands)
        {
            var specification = _memoryBasedDataObjectAccessor.GetFindSpecification(commands);
            var source        = _memoryBasedDataObjectAccessor.GetDataObjects(commands);
            var target        = _query.For <TDataObject>().WhereMatched(specification);

            var result = MergeTool.Merge(source, target, _identityComparer);

            return(result);
        }
        public IReadOnlyCollection <IEvent> ExecuteCommands(IReadOnlyCollection <ICommand> commands)
        {
            var commandsToExecute = commands.OfType <IReplaceDataObjectCommand>()
                                    .Where(x => x.DataObjectType == typeof(TDataObject))
                                    .Distinct()
                                    .ToArray();

            if (!commandsToExecute.Any())
            {
                return(Array.Empty <IEvent>());
            }

            var events = new List <IEvent>();

            foreach (var command in commandsToExecute)
            {
                using (var transaction = new TransactionScope(
                           TransactionScopeOption.Required,
                           new TransactionOptions {
                    IsolationLevel = IsolationLevel.ReadCommitted, Timeout = TimeSpan.Zero
                }))
                {
                    var findSpecification   = _memoryBasedDataObjectAccessor.GetFindSpecification(command);
                    var existingDataObjects = _query.For(findSpecification)
                                              .ToList();
                    if (existingDataObjects.Any())
                    {
                        events.AddRange(_dataChangesHandler.HandleRelates(existingDataObjects));
                        _bulkRepository.Delete(existingDataObjects);
                    }

                    var targetDataObjects = _memoryBasedDataObjectAccessor.GetDataObjects(command);
                    if (targetDataObjects.Any())
                    {
                        _bulkRepository.Create(targetDataObjects);
                        events.AddRange(_dataChangesHandler.HandleCreates(targetDataObjects));
                        events.AddRange(_dataChangesHandler.HandleRelates(targetDataObjects));
                    }

                    transaction.Complete();
                }
            }

            return(events);
        }
Example #3
0
        private void ExecuteBulkReplace(IEnumerable <object> sourceDtos)
        {
            var replaceDataObjectCommand = new ReplaceDataObjectCommand(DataObjectType, sourceDtos);

            var findSpecification = _dataObjectAccessor.GetFindSpecification(replaceDataObjectCommand);
            var predicate         = (Expression <Func <TDataObject, bool> >)PredicateInfo.GetValue(findSpecification);

            _table.Delete <TDataObject>(predicate);

            var dataObjects = _dataObjectAccessor.GetDataObjects(replaceDataObjectCommand);

            try
            {
                _table.BulkCopy <TDataObject>(_bulkCopyOptions, dataObjects);
            }
            catch (Exception ex)
            {
                throw new DataException($"Error occurred while bulk replacing data for data object of type {DataObjectType.Name} using {_dataObjectAccessor.GetType().Name}{Environment.NewLine}", ex);
            }
        }