From 06c5ce5bb870156495a25c8fee9a175004c7538e Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Wed, 24 Feb 2021 08:43:50 +0100 Subject: [PATCH 1/2] Replace static editor model events events with IEventAggregator notifications --- .../Editors/EditorModelEventArgs.cs | 47 ------------ .../Filters/EditorModelEventManager.cs | 75 ------------------- .../OutgoingEditorModelEventAttribute.cs | 38 ++++++++-- .../Filters/SendingContentNotification.cs | 19 +++++ .../Filters/SendingDashboardsNotification.cs | 21 ++++++ .../Filters/SendingMediaNotification.cs | 19 +++++ .../Filters/SendingMemberNotification.cs | 19 +++++ .../Filters/SendingUserNotification.cs | 19 +++++ 8 files changed, 127 insertions(+), 130 deletions(-) delete mode 100644 src/Umbraco.Core/Editors/EditorModelEventArgs.cs delete mode 100644 src/Umbraco.Web.BackOffice/Filters/EditorModelEventManager.cs create mode 100644 src/Umbraco.Web.BackOffice/Filters/SendingContentNotification.cs create mode 100644 src/Umbraco.Web.BackOffice/Filters/SendingDashboardsNotification.cs create mode 100644 src/Umbraco.Web.BackOffice/Filters/SendingMediaNotification.cs create mode 100644 src/Umbraco.Web.BackOffice/Filters/SendingMemberNotification.cs create mode 100644 src/Umbraco.Web.BackOffice/Filters/SendingUserNotification.cs diff --git a/src/Umbraco.Core/Editors/EditorModelEventArgs.cs b/src/Umbraco.Core/Editors/EditorModelEventArgs.cs deleted file mode 100644 index da7f0ba9cb..0000000000 --- a/src/Umbraco.Core/Editors/EditorModelEventArgs.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using Umbraco.Cms.Core.Web; - -namespace Umbraco.Cms.Core.Editors -{ - public sealed class EditorModelEventArgs : EditorModelEventArgs - { - private readonly EditorModelEventArgs _baseArgs; - private T _model; - - public EditorModelEventArgs(EditorModelEventArgs baseArgs) - : base(baseArgs.Model, baseArgs.UmbracoContext) - { - _baseArgs = baseArgs; - Model = (T)baseArgs.Model; - } - - public EditorModelEventArgs(T model, IUmbracoContext umbracoContext) - : base(model, umbracoContext) - { - Model = model; - } - - public new T Model - { - get => _model; - set - { - _model = value; - if (_baseArgs != null) - _baseArgs.Model = _model; - } - } - } - - public class EditorModelEventArgs : EventArgs - { - public EditorModelEventArgs(object model, IUmbracoContext umbracoContext) - { - Model = model; - UmbracoContext = umbracoContext; - } - - public object Model { get; set; } - public IUmbracoContext UmbracoContext { get; } - } -} diff --git a/src/Umbraco.Web.BackOffice/Filters/EditorModelEventManager.cs b/src/Umbraco.Web.BackOffice/Filters/EditorModelEventManager.cs deleted file mode 100644 index a76b1a4091..0000000000 --- a/src/Umbraco.Web.BackOffice/Filters/EditorModelEventManager.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc.Filters; -using Umbraco.Cms.Core.Dashboards; -using Umbraco.Cms.Core.Editors; -using Umbraco.Cms.Core.Events; -using Umbraco.Cms.Core.Models.ContentEditing; - -namespace Umbraco.Cms.Web.BackOffice.Filters -{ - /// - /// Used to emit events for editor models in the back office - /// - public sealed class EditorModelEventManager - { - public static event TypedEventHandler> SendingContentModel; - public static event TypedEventHandler> SendingMediaModel; - public static event TypedEventHandler> SendingMemberModel; - public static event TypedEventHandler> SendingUserModel; - - public static event TypedEventHandler>>> SendingDashboardSlimModel; - - private static void OnSendingDashboardModel(ActionExecutedContext sender, EditorModelEventArgs>> e) - { - var handler = SendingDashboardSlimModel; - handler?.Invoke(sender, e); - } - - private static void OnSendingUserModel(ActionExecutedContext sender, EditorModelEventArgs e) - { - var handler = SendingUserModel; - handler?.Invoke(sender, e); - } - - private static void OnSendingContentModel(ActionExecutedContext sender, EditorModelEventArgs e) - { - var handler = SendingContentModel; - handler?.Invoke(sender, e); - } - - private static void OnSendingMediaModel(ActionExecutedContext sender, EditorModelEventArgs e) - { - var handler = SendingMediaModel; - handler?.Invoke(sender, e); - } - - private static void OnSendingMemberModel(ActionExecutedContext sender, EditorModelEventArgs e) - { - var handler = SendingMemberModel; - handler?.Invoke(sender, e); - } - - /// - /// Based on the type, emit's a specific event - /// - /// - /// - internal static void EmitEvent(ActionExecutedContext sender, EditorModelEventArgs e) - { - if (e.Model is ContentItemDisplay) - OnSendingContentModel(sender, new EditorModelEventArgs(e)); - - if (e.Model is MediaItemDisplay) - OnSendingMediaModel(sender, new EditorModelEventArgs(e)); - - if (e.Model is MemberDisplay) - OnSendingMemberModel(sender, new EditorModelEventArgs(e)); - - if (e.Model is UserDisplay) - OnSendingUserModel(sender, new EditorModelEventArgs(e)); - - if (e.Model is IEnumerable>) - OnSendingDashboardModel(sender, new EditorModelEventArgs>>(e)); - } - } -} diff --git a/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs b/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs index fe825a1907..40ee00a2b2 100644 --- a/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs +++ b/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs @@ -1,7 +1,11 @@ -using System; +using System; +using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; +using Umbraco.Cms.Core.Dashboards; using Umbraco.Cms.Core.Editors; +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models.ContentEditing; using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Web; using Umbraco.Extensions; @@ -25,14 +29,18 @@ namespace Umbraco.Cms.Web.BackOffice.Filters private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; + private readonly IEventAggregator _eventAggregator; + public OutgoingEditorModelEventFilter( IUmbracoContextAccessor umbracoContextAccessor, - IBackOfficeSecurityAccessor backOfficeSecurityAccessor) + IBackOfficeSecurityAccessor backOfficeSecurityAccessor, IEventAggregator eventAggregator) { _umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); _backOfficeSecurityAccessor = backOfficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backOfficeSecurityAccessor)); + _eventAggregator = eventAggregator + ?? throw new ArgumentNullException(nameof(eventAggregator)); } public void OnActionExecuted(ActionExecutedContext context) @@ -40,18 +48,32 @@ namespace Umbraco.Cms.Web.BackOffice.Filters if (context.Result == null) return; var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); - var user = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser; - if (user == null) return; + var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser; + if (currentUser == null) return; if (context.Result is ObjectResult objectContent) { var model = objectContent.Value; - if (model != null) + if (model is ContentItemDisplay content) { - var args = new EditorModelEventArgs(model, umbracoContext); - EditorModelEventManager.EmitEvent(context, args); - objectContent.Value = args.Model; + _eventAggregator.Publish(new SendingContentNotification(content, umbracoContext)); + } + else if (model is MediaItemDisplay media) + { + _eventAggregator.Publish(new SendingMediaNotification(media, umbracoContext)); + } + else if (model is MemberDisplay member) + { + _eventAggregator.Publish(new SendingMemberNotification(member, umbracoContext)); + } + else if (model is UserDisplay user) + { + _eventAggregator.Publish(new SendingUserNotification(user, umbracoContext)); + } + else if (model is IEnumerable> dashboards) + { + _eventAggregator.Publish(new SendingDashboardsNotification(dashboards, umbracoContext)); } } } diff --git a/src/Umbraco.Web.BackOffice/Filters/SendingContentNotification.cs b/src/Umbraco.Web.BackOffice/Filters/SendingContentNotification.cs new file mode 100644 index 0000000000..3955c95e54 --- /dev/null +++ b/src/Umbraco.Web.BackOffice/Filters/SendingContentNotification.cs @@ -0,0 +1,19 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Core.Web; + +namespace Umbraco.Cms.Web.BackOffice.Filters +{ + public class SendingContentNotification : INotification + { + public IUmbracoContext UmbracoContext { get; } + + public ContentItemDisplay Content { get; } + + public SendingContentNotification(ContentItemDisplay content, IUmbracoContext umbracoContext) + { + Content = content; + UmbracoContext = umbracoContext; + } + } +} diff --git a/src/Umbraco.Web.BackOffice/Filters/SendingDashboardsNotification.cs b/src/Umbraco.Web.BackOffice/Filters/SendingDashboardsNotification.cs new file mode 100644 index 0000000000..cfaa91fc2b --- /dev/null +++ b/src/Umbraco.Web.BackOffice/Filters/SendingDashboardsNotification.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using Umbraco.Cms.Core.Dashboards; +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Core.Web; + +namespace Umbraco.Cms.Web.BackOffice.Filters +{ + public class SendingDashboardsNotification : INotification + { + public IUmbracoContext UmbracoContext { get; } + + public IEnumerable> Dashboards { get; } + + public SendingDashboardsNotification(IEnumerable> dashboards, IUmbracoContext umbracoContext) + { + Dashboards = dashboards; + UmbracoContext = umbracoContext; + } + } +} diff --git a/src/Umbraco.Web.BackOffice/Filters/SendingMediaNotification.cs b/src/Umbraco.Web.BackOffice/Filters/SendingMediaNotification.cs new file mode 100644 index 0000000000..cc5d7e7620 --- /dev/null +++ b/src/Umbraco.Web.BackOffice/Filters/SendingMediaNotification.cs @@ -0,0 +1,19 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Core.Web; + +namespace Umbraco.Cms.Web.BackOffice.Filters +{ + public class SendingMediaNotification : INotification + { + public IUmbracoContext UmbracoContext { get; } + + public MediaItemDisplay Media { get; } + + public SendingMediaNotification(MediaItemDisplay media, IUmbracoContext umbracoContext) + { + Media = media; + UmbracoContext = umbracoContext; + } + } +} diff --git a/src/Umbraco.Web.BackOffice/Filters/SendingMemberNotification.cs b/src/Umbraco.Web.BackOffice/Filters/SendingMemberNotification.cs new file mode 100644 index 0000000000..ea2a1d8927 --- /dev/null +++ b/src/Umbraco.Web.BackOffice/Filters/SendingMemberNotification.cs @@ -0,0 +1,19 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Core.Web; + +namespace Umbraco.Cms.Web.BackOffice.Filters +{ + public class SendingMemberNotification : INotification + { + public IUmbracoContext UmbracoContext { get; } + + public MemberDisplay Member { get; } + + public SendingMemberNotification(MemberDisplay member, IUmbracoContext umbracoContext) + { + Member = member; + UmbracoContext = umbracoContext; + } + } +} diff --git a/src/Umbraco.Web.BackOffice/Filters/SendingUserNotification.cs b/src/Umbraco.Web.BackOffice/Filters/SendingUserNotification.cs new file mode 100644 index 0000000000..91bbe2f0db --- /dev/null +++ b/src/Umbraco.Web.BackOffice/Filters/SendingUserNotification.cs @@ -0,0 +1,19 @@ +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Models.ContentEditing; +using Umbraco.Cms.Core.Web; + +namespace Umbraco.Cms.Web.BackOffice.Filters +{ + public class SendingUserNotification : INotification + { + public IUmbracoContext UmbracoContext { get; } + + public UserDisplay User { get; } + + public SendingUserNotification(UserDisplay user, IUmbracoContext umbracoContext) + { + User = user; + UmbracoContext = umbracoContext; + } + } +} From 0bdd1cc874cee8a3716b8827cf4f342dc31659c4 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Mon, 1 Mar 2021 13:17:24 +0100 Subject: [PATCH 2/2] Replaced if/else with switch --- .../OutgoingEditorModelEventAttribute.cs | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs b/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs index 40ee00a2b2..28f3dfbe60 100644 --- a/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs +++ b/src/Umbraco.Web.BackOffice/Filters/OutgoingEditorModelEventAttribute.cs @@ -55,25 +55,23 @@ namespace Umbraco.Cms.Web.BackOffice.Filters { var model = objectContent.Value; - if (model is ContentItemDisplay content) + switch (model) { - _eventAggregator.Publish(new SendingContentNotification(content, umbracoContext)); - } - else if (model is MediaItemDisplay media) - { - _eventAggregator.Publish(new SendingMediaNotification(media, umbracoContext)); - } - else if (model is MemberDisplay member) - { - _eventAggregator.Publish(new SendingMemberNotification(member, umbracoContext)); - } - else if (model is UserDisplay user) - { - _eventAggregator.Publish(new SendingUserNotification(user, umbracoContext)); - } - else if (model is IEnumerable> dashboards) - { - _eventAggregator.Publish(new SendingDashboardsNotification(dashboards, umbracoContext)); + case ContentItemDisplay content: + _eventAggregator.Publish(new SendingContentNotification(content, umbracoContext)); + break; + case MediaItemDisplay media: + _eventAggregator.Publish(new SendingMediaNotification(media, umbracoContext)); + break; + case MemberDisplay member: + _eventAggregator.Publish(new SendingMemberNotification(member, umbracoContext)); + break; + case UserDisplay user: + _eventAggregator.Publish(new SendingUserNotification(user, umbracoContext)); + break; + case IEnumerable> dashboards: + _eventAggregator.Publish(new SendingDashboardsNotification(dashboards, umbracoContext)); + break; } } }