diff --git a/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs b/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs index 70f778ad..f28b6738 100644 --- a/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs +++ b/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs @@ -1319,10 +1319,10 @@ public partial class Type1 : PublishedContentModel, IType1 [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public new const PublishedItemType ModelItemType = PublishedItemType.Content; [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public new static PublishedContentType GetModelContentType() + public new static IPublishedContentType GetModelContentType() => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static PublishedPropertyType GetModelPropertyType(Expression> selector) + public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); #pragma warning restore 0109 @@ -1377,10 +1377,10 @@ public partial class Type2 : PublishedContentModel, IType1 [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public new const PublishedItemType ModelItemType = PublishedItemType.Content; [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public new static PublishedContentType GetModelContentType() + public new static IPublishedContentType GetModelContentType() => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static PublishedPropertyType GetModelPropertyType(Expression> selector) + public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); #pragma warning restore 0109 @@ -1478,10 +1478,10 @@ public partial class Type1 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public new const PublishedItemType ModelItemType = PublishedItemType.Content; [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public new static PublishedContentType GetModelContentType() + public new static IPublishedContentType GetModelContentType() => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static PublishedPropertyType GetModelPropertyType(Expression> selector) + public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); #pragma warning restore 0109 @@ -1595,10 +1595,10 @@ public partial class Type1 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public new const PublishedItemType ModelItemType = PublishedItemType.Content; [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public new static PublishedContentType GetModelContentType() + public new static IPublishedContentType GetModelContentType() => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static PublishedPropertyType GetModelPropertyType(Expression> selector) + public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); #pragma warning restore 0109 @@ -1709,10 +1709,10 @@ public partial class Type1 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public new const PublishedItemType ModelItemType = PublishedItemType.Content; [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public new static PublishedContentType GetModelContentType() + public new static IPublishedContentType GetModelContentType() => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static PublishedPropertyType GetModelPropertyType(Expression> selector) + public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); #pragma warning restore 0109 @@ -1822,10 +1822,10 @@ public partial class Type2 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public new const PublishedItemType ModelItemType = PublishedItemType.Content; [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public new static PublishedContentType GetModelContentType() + public new static IPublishedContentType GetModelContentType() => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static PublishedPropertyType GetModelPropertyType(Expression> selector) + public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); #pragma warning restore 0109 diff --git a/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs b/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs index 18f04cf8..185bf800 100644 --- a/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs +++ b/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs @@ -6,24 +6,20 @@ namespace Umbraco.ModelsBuilder.Umbraco { + /// + /// Utility class for published models. + /// public static class PublishedModelUtility { - // looks safer but probably useless... ppl should not call these methods directly - // and if they do... they have to take care about not doing stupid things - - //public static PublishedPropertyType GetModelPropertyType2(Expression> selector) - // where T : PublishedContentModel - //{ - // var type = typeof (T); - // var s1 = type.GetField("ModelTypeAlias", BindingFlags.Public | BindingFlags.Static); - // var alias = (s1.IsLiteral && s1.IsInitOnly && s1.FieldType == typeof(string)) ? (string)s1.GetValue(null) : null; - // var s2 = type.GetField("ModelItemType", BindingFlags.Public | BindingFlags.Static); - // var itemType = (s2.IsLiteral && s2.IsInitOnly && s2.FieldType == typeof(PublishedItemType)) ? (PublishedItemType)s2.GetValue(null) : 0; - - // var contentType = PublishedContentType.Get(itemType, alias); - // // etc... - //} - + /// + /// Gets the published content type for the specified model . + /// + /// The published item type. + /// The model alias. + /// + /// The published content type. + /// + /// itemType public static IPublishedContentType GetModelContentType(PublishedItemType itemType, string alias) { var facade = Current.UmbracoContext.PublishedSnapshot; // fixme inject! @@ -40,19 +36,37 @@ public static IPublishedContentType GetModelContentType(PublishedItemType itemTy } } - public static IPublishedPropertyType GetModelPropertyType(IPublishedContentType contentType, Expression> selector) - //where TModel : PublishedContentModel // fixme PublishedContentModel _or_ PublishedElementModel + /// + /// Gets the property type alias for the specified . + /// + /// The type of the model. + /// The selector. + /// + /// The property type alias. + /// + public static string GetModelPropertyTypeAlias(Expression> selector) + where TModel : IPublishedElement { - // fixme therefore, missing a check on TModel here + return GetModelPropertyTypeAlias(selector); + } + /// + /// Gets the property type alias for the specified . + /// + /// The type of the model. + /// The type of the value. + /// The selector. + /// + /// The property type alias. + /// + public static string GetModelPropertyTypeAlias(Expression> selector) + where TModel : IPublishedElement + { var expr = selector.Body as MemberExpression; if (expr == null) throw new ArgumentException("Not a property expression.", nameof(selector)); - // there _is_ a risk that contentType and T do not match - // see note above : accepted risk... - var attr = expr.Member .GetCustomAttributes(typeof (ImplementPropertyTypeAttribute), false) .OfType() @@ -61,7 +75,38 @@ public static IPublishedPropertyType GetModelPropertyType(IPubli if (string.IsNullOrWhiteSpace(attr?.Alias)) throw new InvalidOperationException($"Could not figure out property alias for property \"{expr.Member.Name}\"."); - return contentType.GetPropertyType(attr.Alias); + return attr.Alias; + } + + /// + /// Gets the published property type on the for the specified . + /// + /// The type of the model. + /// The published content type. + /// The selector. + /// + /// The published property type. + /// + public static IPublishedPropertyType GetModelPropertyType(IPublishedContentType contentType, Expression> selector) + where TModel : IPublishedElement + { + return GetModelPropertyType(contentType, selector); + } + + /// + /// Gets the published property type on the for the specified . + /// + /// The type of the model. + /// The type of the value. + /// The published content type. + /// The selector. + /// + /// The published property type. + /// + public static IPublishedPropertyType GetModelPropertyType(IPublishedContentType contentType, Expression> selector) + where TModel : IPublishedElement + { + return contentType.GetPropertyType(GetModelPropertyTypeAlias(selector)); } } }