using System; using Umbraco.Core.Composing; namespace Umbraco.Core.Models.PublishedContent { /// /// Provides strongly typed published content models services. /// internal static class PublishedContentExtensionsForModels { /// /// Creates a strongly typed published content model for an internal published content. /// /// The internal published content. /// The strongly typed published content model. public static IPublishedContent CreateModel(this IPublishedContent content) { if (content == null) return null; // in order to provide a nice, "fluent" experience, this extension method // needs to access Current, which is not always initialized in tests - not // very elegant, but works if (!Current.HasContainer) return content; // get model // if factory returns nothing, throw var model = Current.PublishedModelFactory.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; } } }