From d45ea7e7d38438826f7831e6f1ab5a3c65d0758a Mon Sep 17 00:00:00 2001 From: Jack Spektor Date: Tue, 8 Mar 2016 17:14:00 +1300 Subject: [PATCH] Add custom fields for template inheritance Hi. We did some change to T4 template which could be useful. The change introduces custom properties to the template items, so they could be explicitly told to inherit specifc class/interface. This could be useful if there are TDS templates which inherits other TDS templates which are not part of the project. --- .../Code Generation Templates/GlassV3Item.tt | 346 ++++++++---------- 1 file changed, 161 insertions(+), 185 deletions(-) diff --git a/Sitecore.Master/Code Generation Templates/GlassV3Item.tt b/Sitecore.Master/Code Generation Templates/GlassV3Item.tt index 3f13d11..81b9231 100644 --- a/Sitecore.Master/Code Generation Templates/GlassV3Item.tt +++ b/Sitecore.Master/Code Generation Templates/GlassV3Item.tt @@ -1,129 +1,117 @@ <#@ template language="C#" #> <#@ output encoding="utf-8"#> - <#@ include file="Helpers.tt" #> <#@ include file="StringExtensions.tt" #> <#@ include file="GeneralExtensions.tt" #> <#@ include file="Inflector.tt" #> - <#@ assembly name="System.Core.dll" #> - <#@ import namespace="System" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models" #> - <#@ parameter name="Model" type="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models.SitecoreItem" #> <#@ parameter name="DefaultNamespace" type="System.String" #> - <# /* This TDS Code Generation template is used to generate objects that are compatible with the Glass Sitecore Mapper that is available @ http://www.glass.lu/ - -There are a few things you can put in the 'Custom Data' property of a field in TDS. +There are a few things you can put in the 'Custom Data' property of a field in TDS. To use multiple settings put them in as a querystring (key1=value&key2=value) - ignore=true - Sets a field to be skipped over for code gen +Sets a field to be skipped over for code gen name=[name] - Forces the name of the generated property. - If not specified, then the generated property is the name of the Sitecore field. - If the field stores multiple values, the property name is pluralized. +Forces the name of the generated property. +If not specified, then the generated property is the name of the Sitecore field. +If the field stores multiple values, the property name is pluralized. type=[type] - Sets the return type of the generated property +Sets the return type of the generated property generic=[type] - In the event the type (either specificed or auto mapped) is a generic it will use this generic type. i.e. List - +In the event the type (either specificed or auto mapped) is a generic it will use this generic type. i.e. List + +Custom properties for template items: +inherits=[type] +Inherits the generated template model from the custom type +interface=[type] +Inherits the generated template interface from the custom interface + */ #> - <# // we only act on Templates SitecoreTemplate template = Model as SitecoreTemplate; if (template == null) { - return ""; +return ""; } - string Tool = "Team Development for Sitecore - GlassItem.tt"; string ToolVersion = "1.0"; +string TemplateParentInterface = GetCustomProperty(template.Data, "interface"); +string ParentInterface = !string.IsNullOrEmpty(TemplateParentInterface) ? TemplateParentInterface : "IGlassBase"; +Func getInterfaceName = (string s) => AsInterfaceName(!string.IsNullOrEmpty(TemplateParentInterface) ? TemplateParentInterface : s); #> - namespace <#= GetNamespace(DefaultNamespace, template)#> { - - - /// - /// <#= AsInterfaceName(template.Name) #> Interface - /// <#= GetValue(template.SitecoreFields, "__Short description")#> - /// Path: <#= template.Path #> - /// ID: <#= template.ID.ToString() #> - /// - [SitecoreType(TemplateId=<#= AsInterfaceName(template.Name) #>Constants.TemplateIdString )] //, Cachable = true - public partial interface <#= AsInterfaceName(template.Name) #> : IGlassBase <#=GetObjectInheritanceDefinition(DefaultNamespace, template, true, (string s) => AsInterfaceName(s))#> - { - <#foreach(SitecoreField field in GetFieldsForTemplate(template, false)){#> - /// - /// The <#=field.Name#> field. - /// <#= GetValue(field.SitecoreFields, "__Short description")#> - /// Field Type: <#=field.Type#> - /// Field ID: <#=field.ID.ToString()#> - /// Custom Data: <#=field.Data#> - /// - [SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#= GetPropertyName(field) #>FieldName)] - <#=GetGlassFieldType(field)#> <#= GetPropertyName(field) #> {get; set;} - - <#}#> - } - - - public static partial class <#= AsInterfaceName(template.Name) #>Constants{ - - public const string TemplateIdString = "<#= template.ID.ToString() #>"; - public static readonly ID TemplateId = new ID(TemplateIdString); - public const string TemplateName = "<#= template.Name #>"; - - <#foreach(SitecoreField field in GetFieldsForTemplate(template, true)){#> - - public static readonly ID <#= GetPropertyName(field) #>FieldId = new ID("<#=field.ID.ToString()#>"); - public const string <#= GetPropertyName(field) #>FieldName = "<#=field.Name#>"; - - <#}#> - - } - +/// +/// <#= AsInterfaceName(template.Name) #> Interface +/// <#= GetValue(template.SitecoreFields, "__Short description")#> +/// Path: <#= template.Path #> +/// ID: <#= template.ID.ToString() #> +/// +[SitecoreType(TemplateId=<#= AsInterfaceName(template.Name) #>Constants.TemplateIdString )] //, Cachable = true +public partial interface <#= AsInterfaceName(template.Name) #> : <#= ParentInterface #> <#=GetObjectInheritanceDefinition(DefaultNamespace, template, true, getInterfaceName)#> +{ +<#foreach(SitecoreField field in GetFieldsForTemplate(template, false)){#> +/// +/// The <#=field.Name#> field. +/// <#= GetValue(field.SitecoreFields, "__Short description")#> +/// Field Type: <#=field.Type#> +/// Field ID: <#=field.ID.ToString()#> +/// Custom Data: <#=field.Data#> +/// +[SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#= GetPropertyName(field) #>FieldName)] +<#=GetGlassFieldType(field)#> <#= GetPropertyName(field) #> {get; set;} +<#}#> +} +public static partial class <#= AsInterfaceName(template.Name) #>Constants{ +public const string TemplateIdString = "<#= template.ID.ToString() #>"; +public static readonly ID TemplateId = new ID(TemplateIdString); +public const string TemplateName = "<#= template.Name #>"; +<#foreach(SitecoreField field in GetFieldsForTemplate(template, true)){#> +public static readonly ID <#= GetPropertyName(field) #>FieldId = new ID("<#=field.ID.ToString()#>"); +public const string <#= GetPropertyName(field) #>FieldName = "<#=field.Name#>"; +<#}#> +} <# // If the name of the template looks like an Interface, then don't generate a class definition -if (!IsInterfaceWord(template.Name)){ #> - /// - /// <#= AsClassName(template.Name) #> - /// <#= GetValue(template.SitecoreFields, "__Short description")#> - /// Path: <#= template.Path #> - /// ID: <#= template.ID.ToString() #> - /// - [SitecoreType(TemplateId=<#= AsInterfaceName(template.Name) #>Constants.TemplateIdString)] //, Cachable = true - public partial class <#= AsClassName(template.Name) #> : GlassBase, <#=AsInterfaceName(template.Name)#> - { - - <#foreach(SitecoreField field in GetFieldsForTemplate(template, true)){#> - /// - /// The <#=field.Name#> field. - /// <#= GetValue(field.SitecoreFields, "__Short description")#> - /// Field Type: <#=field.Type#> - /// Field ID: <#=field.ID.ToString()#> - /// Custom Data: <#=field.Data#> - /// - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("<#=Tool#>", "<#=ToolVersion#>")] - [SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#= GetPropertyName(field) #>FieldName)] - public virtual <#=GetGlassFieldType(field)#> <#= GetPropertyName(field) #> {get; set;} - - <#}#> - } +if (!IsInterfaceWord(template.Name)){ +string TemplateParentClass = GetCustomProperty(template.Data, "inherits"); +string className = !string.IsNullOrEmpty(TemplateParentClass) ? TemplateParentClass : "GlassBase"; +#> +/// +/// <#= AsClassName(template.Name) #> +/// <#= GetValue(template.SitecoreFields, "__Short description")#> +/// Path: <#= template.Path #> +/// ID: <#= template.ID.ToString() #> +/// +[SitecoreType(TemplateId=<#= AsInterfaceName(template.Name) #>Constants.TemplateIdString)] //, Cachable = true +public partial class <#= AsClassName(template.Name) #> : <#= className #>, <#=AsInterfaceName(template.Name)#> +{ +<#foreach(SitecoreField field in GetFieldsForTemplate(template, true)){#> +/// +/// The <#=field.Name#> field. +/// <#= GetValue(field.SitecoreFields, "__Short description")#> +/// Field Type: <#=field.Type#> +/// Field ID: <#=field.ID.ToString()#> +/// Custom Data: <#=field.Data#> +/// +[global::System.CodeDom.Compiler.GeneratedCodeAttribute("<#=Tool#>", "<#=ToolVersion#>")] +[SitecoreField(<#= AsInterfaceName(template.Name) #>Constants.<#= GetPropertyName(field) #>FieldName)] +public virtual <#=GetGlassFieldType(field)#> <#= GetPropertyName(field) #> {get; set;} +<#}#> +} <#}#> } - <#+ /// /// Gets the inheritance string for the generated template @@ -134,107 +122,95 @@ if (!IsInterfaceWord(template.Name)){ #> /// public static string GetObjectInheritanceDefinition(string defaultNamespace, SitecoreTemplate item, bool includeLeadingComma, Func nameFunc) { - if (item.BaseTemplates.Count > 0) - { - return string.Concat(includeLeadingComma ? ", " : "", - item.BaseTemplates - .Select( bt => GetFullyQualifiedName(defaultNamespace, bt, nameFunc)) // select the name of the template with an 'I' prefix - .Aggregate( (total,next) => total + ", " + next) // basically a string.join(string[], '') - ); - } - return ""; +if (item.BaseTemplates.Count > 0) +{ +return string.Concat(includeLeadingComma ? ", " : "", +item.BaseTemplates +.Select( bt => GetFullyQualifiedName(defaultNamespace, bt, nameFunc)) // select the name of the template with an 'I' prefix +.Aggregate( (total,next) => total + ", " + next) // basically a string.join(string[], '') +); +} +return ""; } - public static string GetGlassFieldType(SitecoreField field) { - if (field != null && field.Type != null) - { - // Pull out any 'type' param from the custom data field on the field in TDS - string customType = GetCustomProperty(field.Data, "type"); - string generic = GetCustomProperty(field.Data, "generic"); - - if (customType != "") - { - if (generic != "") - { - return string.Format("{0}<{1}>", customType, generic); - } - else - { - return customType; - } - } - - switch(field.Type.ToLower()) - { - case "tristate": - return "TriState"; - case "checkbox": - return "bool"; - - case "date": - case "datetime": - return "DateTime"; - - case "number": - return "float"; - - case "integer": - return "int"; - - case "treelist with search": - case "treelist": - case "treelistex": - case "treelist descriptive": - case "checklist": - case "multilist with search": - case "multilist": - return string.Format("IEnumerable<{0}>", string.IsNullOrEmpty(generic) ? "Guid" : generic); - - case "grouped droplink": - case "droplink": - case "lookup": - case "droptree": - case "reference": - case "tree": - return "Guid"; - - case "file": - return "File"; - - case "image": - return "Image"; - - case "general link": - case "general link with search": - return "Link"; - - case "password": - case "icon": - case "rich text": - case "html": - case "single-line text": - case "multi-line text": - case "frame": - case "text": - case "memo": - case "droplist": - case "grouped droplist": - case "valuelookup": - return "string"; - case "attachment": - case "word document": - return "System.IO.Stream"; - case "name lookup value list": - case "name value list": - return "System.Collections.Specialized.NameValueCollection"; - default: - return "object /* UNKNOWN */"; - } - } - else - { - throw new Exception("There is no 'Type' field on the " + field.Name + " field."); - } +if (field != null && field.Type != null) +{ +// Pull out any 'type' param from the custom data field on the field in TDS +string customType = GetCustomProperty(field.Data, "type"); +string generic = GetCustomProperty(field.Data, "generic"); +if (customType != "") +{ +if (generic != "") +{ +return string.Format("{0}<{1}>", customType, generic); +} +else +{ +return customType; +} +} +switch(field.Type.ToLower()) +{ +case "tristate": +return "TriState"; +case "checkbox": +return "bool"; +case "date": +case "datetime": +return "DateTime"; +case "number": +return "float"; +case "integer": +return "int"; +case "treelist with search": +case "treelist": +case "treelistex": +case "treelist descriptive": +case "checklist": +case "multilist with search": +case "multilist": +return string.Format("IEnumerable<{0}>", string.IsNullOrEmpty(generic) ? "Guid" : generic); +case "grouped droplink": +case "droplink": +case "lookup": +case "droptree": +case "reference": +case "tree": +return "Guid"; +case "file": +return "File"; +case "image": +return "Image"; +case "general link": +case "general link with search": +return "Link"; +case "password": +case "icon": +case "rich text": +case "html": +case "single-line text": +case "multi-line text": +case "frame": +case "text": +case "memo": +case "droplist": +case "grouped droplist": +case "valuelookup": +return "string"; +case "attachment": +case "word document": +return "System.IO.Stream"; +case "name lookup value list": +case "name value list": +return "System.Collections.Specialized.NameValueCollection"; +default: +return "object /* UNKNOWN */"; +} +} +else +{ +throw new Exception("There is no 'Type' field on the " + field.Name + " field."); +} } #>