Merge pull request #7983 from umbraco/v8/bugfix/models-builder-enable-flag

ModelsBuilder enabled flag not respected
This commit is contained in:
Bjarke Berg
2020-04-29 07:37:33 +02:00
committed by GitHub
5 changed files with 56 additions and 13 deletions

View File

@@ -1,5 +1,21 @@
namespace Umbraco.Core.Models.PublishedContent
{
/// <summary>
/// Provides a live published model creation service.
/// </summary>
public interface ILivePublishedModelFactory2 : ILivePublishedModelFactory
{
/// <summary>
/// Tells the factory that it should build a new generation of models
/// </summary>
void Reset();
/// <summary>
/// If the live model factory
/// </summary>
bool Enabled { get; }
}
/// <summary>
/// Provides a live published model creation service.
/// </summary>

View File

@@ -17,6 +17,20 @@ namespace Umbraco.Core
/// <returns></returns>
public static bool IsLiveFactory(this IPublishedModelFactory factory) => factory is ILivePublishedModelFactory;
/// <summary>
/// Returns true if the current <see cref="IPublishedModelFactory"/> is an implementation of <see cref="ILivePublishedModelFactory2"/> and is enabled
/// </summary>
/// <param name="factory"></param>
/// <returns></returns>
public static bool IsLiveFactoryEnabled(this IPublishedModelFactory factory)
{
if (factory is ILivePublishedModelFactory2 liveFactory2)
return liveFactory2.Enabled;
// if it's not ILivePublishedModelFactory2 we can't determine if it's enabled or not so return true
return factory is ILivePublishedModelFactory;
}
[Obsolete("This method is no longer used or necessary and will be removed from future")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static void WithSafeLiveFactory(this IPublishedModelFactory factory, Action action)
@@ -50,15 +64,17 @@ namespace Umbraco.Core
{
lock (liveFactory.SyncRoot)
{
// TODO: Fix this in 8.3! - We need to change the ILivePublishedModelFactory interface to have a Reset method and then when we have an embedded MB
// version we will publicize the ResetModels (and change the name to Reset).
// For now, this will suffice and we'll use reflection, there should be no other implementation of ILivePublishedModelFactory.
// Calling ResetModels resets the MB flag so that the next time EnsureModels is called (which is called when nucache lazily calls CreateModel) it will
// trigger the recompiling of pure live models.
var resetMethod = liveFactory.GetType().GetMethod("ResetModels", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (resetMethod != null)
resetMethod.Invoke(liveFactory, null);
if (liveFactory is ILivePublishedModelFactory2 liveFactory2)
{
liveFactory2.Reset();
}
else
{
// This is purely here for backwards compat and to avoid breaking changes but this code will probably never get executed
var resetMethod = liveFactory.GetType().GetMethod("ResetModels", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (resetMethod != null)
resetMethod.Invoke(liveFactory, null);
}
action();
}
}

View File

@@ -21,7 +21,7 @@ using File = System.IO.File;
namespace Umbraco.ModelsBuilder.Embedded
{
internal class PureLiveModelFactory : ILivePublishedModelFactory, IRegisteredObject
internal class PureLiveModelFactory : ILivePublishedModelFactory2, IRegisteredObject
{
private Assembly _modelsAssembly;
private Infos _infos = new Infos { ModelInfos = null, ModelTypeMap = new Dictionary<string, Type>() };
@@ -134,6 +134,16 @@ namespace Umbraco.ModelsBuilder.Embedded
return ctor();
}
/// <inheritdoc />
public bool Enabled => _config.Enable;
/// <inheritdoc />
public void Reset()
{
if (_config.Enable)
ResetModels();
}
#endregion
#region Compilation

View File

@@ -859,7 +859,7 @@ namespace Umbraco.Web.PublishedCache.NuCache
Notify<IContentType>(_contentStore, payloads, RefreshContentTypesLocked);
Notify<IMediaType>(_mediaStore, payloads, RefreshMediaTypesLocked);
if (_publishedModelFactory.IsLiveFactory())
if (_publishedModelFactory.IsLiveFactoryEnabled())
{
//In the case of Pure Live - we actually need to refresh all of the content and the media
//see https://github.com/umbraco/Umbraco-CMS/issues/5671

View File

@@ -1,11 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web.Cache;
namespace Umbraco.Web.PublishedCache
{
abstract class PublishedSnapshotServiceBase : IPublishedSnapshotService
internal abstract class PublishedSnapshotServiceBase : IPublishedSnapshotService
{
protected PublishedSnapshotServiceBase(IPublishedSnapshotAccessor publishedSnapshotAccessor, IVariationContextAccessor variationContextAccessor)
{