public async Task <bool> PublishAsync <TEntity>(IServiceProvider serviceProvider, PersistentEventType eventType)
        {
            var _event  = new EntityEventTypeArgs(eventType);
            var subject = new PersistentSubject(typeof(TEntity), eventType, _event);

            await subMgr.PublishAsync(subject);

            return(!_event.Cancel);
        }
        public bool Publish <TEntity>(IServiceProvider serviceProvider, PersistentEventType eventType)
        {
            var _event  = new EntityEventTypeArgs(eventType);
            var subject = new PersistentSubject(typeof(TEntity), eventType, _event);

            subMgr.Publish(subject);

            AcceptForServiceProvider(serviceProvider, subject);

            return(!_event.Cancel);
        }
        public async Task <bool> PublishAsync(IServiceProvider serviceProvider, IEntity entity, PersistentEventType eventType)
        {
            Guard.ArgumentNull(entity, nameof(entity));

            var _event  = new EntityEventArgs(entity, eventType);
            var subject = new PersistentSubject(entity.EntityType, eventType, _event);

            await subMgr.PublishAsync(subject);

            await AcceptForServiceProviderAsync(serviceProvider, subject);

            return(!_event.Cancel);
        }
        public bool Publish(IServiceProvider serviceProvider, IEntity entity, PersistentEventType eventType)
        {
            Guard.ArgumentNull(entity, nameof(entity));

            var _event  = new EntityEventArgs(entity, eventType);
            var subject = new PersistentSubject(entity.EntityType, eventType, _event);

            subMgr.Publish(subject);

            AcceptForServiceProvider(serviceProvider, subject);

            return(!_event.Cancel);
        }
        public async Task <bool> PublishAsync <TEntity>(IServiceProvider serviceProvider, IEnumerable <IEntity> entities, PersistentOperator operType, PersistentEventType eventType)
        {
            if (!entities.Any())
            {
                return(true);
            }

            var _event  = new EntitiesArgs(entities, operType, eventType);
            var subject = new PersistentSubject(typeof(TEntity), eventType, _event);

            await subMgr.PublishAsync(subject);

            await AcceptForServiceProviderAsync(serviceProvider, subject);

            return(!_event.Cancel);
        }
        private void AcceptForServiceProvider(IServiceProvider serviceProvider, PersistentSubject subject)
        {
#if NETSTANDARD
            if (serviceProvider != null)
            {
                foreach (var sub in serviceProvider.GetService <IEnumerable <PersistentSubscriber> >())
                {
                    sub.Accept(subject);
                }

                foreach (var sub in serviceProvider.GetService <IEnumerable <AsyncPersistentSubscriber> >())
                {
                    sub.AcceptAsync(subject).AsSync();
                }
            }
#endif
        }
        public async Task <bool> PublishAsync(IServiceProvider serviceProvider, IEnumerable <IEntity> entities, PersistentOperator operType, PersistentEventType eventType)
        {
            IEntity first;

            if ((first = entities.FirstOrDefault()) == null)
            {
                return(true);
            }

            var _event     = new EntitiesArgs(entities, operType, eventType);
            var entityType = first.EntityType;
            var subject    = new PersistentSubject(entityType, eventType, _event);

            await subMgr.PublishAsync(subject);

            await AcceptForServiceProviderAsync(serviceProvider, subject);

            return(!_event.Cancel);
        }
        /// <summary>
        /// 接收主题信息并进行处理。
        /// </summary>
        /// <param name="subject"></param>
        public void Accept(PersistentSubject subject)
        {
            switch (subject.EventType)
            {
            case PersistentEventType.BeforeCreate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    arg.Cancel = !OnBeforeCreate(arg.Entity);
                }
            }
            break;

            case PersistentEventType.AfterCreate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    OnAfterCreate(arg.Entity);
                }
            }

                OnCreate(subject.EntityType);
                break;

            case PersistentEventType.BeforeUpdate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    arg.Cancel = !OnBeforeUpdate(arg.Entity);
                }
            }
            break;

            case PersistentEventType.AfterUpdate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    OnAfterUpdate(arg.Entity);
                }
            }

                OnUpdate(subject.EntityType);
                break;

            case PersistentEventType.BeforeRemove:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    arg.Cancel = !OnBeforeRemove(arg.Entity);
                }
            }
            break;

            case PersistentEventType.AfterRemove:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    OnAfterRemove(arg.Entity);
                }
            }

                OnRemove(subject.EntityType);
                break;

            case PersistentEventType.BeforeBatch:
            {
                var arg = subject.Argument as EntitiesArgs;
                arg.Cancel = !OnBeforeBatch(arg.Entities, arg.OperType);
            }
            break;

            case PersistentEventType.AfterBatch:
            {
                var arg = subject.Argument as EntitiesArgs;
                OnAfterBatch(arg.Entities, arg.OperType);

                switch (arg.OperType)
                {
                case PersistentOperator.Create:
                    OnCreate(subject.EntityType);
                    break;

                case PersistentOperator.Update:
                    OnUpdate(subject.EntityType);
                    break;

                case PersistentOperator.Remove:
                    OnRemove(subject.EntityType);
                    break;
                }
            }

            break;
            }
        }
        /// <summary>
        /// 接收主题信息并进行处理。
        /// </summary>
        /// <param name="subject"></param>
        public async Task AcceptAsync(PersistentSubject subject)
        {
            switch (subject.EventType)
            {
            case PersistentEventType.BeforeCreate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    arg.Cancel = !await OnBeforeCreateAsync(arg.Entity);
                }
            }
            break;

            case PersistentEventType.AfterCreate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    await OnAfterCreateAsync(arg.Entity);
                }
            }

                await OnCreateAsync(subject.EntityType);

                break;

            case PersistentEventType.BeforeUpdate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    arg.Cancel = !await OnBeforeUpdateAsync(arg.Entity);
                }
            }
            break;

            case PersistentEventType.AfterUpdate:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    await OnAfterUpdateAsync(arg.Entity);
                }
            }

                await OnUpdateAsync(subject.EntityType);

                break;

            case PersistentEventType.BeforeRemove:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    arg.Cancel = !await OnBeforeRemoveAsync(arg.Entity);
                }
            }
            break;

            case PersistentEventType.AfterRemove:
            {
                if (subject.Argument is EntityEventArgs arg)
                {
                    await OnAfterRemoveAsync(arg.Entity);
                }
            }

                await OnRemoveAsync(subject.EntityType);

                break;

            case PersistentEventType.BeforeBatch:
            {
                var arg = subject.Argument as EntitiesArgs;
                arg.Cancel = !await OnBeforeBatchAsync(arg.Entities, arg.OperType);
            }
            break;

            case PersistentEventType.AfterBatch:
            {
                var arg = subject.Argument as EntitiesArgs;
                await OnAfterBatchAsync(arg.Entities, arg.OperType);

                switch (arg.OperType)
                {
                case PersistentOperator.Create:
                    await OnCreateAsync(subject.EntityType);

                    break;

                case PersistentOperator.Update:
                    await OnUpdateAsync(subject.EntityType);

                    break;

                case PersistentOperator.Remove:
                    await OnRemoveAsync(subject.EntityType);

                    break;
                }
            }

            break;
            }
        }