Exemplo n.º 1
0
    private async Task SendNotification(PrincipalCommandBase <TResponse> request, TResponse response, CancellationToken cancellationToken)
    {
        var operation = request switch
        {
            EntityCreateCommand <TEntityModel, TResponse> _ => EntityChangeOperation.Created,
            EntityDeleteCommand <TKey, TResponse> _ => EntityChangeOperation.Deleted,
            _ => EntityChangeOperation.Updated
        };

        var notification = new EntityChangeNotification <TResponse>(response, operation);
        await _mediator.Publish(notification, cancellationToken);
    }
}
        public System.Threading.Tasks.Task Handle(EntityChangeNotification <TaskReadModel> notification, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Task Changed: {id} {operation}", notification.Model?.Id, notification.Operation);

            return(System.Threading.Tasks.Task.CompletedTask);
        }
Exemplo n.º 3
0
        private void SavingChanges(object sender, EventArgs e)
        {
            if (_transactionPending == false)
            {
                var trans = Transaction.Current;
                trans.TransactionCompleted += OnTransactionCompleted;
                _transactionPending         = true;
            }
            var context = (_context as IObjectContextAdapter).ObjectContext;
            var deletes =
                context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted)
                .Where(ose => ose.IsRelationship == false)
                .Select(
                    ose =>
                    new EntityChangeNotification
            {
                ChangeType = EntityChangeTypeEnum.Deleted,
                EntityType = ObjectContext.GetObjectType(ose.Entity.GetType()),
                Entity     = ose.Entity,
            });
            var mods =
                context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
                .Where(ose => ose.IsRelationship == false)
                .Select(
                    ose =>
                    new EntityChangeNotification
            {
                ChangeType  = EntityChangeTypeEnum.Modified,
                EntityType  = ObjectContext.GetObjectType(ose.Entity.GetType()),
                Entity      = ose.Entity,
                DataChanged =
                    new List <EntityDataChanged>(
                        ose.GetModifiedProperties()
                        .Where(p => ose.IsPropertyChanged(p))
                        .Select(
                            p =>
                            new EntityDataChanged(
                                p,
                                ose.OriginalValues.GetValue(ose.OriginalValues.GetOrdinal(p)),
                                ose.CurrentValues.GetValue(ose.CurrentValues.GetOrdinal(p)))))
            });
            var adds =
                context.ObjectStateManager.GetObjectStateEntries(EntityState.Added)
                .Where(ose => ose.IsRelationship == false)
                .Select(
                    ose =>
                    new EntityChangeNotification
            {
                ChangeType = EntityChangeTypeEnum.Added,
                Entity     = ose.Entity,
                EntityType = ObjectContext.GetObjectType(ose.Entity.GetType()),
            });
            var relations =
                context.ObjectStateManager.GetObjectStateEntries(
                    EntityState.Deleted | EntityState.Modified | EntityState.Added).Where(ose => ose.IsRelationship);

            _changesInProgress = _changesInProgress.Concat(adds).Concat(mods).Concat(deletes).ToList();

            foreach (var relationChange in relations)
            {
                if ((relationChange.UsableValues()[0] as EntityKey).IsTemporary == false)
                {
                    var key    = relationChange.UsableValues()[0] as EntityKey;
                    var entity = context.GetObjectByKey(key) as IEntityIdentifier;
                    var ecn    = new EntityChangeNotification
                    {
                        ChangeType = EntityChangeTypeEnum.Modified,
                        EntityType = ObjectContext.GetObjectType(entity.GetType()),
                        Entity     = entity,
                    };
                    _changesInProgress.Add(ecn);
                }
                if ((relationChange.UsableValues()[1] as EntityKey).IsTemporary == false)
                {
                    var key    = relationChange.UsableValues()[1] as EntityKey;
                    var entity = context.GetObjectByKey(key) as IEntityIdentifier;
                    var ecn    = new EntityChangeNotification
                    {
                        ChangeType = EntityChangeTypeEnum.Modified,
                        EntityType = ObjectContext.GetObjectType(entity.GetType()),
                        Entity     = entity,
                    };
                    _changesInProgress.Add(ecn);
                }
            }
            _changesInProgress =
                _changesInProgress.OrderBy(c => c.ChangeType).ThenBy(c => c.EntityType.Name).Distinct().ToList();
        }