diff --git a/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs b/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs index 9a36d9df7d..dae95d7e44 100644 --- a/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs +++ b/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs @@ -34,7 +34,8 @@ namespace Umbraco.Cms.Core.Cache INotificationHandler, INotificationHandler, INotificationHandler, - INotificationHandler + INotificationHandler, + INotificationHandler { private List _unbinders; @@ -85,10 +86,6 @@ namespace Umbraco.Cms.Core.Cache Bind(() => MemberTypeService.Changed += MemberTypeService_Changed, () => MemberTypeService.Changed -= MemberTypeService_Changed); - // bind to template events - Bind(() => FileService.SavedTemplate += FileService_SavedTemplate, - () => FileService.SavedTemplate -= FileService_SavedTemplate); - // bind to macro events Bind(() => MacroService.Saved += MacroService_Saved, () => MacroService.Saved -= MacroService_Saved); @@ -322,12 +319,13 @@ namespace Umbraco.Cms.Core.Cache /// /// Refresh cache for template /// - /// - /// - private void FileService_SavedTemplate(IFileService sender, SaveEventArgs e) + /// + public void Handle(TemplateSavedNotification notification) { - foreach (var entity in e.SavedEntities) + foreach (ITemplate entity in notification.SavedEntities) + { _distributedCache.RefreshTemplateCache(entity.Id); + } } #endregion diff --git a/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs b/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs index a0895762ea..d7e4fbaf2e 100644 --- a/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs +++ b/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs @@ -79,7 +79,8 @@ namespace Umbraco.Cms.Core.Compose .AddNotificationHandler() .AddNotificationHandler() .AddNotificationHandler() - .AddNotificationHandler(); + .AddNotificationHandler() + .AddNotificationHandler(); // add notification handlers for auditing builder diff --git a/src/Umbraco.Infrastructure/Services/Implement/FileService.cs b/src/Umbraco.Infrastructure/Services/Implement/FileService.cs index 1dfd4db498..fae26db8f7 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/FileService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/FileService.cs @@ -320,9 +320,9 @@ namespace Umbraco.Cms.Core.Services.Implement // This fixes: http://issues.umbraco.org/issue/U4-7953 contentTypeName); - var evtMsgs = EventMessagesFactory.Get(); + EventMessages eventMessages = EventMessagesFactory.Get(); - // TODO: This isn't pretty because we we're required to maintain backwards compatibility so we could not change + // TODO: This isn't pretty because we're required to maintain backwards compatibility so we could not change // the event args here. The other option is to create a different event with different event // args specifically for this method... which also isn't pretty. So fix this in v8! var additionalData = new Dictionary @@ -344,27 +344,23 @@ namespace Umbraco.Cms.Core.Services.Implement template.Content = content; } - - - - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - var saveEventArgs = new SaveEventArgs(template, true, evtMsgs, additionalData); - if (scope.Events.DispatchCancelable(SavingTemplate, this, saveEventArgs)) + var savingEvent = new TemplateSavingNotification(template, eventMessages, additionalData); + if (scope.Notifications.PublishCancelable(savingEvent)) { scope.Complete(); - return OperationResult.Attempt.Fail(OperationResultType.FailedCancelledByEvent, evtMsgs, template); + return OperationResult.Attempt.Fail(OperationResultType.FailedCancelledByEvent, eventMessages, template); } _templateRepository.Save(template); - saveEventArgs.CanCancel = false; - scope.Events.Dispatch(SavedTemplate, this, saveEventArgs); + scope.Notifications.Publish(new TemplateSavedNotification(template, eventMessages, savingEvent.AdditionalData).WithStateFrom(savingEvent)); Audit(AuditType.Save, userId, template.Id, ObjectTypes.GetName(UmbracoObjectTypes.Template)); scope.Complete(); } - return OperationResult.Attempt.Succeed(OperationResultType.Success, evtMsgs, template); + return OperationResult.Attempt.Succeed(OperationResultType.Success, eventMessages, template); } /// @@ -538,9 +534,11 @@ namespace Umbraco.Cms.Core.Services.Implement } - using (var scope = ScopeProvider.CreateScope()) + using (IScope scope = ScopeProvider.CreateScope()) { - if (scope.Events.DispatchCancelable(SavingTemplate, this, new SaveEventArgs(template))) + EventMessages eventMessages = EventMessagesFactory.Get(); + var savingNotification = new TemplateSavingNotification(template, eventMessages); + if (scope.Notifications.PublishCancelable(savingNotification)) { scope.Complete(); return; @@ -548,7 +546,7 @@ namespace Umbraco.Cms.Core.Services.Implement _templateRepository.Save(template); - scope.Events.Dispatch(SavedTemplate, this, new SaveEventArgs(template, false)); + scope.Notifications.Publish(new TemplateSavedNotification(template, eventMessages).WithStateFrom(savingNotification)); Audit(AuditType.Save, userId, template.Id, UmbracoObjectTypes.Template.GetName()); scope.Complete(); @@ -562,19 +560,23 @@ namespace Umbraco.Cms.Core.Services.Implement /// Optional id of the user public void SaveTemplate(IEnumerable templates, int userId = Cms.Core.Constants.Security.SuperUserId) { - var templatesA = templates.ToArray(); - using (var scope = ScopeProvider.CreateScope()) + ITemplate[] templatesA = templates.ToArray(); + using (IScope scope = ScopeProvider.CreateScope()) { - if (scope.Events.DispatchCancelable(SavingTemplate, this, new SaveEventArgs(templatesA))) + EventMessages eventMessages = EventMessagesFactory.Get(); + var savingNotification = new TemplateSavingNotification(templatesA, eventMessages); + if (scope.Notifications.PublishCancelable(savingNotification)) { scope.Complete(); return; } - foreach (var template in templatesA) + foreach (ITemplate template in templatesA) + { _templateRepository.Save(template); + } - scope.Events.Dispatch(SavedTemplate, this, new SaveEventArgs(templatesA, false)); + scope.Notifications.Publish(new TemplateSavingNotification(templatesA, eventMessages).WithStateFrom(savingNotification)); Audit(AuditType.Save, userId, -1, UmbracoObjectTypes.Template.GetName()); scope.Complete(); @@ -1065,16 +1067,6 @@ namespace Umbraco.Cms.Core.Services.Implement #region Event Handlers - /// - /// Occurs before Save - /// - public static event TypedEventHandler> SavingTemplate; - - /// - /// Occurs after Save - /// - public static event TypedEventHandler> SavedTemplate; - /// /// Occurs before Save /// diff --git a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs index 26f300dccd..5bb971089f 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs +++ b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs @@ -16,5 +16,13 @@ namespace Umbraco.Cms.Infrastructure.Services.Notifications public TemplateSavedNotification(IEnumerable target, EventMessages messages) : base(target, messages) { } + + public TemplateSavedNotification(ITemplate target, EventMessages messages, + Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; + + public TemplateSavedNotification(IEnumerable target, EventMessages messages, + Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; + + public Dictionary AdditionalData; } } diff --git a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs index eaf01c6b31..fc12eda6c3 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs +++ b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs @@ -16,5 +16,13 @@ namespace Umbraco.Cms.Infrastructure.Services.Notifications public TemplateSavingNotification(IEnumerable target, EventMessages messages) : base(target, messages) { } + + public TemplateSavingNotification(ITemplate target, EventMessages messages, + Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; + + public TemplateSavingNotification(IEnumerable target, EventMessages messages, + Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; + + public Dictionary AdditionalData; } } diff --git a/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs b/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs index e0e4e13ca8..16f32f92b9 100644 --- a/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs +++ b/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs @@ -56,8 +56,6 @@ namespace Umbraco.Cms.Tests.Integration.Cache new EventDefinition>(null, MemberTypeService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, MemberTypeService, new DeleteEventArgs(Enumerable.Empty())), - new EventDefinition>(null, FileService, new SaveEventArgs(Enumerable.Empty())), - new EventDefinition>(null, MacroService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, MacroService, new DeleteEventArgs(Enumerable.Empty())), diff --git a/src/Umbraco.Web.Common/ModelsBuilder/DependencyInjection/UmbracoBuilderDependencyInjectionExtensions.cs b/src/Umbraco.Web.Common/ModelsBuilder/DependencyInjection/UmbracoBuilderDependencyInjectionExtensions.cs index 8762dc6cc3..31bb23ed97 100644 --- a/src/Umbraco.Web.Common/ModelsBuilder/DependencyInjection/UmbracoBuilderDependencyInjectionExtensions.cs +++ b/src/Umbraco.Web.Common/ModelsBuilder/DependencyInjection/UmbracoBuilderDependencyInjectionExtensions.cs @@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Infrastructure.ModelsBuilder; using Umbraco.Cms.Infrastructure.ModelsBuilder.Building; +using Umbraco.Cms.Infrastructure.Services.Notifications; using Umbraco.Cms.Infrastructure.WebAssets; using Umbraco.Cms.Web.Common.ModelBinders; using Umbraco.Cms.Web.Common.ModelsBuilder; @@ -98,7 +99,7 @@ namespace Umbraco.Extensions // TODO: I feel like we could just do builder.AddNotificationHandler() and it // would automatically just register for all implemented INotificationHandler{T}? - builder.AddNotificationHandler(); + builder.AddNotificationHandler(); builder.AddNotificationHandler(); builder.AddNotificationHandler(); builder.AddNotificationHandler(); diff --git a/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs b/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs index 8f4454bf2e..7595b42eb4 100644 --- a/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs +++ b/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs @@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Services.Implement; using Umbraco.Cms.Core.Strings; using Umbraco.Cms.Infrastructure.ModelsBuilder; +using Umbraco.Cms.Infrastructure.Services.Notifications; using Umbraco.Cms.Infrastructure.WebAssets; using Umbraco.Cms.Web.Common.ModelBinders; @@ -19,7 +20,10 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder /// /// Handles and notifications to initialize MB /// - internal class ModelsBuilderNotificationHandler : INotificationHandler, INotificationHandler, INotificationHandler + internal class ModelsBuilderNotificationHandler : + INotificationHandler, + INotificationHandler, + INotificationHandler { private readonly ModelsBuilderSettings _config; private readonly IShortStringHelper _shortStringHelper; @@ -35,19 +39,6 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder _modelsBuilderDashboardProvider = modelsBuilderDashboardProvider; } - /// - /// Handles the notification - /// - public void Handle(UmbracoApplicationStarting notification) - { - // always setup the dashboard - // note: UmbracoApiController instances are automatically registered - if (_config.ModelsMode != ModelsMode.Nothing) - { - FileService.SavingTemplate += FileService_SavingTemplate; - } - } - /// /// Handles the notification to add custom urls and MB mode /// @@ -99,21 +90,26 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder /// Used to check if a template is being created based on a document type, in this case we need to /// ensure the template markup is correct based on the model name of the document type /// - private void FileService_SavingTemplate(IFileService sender, SaveEventArgs e) + public void Handle(TemplateSavingNotification notification) { + if (_config.ModelsMode == ModelsMode.Nothing) + { + return; + } + // don't do anything if this special key is not found - if (!e.AdditionalData.ContainsKey("CreateTemplateForContentType")) + if (!notification.AdditionalData.ContainsKey("CreateTemplateForContentType")) { return; } // ensure we have the content type alias - if (!e.AdditionalData.ContainsKey("ContentTypeAlias")) + if (!notification.AdditionalData.ContainsKey("ContentTypeAlias")) { throw new InvalidOperationException("The additionalData key: ContentTypeAlias was not found"); } - foreach (ITemplate template in e.SavedEntities) + foreach (ITemplate template in notification.SavedEntities) { // if it is in fact a new entity (not been saved yet) and the "CreateTemplateForContentType" key // is found, then it means a new template is being created based on the creation of a document type @@ -121,7 +117,7 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder { // ensure is safe and always pascal cased, per razor standard // + this is how we get the default model name in Umbraco.ModelsBuilder.Umbraco.Application - var alias = e.AdditionalData["ContentTypeAlias"].ToString(); + var alias = notification.AdditionalData["ContentTypeAlias"].ToString(); var name = template.Name; // will be the name of the content type since we are creating var className = UmbracoServices.GetClrName(_shortStringHelper, name, alias);