From b0293fed8828e3549b42cca5f0c61eb5f0ddd207 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Fri, 4 Oct 2019 11:51:04 +0200 Subject: [PATCH 1/4] Moved getting property alias from attribute to GetModelPropertyTypeAlias --- .../Umbraco/PublishedModelUtility.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs b/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs index 18f04cf8..8d1d8e8c 100644 --- a/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs +++ b/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs @@ -40,7 +40,7 @@ public static IPublishedContentType GetModelContentType(PublishedItemType itemTy } } - public static IPublishedPropertyType GetModelPropertyType(IPublishedContentType contentType, Expression> selector) + public static string GetModelPropertyTypeAlias(Expression> selector) //where TModel : PublishedContentModel // fixme PublishedContentModel _or_ PublishedElementModel { // fixme therefore, missing a check on TModel here @@ -61,7 +61,15 @@ 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; + } + + public static IPublishedPropertyType GetModelPropertyType(IPublishedContentType contentType, Expression> selector) + //where TModel : PublishedContentModel // fixme PublishedContentModel _or_ PublishedElementModel + { + // fixme therefore, missing a check on TModel here + + return contentType.GetPropertyType(GetModelPropertyTypeAlias(selector)); } } } From 183865c381670d588f89a3afebefb8fe4de7ae85 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Fri, 4 Oct 2019 11:52:47 +0200 Subject: [PATCH 2/4] Added GetModelPropertyTypeAlias to builder and fixed tests --- .../BuilderTests.cs | 42 +++++++++++++------ .../SampleGeneratedCode.cs | 2 + .../Building/TextBuilder.cs | 4 ++ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs b/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs index 70f778ad..b65df58a 100644 --- a/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs +++ b/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs @@ -1319,11 +1319,14 @@ 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); + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] + public static string GetModelPropertyTypeAlias(Expression> selector) + => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1377,11 +1380,14 @@ 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); + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] + public static string GetModelPropertyTypeAlias(Expression> selector) + => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1478,11 +1484,14 @@ 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); + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] + public static string GetModelPropertyTypeAlias(Expression> selector) + => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1595,11 +1604,14 @@ 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); + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] + public static string GetModelPropertyTypeAlias(Expression> selector) + => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1709,11 +1721,14 @@ 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); + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] + public static string GetModelPropertyTypeAlias(Expression> selector) + => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // properties @@ -1822,11 +1837,14 @@ 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); + [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] + public static string GetModelPropertyTypeAlias(Expression> selector) + => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // properties diff --git a/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs b/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs index 069cf6cd..20943f02 100644 --- a/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs +++ b/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs @@ -195,6 +195,8 @@ public partial class DebugTextBuilder : PublishedContentModel => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); + public static string GetModelPropertyTypeAlias(Expression> selector) + => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor diff --git a/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs b/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs index 8318c996..23ab8b43 100644 --- a/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs +++ b/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs @@ -209,6 +209,10 @@ private void WriteContentType(StringBuilder sb, TypeModel type) sb.AppendFormat("\t\tpublic static IPublishedPropertyType GetModelPropertyType(Expression> selector)\n", type.ClrName); sb.Append("\t\t\t=> PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector);\n"); + WriteGeneratedCodeAttribute(sb, "\t\t"); + sb.AppendFormat("\t\tpublic static string GetModelPropertyTypeAlias(Expression> selector)\n", + type.ClrName); + sb.Append("\t\t\t=> PublishedModelUtility.GetModelPropertyTypeAlias(selector);\n"); sb.Append("#pragma warning restore 0109\n\n"); // write the ctor From 3419b47f77d9cee8e3733ab12578fd8fa13ed00f Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Fri, 4 Oct 2019 14:42:54 +0200 Subject: [PATCH 3/4] Added overloads without TValue, generic type constraints and documentation to PublishedModelUtility --- .../Umbraco/PublishedModelUtility.cs | 87 +++++++++++++------ 1 file changed, 62 insertions(+), 25 deletions(-) diff --git a/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs b/src/Umbraco.ModelsBuilder/Umbraco/PublishedModelUtility.cs index 8d1d8e8c..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 string GetModelPropertyTypeAlias(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() @@ -64,11 +78,34 @@ public static string GetModelPropertyTypeAlias(Expression(IPublishedContentType contentType, Expression> selector) - //where TModel : PublishedContentModel // fixme PublishedContentModel _or_ PublishedElementModel + /// + /// 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 { - // fixme therefore, missing a check on TModel here + 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)); } } From 2cf54618934bc3b12be819ef89164feccec4b681 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Fri, 4 Oct 2019 14:47:26 +0200 Subject: [PATCH 4/4] Removed GetModelPropertyTypeAlias from builder --- .../BuilderTests.cs | 18 ------------------ .../SampleGeneratedCode.cs | 2 -- .../Building/TextBuilder.cs | 4 ---- 3 files changed, 24 deletions(-) diff --git a/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs b/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs index b65df58a..f28b6738 100644 --- a/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs +++ b/src/Umbraco.ModelsBuilder.Tests/BuilderTests.cs @@ -1324,9 +1324,6 @@ public partial class Type1 : PublishedContentModel, IType1 [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); - [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static string GetModelPropertyTypeAlias(Expression> selector) - => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1385,9 +1382,6 @@ public partial class Type2 : PublishedContentModel, IType1 [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); - [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static string GetModelPropertyTypeAlias(Expression> selector) - => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1489,9 +1483,6 @@ public partial class Type1 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); - [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static string GetModelPropertyTypeAlias(Expression> selector) - => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1609,9 +1600,6 @@ public partial class Type1 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); - [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static string GetModelPropertyTypeAlias(Expression> selector) - => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor @@ -1726,9 +1714,6 @@ public partial class Type1 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); - [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static string GetModelPropertyTypeAlias(Expression> selector) - => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // properties @@ -1842,9 +1827,6 @@ public partial class Type2 : PublishedContentModel [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); - [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""Umbraco.ModelsBuilder"", """ + version + @""")] - public static string GetModelPropertyTypeAlias(Expression> selector) - => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // properties diff --git a/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs b/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs index 20943f02..069cf6cd 100644 --- a/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs +++ b/src/Umbraco.ModelsBuilder.Tests/SampleGeneratedCode.cs @@ -195,8 +195,6 @@ public partial class DebugTextBuilder : PublishedContentModel => PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias); public static IPublishedPropertyType GetModelPropertyType(Expression> selector) => PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector); - public static string GetModelPropertyTypeAlias(Expression> selector) - => PublishedModelUtility.GetModelPropertyTypeAlias(selector); #pragma warning restore 0109 // ctor diff --git a/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs b/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs index 23ab8b43..8318c996 100644 --- a/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs +++ b/src/Umbraco.ModelsBuilder/Building/TextBuilder.cs @@ -209,10 +209,6 @@ private void WriteContentType(StringBuilder sb, TypeModel type) sb.AppendFormat("\t\tpublic static IPublishedPropertyType GetModelPropertyType(Expression> selector)\n", type.ClrName); sb.Append("\t\t\t=> PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector);\n"); - WriteGeneratedCodeAttribute(sb, "\t\t"); - sb.AppendFormat("\t\tpublic static string GetModelPropertyTypeAlias(Expression> selector)\n", - type.ClrName); - sb.Append("\t\t\t=> PublishedModelUtility.GetModelPropertyTypeAlias(selector);\n"); sb.Append("#pragma warning restore 0109\n\n"); // write the ctor