|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq.Expressions; |
| 4 | +using Umbraco.Core; |
| 5 | +using Umbraco.Core.Models; |
| 6 | + |
| 7 | +namespace Umbraco.CodeGen.WaitForSixTwo |
| 8 | +{ |
| 9 | + public class PublishedContentFactoryHook : IApplicationEventHandler |
| 10 | + { |
| 11 | + public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) |
| 12 | + { |
| 13 | + Core.Models.PublishedContent.PublishedContentModelFactoryResolver.Current.SetFactory(new PublishedContentModelFactoryImpl()); |
| 14 | + } |
| 15 | + |
| 16 | + public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) |
| 17 | + { |
| 18 | + |
| 19 | + } |
| 20 | + |
| 21 | + public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) |
| 22 | + { |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + internal class PublishedContentModelFactoryImpl : Core.Models.PublishedContent.IPublishedContentModelFactory |
| 27 | + { |
| 28 | + //private readonly Dictionary<string, ConstructorInfo> _constructors |
| 29 | + // = new Dictionary<string, ConstructorInfo>(); |
| 30 | + |
| 31 | + private readonly Dictionary<string, Func<IPublishedContent, IPublishedContent>> _constructors |
| 32 | + = new Dictionary<string, Func<IPublishedContent, IPublishedContent>>(); |
| 33 | + |
| 34 | + public PublishedContentModelFactoryImpl() |
| 35 | + { |
| 36 | + var types = PluginManager.Current.ResolveTypes<PublishedContentModel>(); |
| 37 | + var ctorArgTypes = new[] { typeof(IPublishedContent) }; |
| 38 | + |
| 39 | + foreach (var type in types) |
| 40 | + { |
| 41 | + var constructor = type.GetConstructor(ctorArgTypes); |
| 42 | + if (constructor == null) |
| 43 | + throw new InvalidOperationException(string.Format("Type {0} is missing a public constructor with one argument of type IPublishedContent.", type.FullName)); |
| 44 | + var attribute = type.GetCustomAttribute<Core.Models.PublishedContent.PublishedContentModelAttribute>(false); |
| 45 | + var typeName = attribute == null ? type.Name : attribute.ContentTypeAlias; |
| 46 | + typeName = typeName.ToLowerInvariant(); |
| 47 | + |
| 48 | + if (_constructors.ContainsKey(typeName)) |
| 49 | + throw new InvalidOperationException(string.Format("More that one type want to be a model for content type {0}.", typeName)); |
| 50 | + |
| 51 | + // should work everywhere, but slow |
| 52 | + //_constructors[typeName] = constructor; |
| 53 | + |
| 54 | + // much faster with a dynamic method but potential MediumTrust issues |
| 55 | + // here http://stackoverflow.com/questions/16363838/how-do-you-call-a-constructor-via-an-expression-tree-on-an-existing-object |
| 56 | + |
| 57 | + // fast enough and works in MediumTrust |
| 58 | + // read http://boxbinary.com/2011/10/how-to-run-a-unit-test-in-medium-trust-with-nunitpart-three-umbraco-framework-testing/ |
| 59 | + var exprArg = Expression.Parameter(typeof(IPublishedContent), "content"); |
| 60 | + var exprNew = Expression.New(constructor, exprArg); |
| 61 | + var expr = Expression.Lambda<Func<IPublishedContent, IPublishedContent>>(exprNew, exprArg); |
| 62 | + var func = expr.Compile(); |
| 63 | + _constructors[typeName] = func; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public IPublishedContent CreateModel(IPublishedContent content) |
| 68 | + { |
| 69 | + // be case-insensitive |
| 70 | + var contentTypeAlias = content.DocumentTypeAlias.ToLowerInvariant(); |
| 71 | + |
| 72 | + //ConstructorInfo constructor; |
| 73 | + //return _constructors.TryGetValue(contentTypeAlias, out constructor) |
| 74 | + // ? (IPublishedContent) constructor.Invoke(new object[] { content }) |
| 75 | + // : content; |
| 76 | + |
| 77 | + Func<IPublishedContent, IPublishedContent> constructor; |
| 78 | + return _constructors.TryGetValue(contentTypeAlias, out constructor) |
| 79 | + ? constructor(content) |
| 80 | + : content; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public abstract class PublishedContentModel : Umbraco.Core.Models.PublishedContent.PublishedContentExtended |
| 85 | + { |
| 86 | + /// <summary> |
| 87 | + /// Initializes a new instance of the <see cref="PublishedContentModel"/> class with |
| 88 | + /// an original <see cref="IPublishedContent"/> instance. |
| 89 | + /// </summary> |
| 90 | + /// <param name="content">The original content.</param> |
| 91 | + protected PublishedContentModel(IPublishedContent content) |
| 92 | + : base(content) |
| 93 | + { } |
| 94 | + } |
| 95 | +} |
0 commit comments