{"action":"closed","issue":{"url":"https://api.github.com/repos/CollaboratingPlatypus/PetaPoco/issues/29","labels_url":"https://api.github.com/repos/CollaboratingPlatypus/PetaPoco/issues/29/labels{/name}","comments_url":"https://api.github.com/repos/CollaboratingPlatypus/PetaPoco/issues/29/comments","events_url":"https://api.github.com/repos/CollaboratingPlatypus/PetaPoco/issues/29/events","html_url":"https://github.com/CollaboratingPlatypus/PetaPoco/issues/29","id":949644,"number":29,"title":"Attributes to support validation","user":{"login":"waydec","id":754691,"avatar_url":"https://avatars.githubusercontent.com/u/754691?v=3","gravatar_id":"","url":"https://api.github.com/users/waydec","html_url":"https://github.com/waydec","followers_url":"https://api.github.com/users/waydec/followers","following_url":"https://api.github.com/users/waydec/following{/other_user}","gists_url":"https://api.github.com/users/waydec/gists{/gist_id}","starred_url":"https://api.github.com/users/waydec/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/waydec/subscriptions","organizations_url":"https://api.github.com/users/waydec/orgs","repos_url":"https://api.github.com/users/waydec/repos","events_url":"https://api.github.com/users/waydec/events{/privacy}","received_events_url":"https://api.github.com/users/waydec/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/CollaboratingPlatypus/PetaPoco/labels/discussion","name":"discussion","color":"009800"},{"url":"https://api.github.com/repos/CollaboratingPlatypus/PetaPoco/labels/needs%20design","name":"needs design","color":"006b75"},{"url":"https://api.github.com/repos/CollaboratingPlatypus/PetaPoco/labels/needs%20review","name":"needs review","color":"fbca04"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2011-05-24T21:22:14Z","updated_at":"2015-12-12T21:14:42Z","closed_at":"2015-12-12T21:14:42Z","body":"I use code generation to create FluentValidation (http://fluentvalidation.codeplex.com/) validator classes. \r\n\r\nI have made a few modifications to PetaPoco.cs and the T4 templates to support the code gen of these classes\r\n\r\nIf you agree i would love to see this included in the product.\r\n\r\n_In PetaPoco.cs_\r\n\r\n\t\t// Property Attribute used to pass metadata to Validator class code generator\r\n\t\t// Marks property as nullable\r\n\t\t[AttributeUsage(AttributeTargets.Property)]\r\n\t\tpublic class NullableAttribute : Attribute\r\n\t\t{\r\n\t\t}\r\n\r\n\t\t// Property Attribute used to pass metadata to Validator class code generator\r\n\t\t// Marks string length\r\n\t\t[AttributeUsage(AttributeTargets.Property)]\r\n\t\tpublic class MaxLengthAttribute : Attribute\r\n\t\t{\r\n\t\t\tpublic MaxLengthAttribute() { }\r\n\t\t\tpublic MaxLengthAttribute(int maxLength) { MaxLength = maxLength; }\r\n\t\t\tpublic int MaxLength { get; set; }\r\n\t\t}\r\n\r\n_In PetaPoco.Core.ttinclude_\r\nAdd a public field (\"MaxLength\") to the Column Class\r\n\r\n\tpublic class Column\r\n\t{\r\n\t\tpublic string Name;\r\n\t\tpublic string PropertyName;\r\n\t\tpublic string PropertyType;\r\n\t\tpublic bool IsPK;\r\n\t\tpublic bool IsNullable;\r\n\r\n\t\t// Change - Add the following Field\r\n\t\tpublic int MaxLength;\t//Used by [MaxLength(MaxLength)] attribute and ultimately by EntityValidator Class\r\n\t}\r\n\r\n\r\n_In PetaPoco.Generator.ttinclude_\r\nIn the column output section add the following code.  It adds padded attributes to the column property declarations\r\n\r\n\tforeach(Column col in from c in tbl.Columns where !c.Ignore select c)\r\n\t{\r\n\t\t// Column bindings\r\n\t\tstring nullable = \"          \";\r\n\t\tif(col.IsNullable==true)\r\n\t\t\tnullable = \"[Nullable]\";\r\n\t\t\r\n\t\tstring maxLength = \"\";\r\n\t\tif(col.PropertyType==\"string\")\r\n\t\t\tmaxLength = \"[MaxLength(\" + col.MaxLength.ToString() + \")]\";\r\n\t\tmaxLength = maxLength.PadRight(17);\r\n\r\n\t\tstring propertyName = col.PropertyName;\r\n\t\t\r\n\t\tstring propertyType = (col.PropertyType + CheckNullable(col));\r\n\t\tpropertyType = propertyType.PadRight(12);\r\n\t}\r\n\r\nI changed the template code to \r\n\r\n<# if (col.Name!=col.PropertyName) { #>\r\n\t\t[Column(\"<#=col.Name#>\")] <#=nullable#> <#=maxLength#> public <#=propertyType#> <#=propertyName#> { get; set; }\r\n<# } else { #>\r\n\t\t[Column] <#=nullable#> <#=maxLength#> public <#=propertyType#> <#=propertyName#> { get; set; }\r\n<# } #>\r\n\r\n\r\n"}}