Switch to INotificationHandler

This commit is contained in:
Mole
2021-03-25 14:51:00 +01:00
parent 18083b7155
commit c8471b096c
6 changed files with 82 additions and 94 deletions

View File

@@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Implement;
using Umbraco.Cms.Infrastructure.Services.Notifications;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Cache
@@ -18,7 +19,11 @@ namespace Umbraco.Cms.Core.Cache
/// <summary>
/// Default <see cref="IDistributedCacheBinder"/> implementation.
/// </summary>
public partial class DistributedCacheBinder
public partial class DistributedCacheBinder :
INotificationHandler<DictionaryItemDeletedNotification>,
INotificationHandler<DictionaryItemSavedNotification>,
INotificationHandler<LanguageSavedNotification>,
INotificationHandler<LanguageDeletedNotification>
{
private List<Action> _unbinders;
@@ -61,12 +66,6 @@ namespace Umbraco.Cms.Core.Cache
Bind(() => UserService.UserGroupPermissionsAssigned += UserService_UserGroupPermissionsAssigned,
() => UserService.UserGroupPermissionsAssigned -= UserService_UserGroupPermissionsAssigned);
// bind to dictionary events
Bind(() => LocalizationService.DeletedDictionaryItem += LocalizationService_DeletedDictionaryItem,
() => LocalizationService.DeletedDictionaryItem -= LocalizationService_DeletedDictionaryItem);
Bind(() => LocalizationService.SavedDictionaryItem += LocalizationService_SavedDictionaryItem,
() => LocalizationService.SavedDictionaryItem -= LocalizationService_SavedDictionaryItem);
// bind to data type events
Bind(() => DataTypeService.Deleted += DataTypeService_Deleted,
() => DataTypeService.Deleted -= DataTypeService_Deleted);
@@ -85,12 +84,6 @@ namespace Umbraco.Cms.Core.Cache
Bind(() => DomainService.Deleted += DomainService_Deleted,
() => DomainService.Deleted -= DomainService_Deleted);
// bind to language events
Bind(() => LocalizationService.SavedLanguage += LocalizationService_SavedLanguage,
() => LocalizationService.SavedLanguage -= LocalizationService_SavedLanguage);
Bind(() => LocalizationService.DeletedLanguage += LocalizationService_DeletedLanguage,
() => LocalizationService.DeletedLanguage -= LocalizationService_DeletedLanguage);
// bind to content type events
Bind(() => ContentTypeService.Changed += ContentTypeService_Changed,
() => ContentTypeService.Changed -= ContentTypeService_Changed);
@@ -196,17 +189,20 @@ namespace Umbraco.Cms.Core.Cache
#endregion
#region LocalizationService / Dictionary
private void LocalizationService_SavedDictionaryItem(ILocalizationService sender, SaveEventArgs<IDictionaryItem> e)
public void Handle(DictionaryItemSavedNotification notification)
{
foreach (var entity in e.SavedEntities)
foreach (IDictionaryItem entity in notification.SavedEntities)
{
_distributedCache.RefreshDictionaryCache(entity.Id);
}
}
private void LocalizationService_DeletedDictionaryItem(ILocalizationService sender, DeleteEventArgs<IDictionaryItem> e)
public void Handle(DictionaryItemDeletedNotification notification)
{
foreach (var entity in e.DeletedEntities)
foreach (IDictionaryItem entity in notification.DeletedEntities)
{
_distributedCache.RemoveDictionaryCache(entity.Id);
}
}
#endregion
@@ -248,23 +244,25 @@ namespace Umbraco.Cms.Core.Cache
/// <summary>
/// Fires when a language is deleted
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LocalizationService_DeletedLanguage(ILocalizationService sender, DeleteEventArgs<ILanguage> e)
/// <param name="notification"></param>
public void Handle(LanguageDeletedNotification notification)
{
foreach (var entity in e.DeletedEntities)
foreach (ILanguage entity in notification.DeletedEntities)
{
_distributedCache.RemoveLanguageCache(entity);
}
}
/// <summary>
/// Fires when a language is saved
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LocalizationService_SavedLanguage(ILocalizationService sender, SaveEventArgs<ILanguage> e)
/// <param name="notification"></param>
public void Handle(LanguageSavedNotification notification)
{
foreach (var entity in e.SavedEntities)
foreach (ILanguage entity in notification.SavedEntities)
{
_distributedCache.RefreshLanguageCache(entity);
}
}
#endregion

View File

@@ -1,6 +1,7 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
@@ -62,6 +63,13 @@ namespace Umbraco.Cms.Core.Compose
.AddNotificationHandler<ContentPublishedNotification, RedirectTrackingHandler>()
.AddNotificationHandler<ContentMovingNotification, RedirectTrackingHandler>()
.AddNotificationHandler<ContentMovedNotification, RedirectTrackingHandler>();
// Add notification handlers for DistributedCache
builder
.AddNotificationHandler<DictionaryItemDeletedNotification, DistributedCacheBinder>()
.AddNotificationHandler<DictionaryItemSavedNotification, DistributedCacheBinder>()
.AddNotificationHandler<LanguageSavedNotification, DistributedCacheBinder>()
.AddNotificationHandler<LanguageDeletedNotification, DistributedCacheBinder>();
}
}
}

View File

@@ -18,7 +18,6 @@ namespace Umbraco.Cms.Core.Services.Implement
{
private readonly IDictionaryRepository _dictionaryRepository;
private readonly ILanguageRepository _languageRepository;
private readonly IEventAggregator _eventAggregator;
private readonly IAuditRepository _auditRepository;
public LocalizationService(
@@ -27,14 +26,12 @@ namespace Umbraco.Cms.Core.Services.Implement
IEventMessagesFactory eventMessagesFactory,
IDictionaryRepository dictionaryRepository,
IAuditRepository auditRepository,
ILanguageRepository languageRepository,
IEventAggregator eventAggregator)
ILanguageRepository languageRepository)
: base(provider, loggerFactory, eventMessagesFactory)
{
_dictionaryRepository = dictionaryRepository;
_auditRepository = auditRepository;
_languageRepository = languageRepository;
_eventAggregator = eventAggregator;
}
/// <summary>
@@ -101,7 +98,7 @@ namespace Umbraco.Cms.Core.Services.Implement
EventMessages eventMessages = EventMessagesFactory.Get();
var savingNotification = new DictionaryItemSavingNotification(item, eventMessages);
if (_eventAggregator.PublishCancelable(savingNotification))
if (scope.Notifications.PublishCancelable(savingNotification))
{
scope.Complete();
return item;
@@ -111,7 +108,7 @@ namespace Umbraco.Cms.Core.Services.Implement
// ensure the lazy Language callback is assigned
EnsureDictionaryItemLanguageCallback(item);
_eventAggregator.Publish(new DictionaryItemSavedNotification(item, eventMessages).WithStateFrom(savingNotification));
scope.Notifications.Publish(new DictionaryItemSavedNotification(item, eventMessages).WithStateFrom(savingNotification));
scope.Complete();
@@ -244,7 +241,7 @@ namespace Umbraco.Cms.Core.Services.Implement
{
EventMessages eventMessages = EventMessagesFactory.Get();
var savingNotification = new DictionaryItemSavingNotification(dictionaryItem, eventMessages);
if (_eventAggregator.PublishCancelable(savingNotification))
if (scope.Notifications.PublishCancelable(savingNotification))
{
scope.Complete();
return;
@@ -256,7 +253,7 @@ namespace Umbraco.Cms.Core.Services.Implement
// ensure the lazy Language callback is assigned
EnsureDictionaryItemLanguageCallback(dictionaryItem);
_eventAggregator.Publish(new DictionaryItemSavedNotification(dictionaryItem, eventMessages).WithStateFrom(savingNotification));
scope.Notifications.Publish(new DictionaryItemSavedNotification(dictionaryItem, eventMessages).WithStateFrom(savingNotification));
Audit(AuditType.Save, "Save DictionaryItem", userId, dictionaryItem.Id, "DictionaryItem");
scope.Complete();
@@ -275,14 +272,14 @@ namespace Umbraco.Cms.Core.Services.Implement
{
EventMessages eventMessages = EventMessagesFactory.Get();
var deletingNotification = new DictionaryItemDeletingNotification(dictionaryItem, eventMessages);
if (_eventAggregator.PublishCancelable(deletingNotification))
if (scope.Notifications.PublishCancelable(deletingNotification))
{
scope.Complete();
return;
}
_dictionaryRepository.Delete(dictionaryItem);
_eventAggregator.Publish(new DictionaryItemDeletedNotification(dictionaryItem, eventMessages).WithStateFrom(deletingNotification));
scope.Notifications.Publish(new DictionaryItemDeletedNotification(dictionaryItem, eventMessages).WithStateFrom(deletingNotification));
Audit(AuditType.Delete, "Delete DictionaryItem", userId, dictionaryItem.Id, "DictionaryItem");
@@ -388,14 +385,14 @@ namespace Umbraco.Cms.Core.Services.Implement
EventMessages eventMessages = EventMessagesFactory.Get();
var savingNotification = new LanguageSavingNotification(language, eventMessages);
if (_eventAggregator.PublishCancelable(savingNotification))
if (scope.Notifications.PublishCancelable(savingNotification))
{
scope.Complete();
return;
}
_languageRepository.Save(language);
_eventAggregator.Publish(new LanguageSavedNotification(language, eventMessages).WithStateFrom(savingNotification));
scope.Notifications.Publish(new LanguageSavedNotification(language, eventMessages).WithStateFrom(savingNotification));
Audit(AuditType.Save, "Save Language", userId, language.Id, ObjectTypes.GetName(UmbracoObjectTypes.Language));
@@ -431,7 +428,7 @@ namespace Umbraco.Cms.Core.Services.Implement
EventMessages eventMessages = EventMessagesFactory.Get();
var deletingLanguageNotification = new LanguageDeletingNotification(language, eventMessages);
if (_eventAggregator.PublishCancelable(deletingLanguageNotification))
if (scope.Notifications.PublishCancelable(deletingLanguageNotification))
{
scope.Complete();
return;
@@ -440,7 +437,7 @@ namespace Umbraco.Cms.Core.Services.Implement
// NOTE: Other than the fall-back language, there aren't any other constraints in the db, so possible references aren't deleted
_languageRepository.Delete(language);
_eventAggregator.Publish(new LanguageDeletedNotification(language, eventMessages).WithStateFrom(deletingLanguageNotification));
scope.Notifications.Publish(new LanguageDeletedNotification(language, eventMessages).WithStateFrom(deletingLanguageNotification));
Audit(AuditType.Delete, "Delete Language", userId, language.Id, ObjectTypes.GetName(UmbracoObjectTypes.Language));
scope.Complete();
@@ -475,47 +472,5 @@ namespace Umbraco.Cms.Core.Services.Implement
return _dictionaryRepository.GetDictionaryItemKeyMap();
}
}
#region Events
/// <summary>
/// Occurs before Delete
/// </summary>
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<ILanguage>> DeletingLanguage;
/// <summary>
/// Occurs after Delete
/// </summary>
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<ILanguage>> DeletedLanguage;
/// <summary>
/// Occurs before Delete
/// </summary>
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<IDictionaryItem>> DeletingDictionaryItem;
/// <summary>
/// Occurs after Delete
/// </summary>
public static event TypedEventHandler<ILocalizationService, DeleteEventArgs<IDictionaryItem>> DeletedDictionaryItem;
/// <summary>
/// Occurs before Save
/// </summary>
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<IDictionaryItem>> SavingDictionaryItem;
/// <summary>
/// Occurs after Save
/// </summary>
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<IDictionaryItem>> SavedDictionaryItem;
/// <summary>
/// Occurs before Save
/// </summary>
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<ILanguage>> SavingLanguage;
/// <summary>
/// Occurs after Save
/// </summary>
public static event TypedEventHandler<ILocalizationService, SaveEventArgs<ILanguage>> SavedLanguage;
#endregion
}
}