2014-07-24 11:59:37 +02:00
|
|
|
|
using System;
|
2017-05-30 15:46:25 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2014-07-24 11:59:37 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Models.PublishedContent
|
2014-05-18 21:23:09 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides strongly typed published content models services.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal static class PublishedContentExtensionsForModels
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <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>
|
|
|
|
|
|
public static IPublishedContent CreateModel(this IPublishedContent content)
|
|
|
|
|
|
{
|
2014-07-24 11:59:37 +02:00
|
|
|
|
if (content == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
// get model
|
|
|
|
|
|
// if factory returns nothing, throw
|
2016-08-23 11:06:48 +02:00
|
|
|
|
var model = Current.PublishedContentModelFactory.CreateModel(content);
|
2014-07-24 11:59:37 +02:00
|
|
|
|
if (model == null)
|
2017-07-21 17:19:00 +02:00
|
|
|
|
throw new Exception("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
|
|
|
|
|
|
var publishedContent = model as IPublishedContent;
|
|
|
|
|
|
if (publishedContent == null)
|
|
|
|
|
|
throw new Exception($"Factory returned model of type {model.GetType().FullName} which does not implement IPublishedContent.");
|
|
|
|
|
|
|
|
|
|
|
|
return publishedContent;
|
2014-05-18 21:23:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|