Ejemplo n.º 1
0
        private void PublishModelPrepared <TModel>(TModel model)
        {
            //Components are not part of the controller life cycle.
            //Hence, we could no longer use Action Filters to intercept the Models being returned
            //as we do in the /Nop.Web.Framework/Mvc/Filters/PublishModelEventsAttribute.cs for controllers

            //model prepared event
            if (model is BaseAgsModel)
            {
                IEventPublisher eventPublisher = ServiceProviderFactory.ServiceProvider.GetRequiredService <IEventPublisher>();

                //we publish the ModelPrepared event for all models as the BaseAgsModel,
                //so you need to implement IConsumer<ModelPrepared<BaseAgsModel>> interface to handle this event
                eventPublisher.ModelPrepared(model as BaseAgsModel);
            }

            if (model is IEnumerable <BaseAgsModel> modelCollection)
            {
                IEventPublisher eventPublisher = ServiceProviderFactory.ServiceProvider.GetRequiredService <IEventPublisher>();

                //we publish the ModelPrepared event for collection as the IEnumerable<BaseAgsModel>,
                //so you need to implement IConsumer<ModelPrepared<IEnumerable<BaseAgsModel>>> interface to handle this event
                eventPublisher.ModelPrepared(modelCollection);
            }
        }
            /// <summary>
            /// Called after the action executes, before the action result
            /// </summary>
            /// <param name="context">A context for action filters</param>
            public void OnActionExecuted(ActionExecutedContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                //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;
                }

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

                //model prepared event
                if (context.Controller is Controller controller && 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
                    _eventPublisher.ModelPrepared(model);
                }
            }