2014-07-24 11:59:37 +02:00
|
|
|
|
using System;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
2014-07-24 11:59:37 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Extensions
|
2014-05-18 21:23:09 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides strongly typed published content models services.
|
|
|
|
|
|
/// </summary>
|
2019-12-10 08:37:19 +01:00
|
|
|
|
public static class PublishedContentExtensionsForModels
|
2014-05-18 21:23:09 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a strongly typed published content model for an internal published content.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The internal published content.</param>
|
|
|
|
|
|
/// <returns>The strongly typed published content model.</returns>
|
2019-12-10 08:37:19 +01:00
|
|
|
|
public static IPublishedContent CreateModel(this IPublishedContent content, IPublishedModelFactory publishedModelFactory)
|
2014-05-18 21:23:09 +02:00
|
|
|
|
{
|
2019-12-10 08:37:19 +01:00
|
|
|
|
if (publishedModelFactory == null) throw new ArgumentNullException(nameof(publishedModelFactory));
|
2014-07-24 11:59:37 +02:00
|
|
|
|
if (content == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
// get model
|
|
|
|
|
|
// if factory returns nothing, throw
|
2019-12-10 08:37:19 +01:00
|
|
|
|
var model = publishedModelFactory.CreateModel(content);
|
2014-07-24 11:59:37 +02:00
|
|
|
|
if (model == null)
|
2019-12-10 08:37:19 +01:00
|
|
|
|
throw new InvalidOperationException("Factory returned null.");
|
2014-07-24 11:59:37 +02:00
|
|
|
|
|
2017-07-21 17:19:00 +02:00
|
|
|
|
// if factory returns a different type, throw
|
2017-09-26 14:57:50 +02:00
|
|
|
|
if (!(model is IPublishedContent publishedContent))
|
2019-12-10 08:37:19 +01:00
|
|
|
|
throw new InvalidOperationException($"Factory returned model of type {model.GetType().FullName} which does not implement IPublishedContent.");
|
2017-07-21 17:19:00 +02:00
|
|
|
|
|
|
|
|
|
|
return publishedContent;
|
2014-05-18 21:23:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|