Use notifications instead of events.

This commit is contained in:
Mole
2021-03-30 12:14:32 +02:00
parent 1b3468303c
commit 5c4a6b8f6e
4 changed files with 30 additions and 71 deletions

View File

@@ -33,7 +33,8 @@ namespace Umbraco.Cms.Core.Cache
INotificationHandler<UserGroupWithUsersSavedNotification>,
INotificationHandler<UserGroupDeletedNotification>,
INotificationHandler<MemberGroupDeletedNotification>,
INotificationHandler<MemberGroupSavedNotification>
INotificationHandler<MemberGroupSavedNotification>,
INotificationHandler<TemplateDeletedNotification>
{
private List<Action> _unbinders;
@@ -70,12 +71,6 @@ namespace Umbraco.Cms.Core.Cache
Bind(() => DataTypeService.Saved += DataTypeService_Saved,
() => DataTypeService.Saved -= DataTypeService_Saved);
// bind to stylesheet events
Bind(() => FileService.SavedStylesheet += FileService_SavedStylesheet,
() => FileService.SavedStylesheet -= FileService_SavedStylesheet);
Bind(() => FileService.DeletedStylesheet += FileService_DeletedStylesheet,
() => FileService.DeletedStylesheet -= FileService_DeletedStylesheet);
// bind to domain events
Bind(() => DomainService.Saved += DomainService_Saved,
() => DomainService.Saved -= DomainService_Saved);
@@ -93,8 +88,6 @@ namespace Umbraco.Cms.Core.Cache
// bind to template events
Bind(() => FileService.SavedTemplate += FileService_SavedTemplate,
() => FileService.SavedTemplate -= FileService_SavedTemplate);
Bind(() => FileService.DeletedTemplate += FileService_DeletedTemplate,
() => FileService.DeletedTemplate -= FileService_DeletedTemplate);
// bind to macro events
Bind(() => MacroService.Saved += MacroService_Saved,
@@ -317,12 +310,13 @@ namespace Umbraco.Cms.Core.Cache
/// <summary>
/// Removes cache for template
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileService_DeletedTemplate(IFileService sender, DeleteEventArgs<ITemplate> e)
/// <param name="notification"></param>
public void Handle(TemplateDeletedNotification notification)
{
foreach (var entity in e.DeletedEntities)
foreach (ITemplate entity in notification.DeletedEntities)
{
_distributedCache.RemoveTemplateCache(entity.Id);
}
}
/// <summary>
@@ -336,10 +330,6 @@ namespace Umbraco.Cms.Core.Cache
_distributedCache.RefreshTemplateCache(entity.Id);
}
// TODO: our weird events handling wants this for now
private void FileService_DeletedStylesheet(IFileService sender, DeleteEventArgs<IStylesheet> e) { }
private void FileService_SavedStylesheet(IFileService sender, SaveEventArgs<IStylesheet> e) { }
#endregion
#region MacroService

View File

@@ -78,7 +78,8 @@ namespace Umbraco.Cms.Core.Compose
.AddNotificationHandler<UserSavedNotification, DistributedCacheBinder>()
.AddNotificationHandler<UserDeletedNotification, DistributedCacheBinder>()
.AddNotificationHandler<UserGroupWithUsersSavedNotification, DistributedCacheBinder>()
.AddNotificationHandler<UserGroupDeletedNotification, DistributedCacheBinder>();
.AddNotificationHandler<UserGroupDeletedNotification, DistributedCacheBinder>()
.AddNotificationHandler<TemplateDeletedNotification, DistributedCacheBinder>();
// add notification handlers for auditing
builder

View File

@@ -12,6 +12,7 @@ using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.Services.Notifications;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Services.Implement
@@ -96,25 +97,26 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <inheritdoc />
public void DeleteStylesheet(string path, int userId = Cms.Core.Constants.Security.SuperUserId)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
var stylesheet = _stylesheetRepository.Get(path);
IStylesheet stylesheet = _stylesheetRepository.Get(path);
if (stylesheet == null)
{
scope.Complete();
return;
}
var deleteEventArgs = new DeleteEventArgs<IStylesheet>(stylesheet);
if (scope.Events.DispatchCancelable(DeletingStylesheet, this, deleteEventArgs))
EventMessages eventMessages = EventMessagesFactory.Get();
var deletingNotification = new StylesheetDeletingNotification(stylesheet, eventMessages);
if (scope.Notifications.PublishCancelable(deletingNotification))
{
scope.Complete();
return; // causes rollback // causes rollback
return; // causes rollback
}
_stylesheetRepository.Delete(stylesheet);
deleteEventArgs.CanCancel = false;
scope.Events.Dispatch(DeletedStylesheet, this, deleteEventArgs);
scope.Notifications.Publish(new StylesheetDeletedNotification(stylesheet, eventMessages).WithStateFrom(deletingNotification));
Audit(AuditType.Delete, userId, -1, "Stylesheet");
scope.Complete();
@@ -219,25 +221,25 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <inheritdoc />
public void DeleteScript(string path, int userId = Cms.Core.Constants.Security.SuperUserId)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
var script = _scriptRepository.Get(path);
IScript script = _scriptRepository.Get(path);
if (script == null)
{
scope.Complete();
return;
}
var deleteEventArgs = new DeleteEventArgs<IScript>(script);
if (scope.Events.DispatchCancelable(DeletingScript, this, deleteEventArgs))
EventMessages eventMessages = EventMessagesFactory.Get();
var deletingNotification = new ScriptDeletingNotification(script, eventMessages);
if (scope.Notifications.PublishCancelable(deletingNotification))
{
scope.Complete();
return;
}
_scriptRepository.Delete(script);
deleteEventArgs.CanCancel = false;
scope.Events.Dispatch(DeletedScript, this, deleteEventArgs);
scope.Notifications.Publish(new ScriptDeletedNotification(script, eventMessages).WithStateFrom(deletingNotification));
Audit(AuditType.Delete, userId, -1, "Script");
scope.Complete();
@@ -586,17 +588,18 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <param name="userId"></param>
public void DeleteTemplate(string alias, int userId = Cms.Core.Constants.Security.SuperUserId)
{
using (var scope = ScopeProvider.CreateScope())
using (IScope scope = ScopeProvider.CreateScope())
{
var template = _templateRepository.Get(alias);
ITemplate template = _templateRepository.Get(alias);
if (template == null)
{
scope.Complete();
return;
}
var args = new DeleteEventArgs<ITemplate>(template);
if (scope.Events.DispatchCancelable(DeletingTemplate, this, args))
EventMessages eventMessages = EventMessagesFactory.Get();
var deletingNotification = new TemplateDeletingNotification(template, eventMessages);
if (scope.Notifications.PublishCancelable(deletingNotification))
{
scope.Complete();
return;
@@ -604,8 +607,7 @@ namespace Umbraco.Cms.Core.Services.Implement
_templateRepository.Delete(template);
args.CanCancel = false;
scope.Events.Dispatch(DeletedTemplate, this, args);
scope.Notifications.Publish(new TemplateDeletedNotification(template, eventMessages).WithStateFrom(deletingNotification));
Audit(AuditType.Delete, userId, template.Id, ObjectTypes.GetName(UmbracoObjectTypes.Template));
scope.Complete();
@@ -1063,36 +1065,6 @@ namespace Umbraco.Cms.Core.Services.Implement
#region Event Handlers
/// <summary>
/// Occurs before Delete
/// </summary>
public static event TypedEventHandler<IFileService, DeleteEventArgs<ITemplate>> DeletingTemplate;
/// <summary>
/// Occurs after Delete
/// </summary>
public static event TypedEventHandler<IFileService, DeleteEventArgs<ITemplate>> DeletedTemplate;
/// <summary>
/// Occurs before Delete
/// </summary>
public static event TypedEventHandler<IFileService, DeleteEventArgs<IScript>> DeletingScript;
/// <summary>
/// Occurs after Delete
/// </summary>
public static event TypedEventHandler<IFileService, DeleteEventArgs<IScript>> DeletedScript;
/// <summary>
/// Occurs before Delete
/// </summary>
public static event TypedEventHandler<IFileService, DeleteEventArgs<IStylesheet>> DeletingStylesheet;
/// <summary>
/// Occurs after Delete
/// </summary>
public static event TypedEventHandler<IFileService, DeleteEventArgs<IStylesheet>> DeletedStylesheet;
/// <summary>
/// Occurs before Save
/// </summary>

View File

@@ -45,9 +45,6 @@ namespace Umbraco.Cms.Tests.Integration.Cache
new EventDefinition<IDataTypeService, SaveEventArgs<IDataType>>(null, DataTypeService, new SaveEventArgs<IDataType>(Enumerable.Empty<IDataType>())),
new EventDefinition<IDataTypeService, DeleteEventArgs<IDataType>>(null, DataTypeService, new DeleteEventArgs<IDataType>(Enumerable.Empty<IDataType>())),
new EventDefinition<IFileService, SaveEventArgs<IStylesheet>>(null, FileService, new SaveEventArgs<IStylesheet>(Enumerable.Empty<IStylesheet>())),
new EventDefinition<IFileService, DeleteEventArgs<IStylesheet>>(null, FileService, new DeleteEventArgs<IStylesheet>(Enumerable.Empty<IStylesheet>())),
new EventDefinition<IDomainService, SaveEventArgs<IDomain>>(null, DomainService, new SaveEventArgs<IDomain>(Enumerable.Empty<IDomain>())),
new EventDefinition<IDomainService, DeleteEventArgs<IDomain>>(null, DomainService, new DeleteEventArgs<IDomain>(Enumerable.Empty<IDomain>())),
@@ -60,7 +57,6 @@ namespace Umbraco.Cms.Tests.Integration.Cache
new EventDefinition<IMemberTypeService, DeleteEventArgs<IMemberType>>(null, MemberTypeService, new DeleteEventArgs<IMemberType>(Enumerable.Empty<IMemberType>())),
new EventDefinition<IFileService, SaveEventArgs<ITemplate>>(null, FileService, new SaveEventArgs<ITemplate>(Enumerable.Empty<ITemplate>())),
new EventDefinition<IFileService, DeleteEventArgs<ITemplate>>(null, FileService, new DeleteEventArgs<ITemplate>(Enumerable.Empty<ITemplate>())),
new EventDefinition<IMacroService, SaveEventArgs<IMacro>>(null, MacroService, new SaveEventArgs<IMacro>(Enumerable.Empty<IMacro>())),
new EventDefinition<IMacroService, DeleteEventArgs<IMacro>>(null, MacroService, new DeleteEventArgs<IMacro>(Enumerable.Empty<IMacro>())),