2019-02-22 19:13:25 +01:00
|
|
|
|
using System;
|
2019-09-16 11:47:05 +10:00
|
|
|
|
using System.Collections.Generic;
|
2019-09-16 17:44:50 +10:00
|
|
|
|
using System.ComponentModel;
|
2019-02-22 19:13:25 +01:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides extension methods for <see cref="IPublishedModelFactory"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class PublishedModelFactoryExtensions
|
|
|
|
|
|
{
|
2019-07-03 18:11:00 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the current <see cref="IPublishedModelFactory"/> is an implementation of <see cref="ILivePublishedModelFactory"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="factory"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool IsLiveFactory(this IPublishedModelFactory factory) => factory is ILivePublishedModelFactory;
|
|
|
|
|
|
|
2019-09-16 17:44:50 +10:00
|
|
|
|
[Obsolete("This method is no longer used or necessary and will be removed from future")]
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
2019-02-22 19:13:25 +01:00
|
|
|
|
public static void WithSafeLiveFactory(this IPublishedModelFactory factory, Action action)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (factory is ILivePublishedModelFactory liveFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (liveFactory.SyncRoot)
|
|
|
|
|
|
{
|
2019-09-16 16:59:57 +10:00
|
|
|
|
//Call refresh on the live factory to re-compile the models
|
|
|
|
|
|
liveFactory.Refresh();
|
|
|
|
|
|
action();
|
2019-02-22 19:13:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
action();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-09-16 11:47:05 +10:00
|
|
|
|
|
2019-09-16 14:50:05 +10:00
|
|
|
|
/// <summary>
|
2019-09-16 17:23:00 +10:00
|
|
|
|
/// Sets a flag to reset the ModelsBuilder models if the <see cref="IPublishedModelFactory"/> is <see cref="ILivePublishedModelFactory"/>
|
2019-09-16 14:50:05 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="factory"></param>
|
|
|
|
|
|
/// <param name="action"></param>
|
2019-09-16 17:23:00 +10:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This does not recompile the pure live models, only sets a flag to tell models builder to recompile when they are requested.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
internal static void WithSafeLiveFactoryReset(this IPublishedModelFactory factory, Action action)
|
2019-09-16 14:50:05 +10:00
|
|
|
|
{
|
|
|
|
|
|
if (factory is ILivePublishedModelFactory liveFactory)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (liveFactory.SyncRoot)
|
|
|
|
|
|
{
|
2020-04-20 22:25:05 +10:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2019-09-16 17:23:00 +10:00
|
|
|
|
|
2019-09-16 14:50:05 +10:00
|
|
|
|
action();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
action();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-22 19:13:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|