diff --git a/src/Umbraco.Core/Events/DataTypeDeletedNotification.cs b/src/Umbraco.Core/Events/DataTypeDeletedNotification.cs new file mode 100644 index 0000000000..b3461e27f1 --- /dev/null +++ b/src/Umbraco.Core/Events/DataTypeDeletedNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class DataTypeDeletedNotification : DeletedNotification + { + public DataTypeDeletedNotification(IDataType target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/DataTypeDeletingNotification.cs b/src/Umbraco.Core/Events/DataTypeDeletingNotification.cs new file mode 100644 index 0000000000..16b2ee68ac --- /dev/null +++ b/src/Umbraco.Core/Events/DataTypeDeletingNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class DataTypeDeletingNotification : DeletingNotification + { + public DataTypeDeletingNotification(IDataType target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/DataTypeMovedNotification.cs b/src/Umbraco.Core/Events/DataTypeMovedNotification.cs new file mode 100644 index 0000000000..10e42c7a82 --- /dev/null +++ b/src/Umbraco.Core/Events/DataTypeMovedNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class DataTypeMovedNotification : MovedNotification + { + public DataTypeMovedNotification(MoveEventInfo target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/DataTypeMovingNotification.cs b/src/Umbraco.Core/Events/DataTypeMovingNotification.cs new file mode 100644 index 0000000000..4b48f51a00 --- /dev/null +++ b/src/Umbraco.Core/Events/DataTypeMovingNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class DataTypeMovingNotification : MovingNotification + { + public DataTypeMovingNotification(MoveEventInfo target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/DataTypeSavedNotification.cs b/src/Umbraco.Core/Events/DataTypeSavedNotification.cs new file mode 100644 index 0000000000..09c42d32a9 --- /dev/null +++ b/src/Umbraco.Core/Events/DataTypeSavedNotification.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class DataTypeSavedNotification : SavedNotification + { + public DataTypeSavedNotification(IDataType target, EventMessages messages) : base(target, messages) + { + } + + public DataTypeSavedNotification(IEnumerable target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/DataTypeSavingNotification.cs b/src/Umbraco.Core/Events/DataTypeSavingNotification.cs new file mode 100644 index 0000000000..dd110df5a3 --- /dev/null +++ b/src/Umbraco.Core/Events/DataTypeSavingNotification.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class DataTypeSavingNotification : SavingNotification + { + public DataTypeSavingNotification(IDataType target, EventMessages messages) : base(target, messages) + { + } + + public DataTypeSavingNotification(IEnumerable target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/DeletingNotification.cs b/src/Umbraco.Core/Events/DeletingNotification.cs index 46fc9b9a03..d88345b427 100644 --- a/src/Umbraco.Core/Events/DeletingNotification.cs +++ b/src/Umbraco.Core/Events/DeletingNotification.cs @@ -1,6 +1,5 @@ // Copyright (c) Umbraco. // See LICENSE for more details. - using System.Collections.Generic; namespace Umbraco.Cms.Core.Events diff --git a/src/Umbraco.Core/Events/EntityContainerDeletedNotification.cs b/src/Umbraco.Core/Events/EntityContainerDeletedNotification.cs new file mode 100644 index 0000000000..eca96962be --- /dev/null +++ b/src/Umbraco.Core/Events/EntityContainerDeletedNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class EntityContainerDeletedNotification : DeletedNotification + { + public EntityContainerDeletedNotification(EntityContainer target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/EntityContainerDeletingNotification.cs b/src/Umbraco.Core/Events/EntityContainerDeletingNotification.cs new file mode 100644 index 0000000000..58d0830cf9 --- /dev/null +++ b/src/Umbraco.Core/Events/EntityContainerDeletingNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class EntityContainerDeletingNotification : DeletingNotification + { + public EntityContainerDeletingNotification(EntityContainer target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/EntityContainerRenamedNotification.cs b/src/Umbraco.Core/Events/EntityContainerRenamedNotification.cs new file mode 100644 index 0000000000..dc1b858bd9 --- /dev/null +++ b/src/Umbraco.Core/Events/EntityContainerRenamedNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class EntityContainerRenamedNotification : RenamedNotification + { + public EntityContainerRenamedNotification(EntityContainer target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/EntityContainerRenamingNotification.cs b/src/Umbraco.Core/Events/EntityContainerRenamingNotification.cs new file mode 100644 index 0000000000..f2408a1faf --- /dev/null +++ b/src/Umbraco.Core/Events/EntityContainerRenamingNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class EntityContainerRenamingNotification : RenamingNotification + { + public EntityContainerRenamingNotification(EntityContainer target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/EntityContainerSavedNotification.cs b/src/Umbraco.Core/Events/EntityContainerSavedNotification.cs new file mode 100644 index 0000000000..108d6649df --- /dev/null +++ b/src/Umbraco.Core/Events/EntityContainerSavedNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class EntityContainerSavedNotification : SavedNotification + { + public EntityContainerSavedNotification(EntityContainer target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/EntityContainerSavingNotification.cs b/src/Umbraco.Core/Events/EntityContainerSavingNotification.cs new file mode 100644 index 0000000000..98a8a34926 --- /dev/null +++ b/src/Umbraco.Core/Events/EntityContainerSavingNotification.cs @@ -0,0 +1,11 @@ +using Umbraco.Cms.Core.Models; + +namespace Umbraco.Cms.Core.Events +{ + public class EntityContainerSavingNotification : SavingNotification + { + public EntityContainerSavingNotification(EntityContainer target, EventMessages messages) : base(target, messages) + { + } + } +} diff --git a/src/Umbraco.Core/Events/RenamedNotification.cs b/src/Umbraco.Core/Events/RenamedNotification.cs new file mode 100644 index 0000000000..81a568b6df --- /dev/null +++ b/src/Umbraco.Core/Events/RenamedNotification.cs @@ -0,0 +1,20 @@ +// Copyright (c) Umbraco. +// See LICENSE for more details. + +using System.Collections.Generic; + +namespace Umbraco.Cms.Core.Events +{ + public abstract class RenamedNotification : EnumerableObjectNotification + { + protected RenamedNotification(T target, EventMessages messages) : base(target, messages) + { + } + + protected RenamedNotification(IEnumerable target, EventMessages messages) : base(target, messages) + { + } + + public IEnumerable Entities => Target; + } +} diff --git a/src/Umbraco.Core/Events/RenamingNotification.cs b/src/Umbraco.Core/Events/RenamingNotification.cs new file mode 100644 index 0000000000..f215a8ea54 --- /dev/null +++ b/src/Umbraco.Core/Events/RenamingNotification.cs @@ -0,0 +1,20 @@ +// Copyright (c) Umbraco. +// See LICENSE for more details. + +using System.Collections.Generic; + +namespace Umbraco.Cms.Core.Events +{ + public abstract class RenamingNotification : CancelableEnumerableObjectNotification + { + protected RenamingNotification(T target, EventMessages messages) : base(target, messages) + { + } + + protected RenamingNotification(IEnumerable target, EventMessages messages) : base(target, messages) + { + } + + public IEnumerable Entities => Target; + } +} diff --git a/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs b/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs index 0782579497..41195be9fe 100644 --- a/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs +++ b/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs @@ -33,7 +33,9 @@ namespace Umbraco.Cms.Core.Cache INotificationHandler, INotificationHandler, INotificationHandler, - INotificationHandler + INotificationHandler, + INotificationHandler, + INotificationHandler { private List _unbinders; @@ -64,12 +66,6 @@ namespace Umbraco.Cms.Core.Cache _logger.LogInformation("Initializing Umbraco internal event handlers for cache refreshing."); - // bind to data type events - Bind(() => DataTypeService.Deleted += DataTypeService_Deleted, - () => DataTypeService.Deleted -= DataTypeService_Deleted); - Bind(() => DataTypeService.Saved += DataTypeService_Saved, - () => DataTypeService.Saved -= DataTypeService_Saved); - // bind to stylesheet events Bind(() => FileService.SavedStylesheet += FileService_SavedStylesheet, () => FileService.SavedStylesheet -= FileService_SavedStylesheet); @@ -191,16 +187,20 @@ namespace Umbraco.Cms.Core.Cache #region DataTypeService - private void DataTypeService_Saved(IDataTypeService sender, SaveEventArgs e) + public void Handle(DataTypeSavedNotification notification) { - foreach (var entity in e.SavedEntities) + foreach (IDataType entity in notification.SavedEntities) + { _distributedCache.RefreshDataTypeCache(entity); + } } - private void DataTypeService_Deleted(IDataTypeService sender, DeleteEventArgs e) + public void Handle(DataTypeDeletedNotification notification) { - foreach (var entity in e.DeletedEntities) + foreach (IDataType entity in notification.DeletedEntities) + { _distributedCache.RemoveDataTypeCache(entity); + } } #endregion diff --git a/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs b/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs index be6539ac55..e9e7bf30af 100644 --- a/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs +++ b/src/Umbraco.Infrastructure/Compose/NotificationsComposer.cs @@ -78,7 +78,11 @@ namespace Umbraco.Cms.Core.Compose .AddNotificationHandler() .AddNotificationHandler() .AddNotificationHandler() - .AddNotificationHandler(); + .AddNotificationHandler() + .AddNotificationHandler() + .AddNotificationHandler() + .AddNotificationHandler() + .AddNotificationHandler(); // add notification handlers for auditing builder diff --git a/src/Umbraco.Infrastructure/Services/Implement/DataTypeService.cs b/src/Umbraco.Infrastructure/Services/Implement/DataTypeService.cs index a2ef8f4544..4bb1e46c29 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/DataTypeService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/DataTypeService.cs @@ -68,7 +68,8 @@ namespace Umbraco.Cms.Core.Services.Implement CreatorId = userId }; - if (scope.Events.DispatchCancelable(SavingContainer, this, new SaveEventArgs(container, evtMsgs))) + var savingEntityContainerNotification = new EntityContainerSavingNotification(container, evtMsgs); + if (scope.Notifications.PublishCancelable(savingEntityContainerNotification)) { scope.Complete(); return OperationResult.Attempt.Cancel(evtMsgs, container); @@ -77,7 +78,8 @@ namespace Umbraco.Cms.Core.Services.Implement _dataTypeContainerRepository.Save(container); scope.Complete(); - scope.Events.Dispatch(SavedContainer, this, new SaveEventArgs(container, evtMsgs)); + scope.Notifications.Publish(new EntityContainerSavedNotification(container, evtMsgs).WithStateFrom(savingEntityContainerNotification)); + // TODO: Audit trail ? return OperationResult.Attempt.Succeed(evtMsgs, container); @@ -153,7 +155,8 @@ namespace Umbraco.Cms.Core.Services.Implement using (var scope = ScopeProvider.CreateScope()) { - if (scope.Events.DispatchCancelable(SavingContainer, this, new SaveEventArgs(container, evtMsgs))) + var savingEntityContainerNotification = new EntityContainerSavingNotification(container, evtMsgs); + if (scope.Notifications.PublishCancelable(savingEntityContainerNotification)) { scope.Complete(); return OperationResult.Attempt.Cancel(evtMsgs); @@ -161,7 +164,7 @@ namespace Umbraco.Cms.Core.Services.Implement _dataTypeContainerRepository.Save(container); - scope.Events.Dispatch(SavedContainer, this, new SaveEventArgs(container, evtMsgs)); + scope.Notifications.Publish(new EntityContainerSavedNotification(container, evtMsgs).WithStateFrom(savingEntityContainerNotification)); scope.Complete(); } @@ -186,7 +189,8 @@ namespace Umbraco.Cms.Core.Services.Implement return Attempt.Fail(new OperationResult(OperationResultType.FailedCannot, evtMsgs)); } - if (scope.Events.DispatchCancelable(DeletingContainer, this, new DeleteEventArgs(container, evtMsgs))) + var deletingEntityContainerNotification = new EntityContainerDeletingNotification(container, evtMsgs); + if (scope.Notifications.PublishCancelable(deletingEntityContainerNotification)) { scope.Complete(); return Attempt.Fail(new OperationResult(OperationResultType.FailedCancelledByEvent, evtMsgs)); @@ -194,7 +198,7 @@ namespace Umbraco.Cms.Core.Services.Implement _dataTypeContainerRepository.Delete(container); - scope.Events.Dispatch(DeletedContainer, this, new DeleteEventArgs(container, evtMsgs)); + scope.Notifications.Publish(new EntityContainerDeletedNotification(container, evtMsgs).WithStateFrom(deletingEntityContainerNotification)); scope.Complete(); } @@ -217,11 +221,17 @@ namespace Umbraco.Cms.Core.Services.Implement container.Name = name; + var renamingEntityContainerNotification = new EntityContainerRenamingNotification(container, evtMsgs); + if (scope.Notifications.PublishCancelable(renamingEntityContainerNotification)) + { + scope.Complete(); + return OperationResult.Attempt.Cancel(evtMsgs, container); + } + _dataTypeContainerRepository.Save(container); scope.Complete(); - // TODO: triggering SavedContainer with a different name?! - scope.Events.Dispatch(SavedContainer, this, new SaveEventArgs(container, evtMsgs), "RenamedContainer"); + scope.Notifications.Publish(new EntityContainerRenamedNotification(container, evtMsgs).WithStateFrom(renamingEntityContainerNotification)); return OperationResult.Attempt.Succeed(OperationResultType.Success, evtMsgs, container); } @@ -341,8 +351,9 @@ namespace Umbraco.Cms.Core.Services.Implement using (var scope = ScopeProvider.CreateScope()) { var moveEventInfo = new MoveEventInfo(toMove, toMove.Path, parentId); - var moveEventArgs = new MoveEventArgs(evtMsgs, moveEventInfo); - if (scope.Events.DispatchCancelable(Moving, this, moveEventArgs)) + + var movingDataTypeNotification = new DataTypeMovingNotification(moveEventInfo, evtMsgs); + if (scope.Notifications.PublishCancelable(movingDataTypeNotification)) { scope.Complete(); return OperationResult.Attempt.Fail(MoveOperationStatusType.FailedCancelledByEvent, evtMsgs); @@ -359,9 +370,8 @@ namespace Umbraco.Cms.Core.Services.Implement } moveInfo.AddRange(_dataTypeRepository.Move(toMove, container)); - moveEventArgs.MoveInfoCollection = moveInfo; - moveEventArgs.CanCancel = false; - scope.Events.Dispatch(Moved, this, moveEventArgs); + scope.Notifications.Publish(new DataTypeMovedNotification(moveEventInfo, evtMsgs).WithStateFrom(movingDataTypeNotification)); + scope.Complete(); } catch (DataOperationException ex) @@ -381,12 +391,15 @@ namespace Umbraco.Cms.Core.Services.Implement /// Id of the user issuing the save public void Save(IDataType dataType, int userId = Cms.Core.Constants.Security.SuperUserId) { + var evtMsgs = EventMessagesFactory.Get(); dataType.CreatorId = userId; using (var scope = ScopeProvider.CreateScope()) { var saveEventArgs = new SaveEventArgs(dataType); - if (scope.Events.DispatchCancelable(Saving, this, saveEventArgs)) + + var savingDataTypeNotification = new DataTypeSavingNotification(dataType, evtMsgs); + if (scope.Notifications.PublishCancelable(savingDataTypeNotification)) { scope.Complete(); return; @@ -404,8 +417,8 @@ namespace Umbraco.Cms.Core.Services.Implement _dataTypeRepository.Save(dataType); - saveEventArgs.CanCancel = false; - scope.Events.Dispatch(Saved, this, saveEventArgs); + scope.Notifications.Publish(new DataTypeSavedNotification(dataType, evtMsgs).WithStateFrom(savingDataTypeNotification)); + Audit(AuditType.Save, userId, dataType.Id); scope.Complete(); } @@ -429,12 +442,13 @@ namespace Umbraco.Cms.Core.Services.Implement /// Boolean indicating whether or not to raise events public void Save(IEnumerable dataTypeDefinitions, int userId, bool raiseEvents) { + var evtMsgs = EventMessagesFactory.Get(); var dataTypeDefinitionsA = dataTypeDefinitions.ToArray(); - var saveEventArgs = new SaveEventArgs(dataTypeDefinitionsA); using (var scope = ScopeProvider.CreateScope()) { - if (raiseEvents && scope.Events.DispatchCancelable(Saving, this, saveEventArgs)) + var savingDataTypeNotification = new DataTypeSavingNotification(dataTypeDefinitions, evtMsgs); + if (raiseEvents && scope.Notifications.PublishCancelable(savingDataTypeNotification)) { scope.Complete(); return; @@ -448,8 +462,7 @@ namespace Umbraco.Cms.Core.Services.Implement if (raiseEvents) { - saveEventArgs.CanCancel = false; - scope.Events.Dispatch(Saved, this, saveEventArgs); + scope.Notifications.Publish(new DataTypeSavedNotification(dataTypeDefinitions, evtMsgs).WithStateFrom(savingDataTypeNotification)); } Audit(AuditType.Save, userId, -1); @@ -468,10 +481,11 @@ namespace Umbraco.Cms.Core.Services.Implement /// Optional Id of the user issuing the deletion public void Delete(IDataType dataType, int userId = Cms.Core.Constants.Security.SuperUserId) { + var evtMsgs = EventMessagesFactory.Get(); using (var scope = ScopeProvider.CreateScope()) { - var deleteEventArgs = new DeleteEventArgs(dataType); - if (scope.Events.DispatchCancelable(Deleting, this, deleteEventArgs)) + var deletingDataTypeNotification = new DataTypeDeletingNotification(dataType, evtMsgs); + if (scope.Notifications.PublishCancelable(deletingDataTypeNotification)) { scope.Complete(); return; @@ -506,8 +520,8 @@ namespace Umbraco.Cms.Core.Services.Implement _dataTypeRepository.Delete(dataType); - deleteEventArgs.CanCancel = false; - scope.Events.Dispatch(Deleted, this, deleteEventArgs); + scope.Notifications.Publish(new DataTypeDeletedNotification(dataType, evtMsgs).WithStateFrom(deletingDataTypeNotification)); + Audit(AuditType.Delete, userId, dataType.Id); scope.Complete(); @@ -527,42 +541,5 @@ namespace Umbraco.Cms.Core.Services.Implement _auditRepository.Save(new AuditItem(objectId, type, userId, ObjectTypes.GetName(UmbracoObjectTypes.DataType))); } - #region Event Handlers - - public static event TypedEventHandler> SavingContainer; - public static event TypedEventHandler> SavedContainer; - public static event TypedEventHandler> DeletingContainer; - public static event TypedEventHandler> DeletedContainer; - - /// - /// Occurs before Delete - /// - public static event TypedEventHandler> Deleting; - - /// - /// Occurs after Delete - /// - public static event TypedEventHandler> Deleted; - - /// - /// Occurs before Save - /// - public static event TypedEventHandler> Saving; - - /// - /// Occurs after Save - /// - public static event TypedEventHandler> Saved; - - /// - /// Occurs before Move - /// - public static event TypedEventHandler> Moving; - - /// - /// Occurs after Move - /// - public static event TypedEventHandler> Moved; - #endregion } } diff --git a/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs b/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs index 2d9bcf2c9d..80488e91b1 100644 --- a/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs +++ b/src/Umbraco.Tests.Integration/Cache/DistributedCacheBinderTests.cs @@ -42,9 +42,6 @@ namespace Umbraco.Cms.Tests.Integration.Cache var definitions = new IEventDefinition[] { - new EventDefinition>(null, DataTypeService, new SaveEventArgs(Enumerable.Empty())), - new EventDefinition>(null, DataTypeService, new DeleteEventArgs(Enumerable.Empty())), - new EventDefinition>(null, FileService, new SaveEventArgs(Enumerable.Empty())), new EventDefinition>(null, FileService, new DeleteEventArgs(Enumerable.Empty())),