2019-02-22 19:13:25 +01:00
using System ;
2019-09-16 11:47:05 +10:00
using System.Collections.Generic ;
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-02-22 19:13:25 +01:00
/// <summary>
2019-07-03 15:17:51 +10:00
/// Executes an action with a safe live factory
2019-02-22 19:13:25 +01:00
/// </summary>
/// <remarks>
/// <para>If the factory is a live factory, ensures it is refreshed and locked while executing the action.</para>
/// </remarks>
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>
/// Creates a strongly typed model while checking if the factory is <see cref="ILivePublishedModelFactory"/> and if a refresh flag has been set, in which
/// case the models will be recompiled before model creation
/// </summary>
/// <param name="factory"></param>
/// <param name="content"></param>
/// <returns></returns>
internal static IPublishedContent CreateModelWithSafeLiveFactoryRefreshCheck ( this IPublishedModelFactory factory , IPublishedContent content )
{
if ( factory is ILivePublishedModelFactory liveFactory & & _refresh )
{
lock ( liveFactory . SyncRoot )
{
if ( _refresh )
{
_refresh = false ;
//Call refresh on the live factory to re-compile the models
liveFactory . Refresh ( ) ;
}
}
}
var model = factory . CreateModel ( content ) ;
if ( model = = null )
throw new Exception ( "Factory returned null." ) ;
// if factory returns a different type, throw
if ( ! ( model is IPublishedContent publishedContent ) )
throw new Exception ( $"Factory returned model of type {model.GetType().FullName} which does not implement IPublishedContent." ) ;
return publishedContent ;
}
/// <summary>
/// Sets a flag to re-compile the models if the <see cref="IPublishedModelFactory"/> is <see cref="ILivePublishedModelFactory"/>
/// </summary>
/// <param name="factory"></param>
/// <param name="action"></param>
internal static void WithSafeLiveFactoryRefreshSet ( this IPublishedModelFactory factory , Action action )
{
if ( factory is ILivePublishedModelFactory liveFactory )
{
lock ( liveFactory . SyncRoot )
{
_refresh = true ;
action ( ) ;
}
}
else
{
action ( ) ;
}
}
private static volatile bool _refresh = false ;
2019-02-22 19:13:25 +01:00
}
}