makes MB event an INotification

This commit is contained in:
Shannon
2021-02-01 15:37:41 +11:00
parent 9b01f24b1c
commit dd90193365
7 changed files with 78 additions and 69 deletions

View File

@@ -12,6 +12,7 @@ using Umbraco.Core.DependencyInjection;
using Umbraco.Core.Events;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.ModelsBuilder.Embedded.Building;
using Umbraco.Web.Common.ModelBinders;
using Umbraco.Web.WebAssets;
/*
@@ -91,6 +92,7 @@ namespace Umbraco.ModelsBuilder.Embedded.DependencyInjection
// would automatically just register for all implemented INotificationHandler{T}?
builder.AddNotificationHandler<UmbracoApplicationStarting, ModelsBuilderNotificationHandler>();
builder.AddNotificationHandler<ServerVariablesParsing, ModelsBuilderNotificationHandler>();
builder.AddNotificationHandler<ModelBindingError, ModelsBuilderNotificationHandler>();
builder.AddNotificationHandler<UmbracoApplicationStarting, LiveModelsProvider>();
builder.AddNotificationHandler<UmbracoRequestEnd, LiveModelsProvider>();
builder.AddNotificationHandler<UmbracoApplicationStarting, OutOfDateModelsStatus>();

View File

@@ -22,24 +22,21 @@ namespace Umbraco.ModelsBuilder.Embedded
/// <summary>
/// Handles <see cref="UmbracoApplicationStarting"/> and <see cref="ServerVariablesParsing"/> notifications to initialize MB
/// </summary>
internal class ModelsBuilderNotificationHandler : INotificationHandler<UmbracoApplicationStarting>, INotificationHandler<ServerVariablesParsing>
internal class ModelsBuilderNotificationHandler : INotificationHandler<UmbracoApplicationStarting>, INotificationHandler<ServerVariablesParsing>, INotificationHandler<ModelBindingError>
{
private readonly ModelsBuilderSettings _config;
private readonly IShortStringHelper _shortStringHelper;
private readonly LinkGenerator _linkGenerator;
private readonly ContentModelBinder _modelBinder;
public ModelsBuilderNotificationHandler(
IOptions<ModelsBuilderSettings> config,
IShortStringHelper shortStringHelper,
LinkGenerator linkGenerator,
ContentModelBinder modelBinder)
LinkGenerator linkGenerator)
{
_config = config.Value;
_shortStringHelper = shortStringHelper;
_shortStringHelper = shortStringHelper;
_linkGenerator = linkGenerator;
_modelBinder = modelBinder;
}
/// <summary>
@@ -49,8 +46,6 @@ namespace Umbraco.ModelsBuilder.Embedded
{
// always setup the dashboard
// note: UmbracoApiController instances are automatically registered
_modelBinder.ModelBindingException += ContentModelBinder_ModelBindingException;
if (_config.ModelsMode != ModelsMode.Nothing)
{
FileService.SavingTemplate += FileService_SavingTemplate;
@@ -149,10 +144,13 @@ namespace Umbraco.ModelsBuilder.Embedded
}
}
private void ContentModelBinder_ModelBindingException(object sender, ContentModelBinder.ModelBindingArgs args)
/// <summary>
/// Handles when a model binding error occurs
/// </summary>
public void Handle(ModelBindingError notification)
{
ModelsBuilderAssemblyAttribute sourceAttr = args.SourceType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
ModelsBuilderAssemblyAttribute modelAttr = args.ModelType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
ModelsBuilderAssemblyAttribute sourceAttr = notification.SourceType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
ModelsBuilderAssemblyAttribute modelAttr = notification.ModelType.Assembly.GetCustomAttribute<ModelsBuilderAssemblyAttribute>();
// if source or model is not a ModelsBuider type...
if (sourceAttr == null || modelAttr == null)
@@ -164,11 +162,11 @@ namespace Umbraco.ModelsBuilder.Embedded
}
// else report, but better not restart (loops?)
args.Message.Append(" The ");
args.Message.Append(sourceAttr == null ? "view model" : "source");
args.Message.Append(" is a ModelsBuilder type, but the ");
args.Message.Append(sourceAttr != null ? "view model" : "source");
args.Message.Append(" is not. The application is in an unstable state and should be restarted.");
notification.Message.Append(" The ");
notification.Message.Append(sourceAttr == null ? "view model" : "source");
notification.Message.Append(" is a ModelsBuilder type, but the ");
notification.Message.Append(sourceAttr != null ? "view model" : "source");
notification.Message.Append(" is not. The application is in an unstable state and should be restarted.");
return;
}
@@ -181,22 +179,22 @@ namespace Umbraco.ModelsBuilder.Embedded
if (pureSource == false || pureModel == false)
{
// only one is pure - report, but better not restart (loops?)
args.Message.Append(pureSource
notification.Message.Append(pureSource
? " The content model is PureLive, but the view model is not."
: " The view model is PureLive, but the content model is not.");
args.Message.Append(" The application is in an unstable state and should be restarted.");
notification.Message.Append(" The application is in an unstable state and should be restarted.");
}
else
{
// both are pure - report, and if different versions, restart
// if same version... makes no sense... and better not restart (loops?)
var sourceVersion = args.SourceType.Assembly.GetName().Version;
var modelVersion = args.ModelType.Assembly.GetName().Version;
args.Message.Append(" Both view and content models are PureLive, with ");
args.Message.Append(sourceVersion == modelVersion
var sourceVersion = notification.SourceType.Assembly.GetName().Version;
var modelVersion = notification.ModelType.Assembly.GetName().Version;
notification.Message.Append(" Both view and content models are PureLive, with ");
notification.Message.Append(sourceVersion == modelVersion
? "same version. The application is in an unstable state and should be restarted."
: "different versions. The application is in an unstable state and is going to be restarted.");
args.Restart = sourceVersion != modelVersion;
notification.Restart = sourceVersion != modelVersion;
}
}
}