using System;
using System.Collections.Generic;
using System.ComponentModel;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core
{
///
/// Provides extension methods for .
///
public static class PublishedModelFactoryExtensions
{
///
/// Returns true if the current is an implementation of
///
///
///
public static bool IsLiveFactory(this IPublishedModelFactory factory) => 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)
{
if (factory is ILivePublishedModelFactory liveFactory)
{
lock (liveFactory.SyncRoot)
{
//Call refresh on the live factory to re-compile the models
liveFactory.Refresh();
action();
}
}
else
{
action();
}
}
///
/// Sets a flag to reset the ModelsBuilder models if the is
///
///
///
///
/// This does not recompile the pure live models, only sets a flag to tell models builder to recompile when they are requested.
///
internal static void WithSafeLiveFactoryReset(this IPublishedModelFactory factory, Action action)
{
if (factory is ILivePublishedModelFactory liveFactory)
{
lock (liveFactory.SyncRoot)
{
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();
}
}
else
{
action();
}
}
}
}