protected override sealed async Task PersistModelAsync(PersistEvent @event, TModel model, Graph <TModel> graph, bool create)
        {
            var id         = ModelAnalyzer.GetIdentity(model);
            var streamName = EventStoreCommon.GetStreamName <TModel>(id);

            var eventStoreModel = new EventStoreEventModelData <TModel>()
            {
                Source     = @event.Source,
                SourceType = @event.Source?.GetType().Name,
                Model      = model,
                Graph      = graph
            };

            var data = EventStoreCommon.Serialize(eventStoreModel);

            var eventNumber = await Engine.AppendAsync(@event.ID, @event.Name, streamName, null, create?EventStoreState.NotExisting : EventStoreState.Existing, data);

            if (eventNumber > 0 && eventNumber % SaveStateEvery == 0)
            {
                var thisEventData = (await Engine.ReadBackwardsAsync(streamName, eventNumber, 1, null, null, null))[0];

                var where = ModelAnalyzer.GetIdentityExpression <TModel>(id);

                var eventStates = await Repo.QueryAsync(new EventQueryMany <TModel>(thisEventData.Date, thisEventData.Date, where));

                var eventState = eventStates.Where(x => x.Number == eventNumber).Single();

                await SaveModelStateAsync(id, eventState.Model, eventState.Number);
            }
        }
Esempio n. 2
0
        protected override sealed async Task PersistModelAsync(PersistEvent @event, TModel model, Graph <TModel> graph, bool create)
        {
            if (create)
            {
                int autoGeneratedCount = ModelDetail.IdentityAutoGeneratedProperties.Count;

                if (autoGeneratedCount == 1)
                {
                    var rows = await Engine.ExecuteInsertGetIdentitiesAsync(model, graph, ModelDetail);

                    if (rows.Count != 1)
                    {
                        throw new Exception($"Insert failed: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                    }
                    var identity          = rows.First();
                    var modelPropertyInfo = ModelDetail.IdentityAutoGeneratedProperties[0];
                    identity = TypeAnalyzer.Convert(identity, modelPropertyInfo.Type);
                    modelPropertyInfo.Setter(model, identity);
                }
                else if (autoGeneratedCount > 1)
                {
                    var rows = await Engine.ExecuteInsertGetIdentitiesAsync(model, graph, ModelDetail);

                    if (rows.Count != 1)
                    {
                        throw new Exception($"Insert failed: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                    }
                    var identities = (IList <object>)rows.First();
                    var i          = 0;
                    foreach (var modelPropertyInfo in ModelDetail.IdentityAutoGeneratedProperties)
                    {
                        object identity = modelPropertyInfo.Getter(identities[i]);
                        identity = TypeAnalyzer.Convert(identity, modelPropertyInfo.Type);
                        modelPropertyInfo.Setter(model, identity);
                        i++;
                    }
                }
                else
                {
                    var rows = await Engine.ExecuteInsertAsync(model, graph, ModelDetail);

                    if (rows == 0)
                    {
                        throw new Exception($"No rows affected: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                    }
                }
            }
            else
            {
                var rowsAffected = await Engine.ExecuteUpdateAsync(model, graph, ModelDetail);

                if (rowsAffected == 0)
                {
                    throw new Exception($"No rows affected: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
                }
            }
        }
        protected override sealed async Task DeleteModelAsync(PersistEvent @event, object[] ids)
        {
            foreach (var id in ids)
            {
                var streamName = EventStoreCommon.GetStreamName <TModel>(id);

                var eventNumber = await Engine.TerminateAsync(@event.ID, "Delete", streamName, null, EventStoreState.Existing);

                await SaveModelStateAsync(id, null, eventNumber);
            }
        }
Esempio n. 4
0
 protected override sealed void DeleteModel(PersistEvent @event, object[] ids)
 {
     for (var i = 0; i <= ids.Length; i += deleteBatchSize)
     {
         var deleteIds    = ids.Skip(i).Take(deleteBatchSize).ToArray();
         var rowsAffected = Engine.ExecuteDelete(deleteIds, ModelDetail);
         if (rowsAffected == 0)
         {
             throw new Exception($"No rows affected: {(String.IsNullOrWhiteSpace(@event.Name) ? defaultEventName : @event.Name)}");
         }
     }
 }
Esempio n. 5
0
 public Persist(PersistOperation operation, PersistEvent @event)
 {
     this.Operation = operation;
     this.Event     = @event;
     if (this.Event == null)
     {
         this.Event = new PersistEvent()
         {
             ID     = Guid.NewGuid(),
             Name   = GetEventName(operation),
             Source = null
         };
     }
     else if (String.IsNullOrWhiteSpace(this.Event.Name))
     {
         this.Event.Name = GetEventName(operation);
     }
 }
Esempio n. 6
0
 public Delete(PersistEvent @event, ICollection <TModel> models, Graph <TModel> graph)
     : base(PersistOperation.Delete, @event)
 {
     this.Models = models.ToArray();
     this.Graph  = graph;
 }
Esempio n. 7
0
 public Delete(PersistEvent @event, ICollection <TModel> models) : this(@event, models, null)
 {
 }
Esempio n. 8
0
 public Delete(PersistEvent @event, TModel model, Graph <TModel> graph)
     : base(PersistOperation.Delete, @event)
 {
     this.Models = new TModel[] { model };
     this.Graph  = graph;
 }
Esempio n. 9
0
 public Delete(PersistEvent @event, TModel model) : this(@event, model, null)
 {
 }
Esempio n. 10
0
 public DeleteByID(PersistEvent @event, ICollection ids, Graph<TModel> graph)
     : base(PersistOperation.Delete, @event)
 {
     this.IDs = ids;
     this.Graph = graph;
 }
Esempio n. 11
0
 public DeleteByID(PersistEvent @event, ICollection ids) : this(@event, ids, null) { }
Esempio n. 12
0
 public DeleteByID(PersistEvent @event, object id, Graph<TModel> graph)
     : base(PersistOperation.Delete, @event)
 {
     this.IDs = new object[] { id };
     this.Graph = graph;
 }
Esempio n. 13
0
 public DeleteByID(PersistEvent @event, object id) : this(@event, id, null) { }