Ejemplo n.º 1
0
            /// <summary>
            /// Publish model prepared event
            /// </summary>
            /// <param name="model">Model</param>
            /// <returns>A task that represents the asynchronous operation</returns>
            protected virtual async Task PublishModelPreparedEventAsync(object model)
            {
                //we publish the ModelPrepared event for all models as the BaseNopModel,
                //so you need to implement IConsumer<ModelPrepared<BaseNopModel>> interface to handle this event
                if (model is BaseNopModel nopModel)
                {
                    await _eventPublisher.ModelPreparedAsync(nopModel);
                }

                //we publish the ModelPrepared event for collection as the IEnumerable<BaseNopModel>,
                //so you need to implement IConsumer<ModelPrepared<IEnumerable<BaseNopModel>>> interface to handle this event
                if (model is IEnumerable <BaseNopModel> nopModelCollection)
                {
                    await _eventPublisher.ModelPreparedAsync(nopModelCollection);
                }
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Called asynchronously before the action, after model binding is complete.
            /// </summary>
            /// <param name="context">A context for action filters</param>
            /// <returns>A task that on completion indicates the necessary filter actions have been executed</returns>
            private async Task PublishModelPreparedEventAsync(ActionExecutingContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                if (context.HttpContext.Request == null)
                {
                    return;
                }

                //check whether this filter has been overridden for the Action
                var actionFilter = context.ActionDescriptor.FilterDescriptors
                                   .Where(filterDescriptor => filterDescriptor.Scope == FilterScope.Action)
                                   .Select(filterDescriptor => filterDescriptor.Filter)
                                   .OfType <PublishModelEventsAttribute>()
                                   .FirstOrDefault();

                //whether to ignore this filter
                if (actionFilter?.IgnoreFilter ?? _ignoreFilter)
                {
                    return;
                }

                //model prepared event
                if (context.Controller is Controller controller)
                {
                    if (controller.ViewData.Model is BaseNopModel model)
                    {
                        //we publish the ModelPrepared event for all models as the BaseNopModel,
                        //so you need to implement IConsumer<ModelPrepared<BaseNopModel>> interface to handle this event
                        await _eventPublisher.ModelPreparedAsync(model);
                    }

                    if (controller.ViewData.Model is IEnumerable <BaseNopModel> modelCollection)
                    {
                        //we publish the ModelPrepared event for collection as the IEnumerable<BaseNopModel>,
                        //so you need to implement IConsumer<ModelPrepared<IEnumerable<BaseNopModel>>> interface to handle this event
                        await _eventPublisher.ModelPreparedAsync(modelCollection);
                    }
                }
            }