Files
Umbraco-CMS/src/Umbraco.Core/Models/PublishedContent/PublishedContentExtensionsForModels.cs

36 lines
1.4 KiB
C#
Raw Normal View History

2014-07-24 11:59:37 +02:00
using System;
using Umbraco.Cms.Core.Models.PublishedContent;
2014-07-24 11:59:37 +02:00
namespace Umbraco.Extensions
2014-05-18 21:23:09 +02:00
{
/// <summary>
/// Provides strongly typed published content models services.
/// </summary>
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>
public static IPublishedContent CreateModel(this IPublishedContent content, IPublishedModelFactory publishedModelFactory)
2014-05-18 21:23:09 +02: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
var model = publishedModelFactory.CreateModel(content);
2014-07-24 11:59:37 +02:00
if (model == null)
throw new InvalidOperationException("Factory returned null.");
2014-07-24 11:59:37 +02:00
// if factory returns a different type, throw
2017-09-26 14:57:50 +02:00
if (!(model is IPublishedContent publishedContent))
throw new InvalidOperationException($"Factory returned model of type {model.GetType().FullName} which does not implement IPublishedContent.");
return publishedContent;
2014-05-18 21:23:09 +02:00
}
}
}