public
Created

Read DataAnnotations values from the enity

  • Download Gist
HerokuTemplate.t4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
<#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #>
<#@ Output extension="txt" #>
<#@ assembly name="System.ComponentModel.DataAnnotations" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Data.Linq" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.ComponentModel.DataAnnotations" #>
<#@ import namespace="System.Data.Linq.Mapping" #>
<#@ import namespace="System.Data.Objects.DataClasses" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
 
<# var viewDataType = (EnvDTE.CodeType) Model.ViewDataType; #>
Namespace :<#= Model.Namespace #>
Full Namespace :<#= viewDataType.FullName #>
Entity :<#= viewDataType.Name #>
<#
foreach (var prop in viewDataType.VisibleMembers().OfType<EnvDTE.CodeProperty>()) {
#>
--------------------------------------------------------------------------
PropertyName :<#= prop.Name #>
Type :<#= prop.Type #>
IsBindableType :<#= IsBindableType(prop.Type) #>
 
IsDouble :<#= prop.Type.UnderlyingTypeIs<double>() #>
IsString :<#= prop.Type.UnderlyingTypeIs<string>() #>
IsBool :<#= prop.Type.UnderlyingTypeIs<bool>() #>
Isint :<#= prop.Type.UnderlyingTypeIs<int>() #>
IsDateTime :<#= IsDateTime(prop) #>
 
IsReadable :<#= prop.IsReadable() #>
HasIndexParameters :<#= !prop.HasIndexParameters() #>
 
FullName :<#= prop.FullName #>
AsFullName :<#= prop.Type.AsFullName #>
MaxLenght :<#= GetMaxLength(prop) #>
IsRequired :<#= IsRequired(prop) #>
 
GetValueExpression :<#= !IsBindableType(prop.Type) #>
<#}#>
 
<#+
 
bool IsDateTime(EnvDTE.CodeProperty propInfo) {
return propInfo.Type.UnderlyingTypeIs<bool>();
}
 
string GetMaxLength(EnvDTE.CodeProperty propInfo) {
var attributes = propInfo.Attributes.OfType<CodeAttribute2>();
if(attributes == null){ return "";}
var me = attributes.FirstOrDefault(x => x.Name == "MaxLength");
if(me == null){ return "";}
var firstArgValue = me.Value.Split(',').FirstOrDefault();
return firstArgValue;
}
bool IsRequired(EnvDTE.CodeProperty propInfo){
var attributes = propInfo.Attributes.OfType<CodeAttribute2>();
var me = attributes.FirstOrDefault(x => x.Name == "Required");
if(me == null) {
return false;
}
 
return true;
}
 
// propertyType.FindProperty(displayPropertyNames);
 
static string[] HasAttributMaxLength = new[] {"MaxLengthAttribute"};
static string[] HasAttributRequired = new[] {"RequiredAttribute"};
static string[] displayPropertyNames = new[] { "Name", "Title", "LastName", "Surname", "Subject", "Count" };
 
// Describes the information about a property on the model
class ModelProperty {
public string Name { get; set; }
public string ValueExpression { get; set; }
public EnvDTE.CodeTypeRef Type { get; set; }
public bool IsPrimaryKey { get; set; }
public bool IsForeignKey { get; set; }
public bool IsReadOnly { get; set; }
public bool IsRequired { get; set; }
public bool HasMaxLength { get; set; }
public int MaxLength { get; set; }
}
 
// Liste von NonPrimitiveTypes die aber bearbeitet werden sollten in einem input
static Type[] bindableNonPrimitiveTypes = new[] {
typeof(string),
typeof(decimal),
typeof(Guid),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
};
 
// Helper Code Type Ref
bool IsBindableType(EnvDTE.CodeTypeRef type) {
return type.UnderlyingIsPrimitive() || bindableNonPrimitiveTypes.Any(x => type.UnderlyingTypeIs(x));
}
 
IEnumerable<RelatedEntityInfo> ParentRelations {
get { return ((IEnumerable)Model.RelatedEntities).OfType<RelatedEntityInfo>().Where(x => x.RelationType == RelationType.Parent); }
}
#>
Role.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
 
namespace Ch.Heroku.Entities
{
[DisplayColumn("RoleName")]
public class Role
{
 
[Required]
public int Id { get; set; }
 
[Display(Name = "Role Name")]
[Required(ErrorMessage = "Role Name is required",AllowEmptyStrings = true)]
[MaxLength(100, ErrorMessage = "max is 100")]
public string RoleName { get; set; }
 
[MaxLength(8, ErrorMessage = "max is 8")]
public DateTime ModifedAt { get; set; }
 
[ForeignKey("Id")]
public virtual ICollection<User> Users { get; set; }
}
}
demo-output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
 
Namespace :Ch.Heroku
Full Namespace :Ch.Heroku.Entities.Role
Entity :Role
--------------------------------------------------------------------------
PropertyName :Id
Type :System.__ComObject
IsBindableType :True
 
IsDouble :False
IsString :False
IsBool :False
Isint :True
IsDateTime :False
 
IsReadable :True
HasIndexParameters :True
 
FullName :Ch.Heroku.Entities.Role.Id
AsFullName :System.Int32
MaxLenght :
IsRequired :True
 
GetValueExpression :False
--------------------------------------------------------------------------
PropertyName :RoleName
Type :System.__ComObject
IsBindableType :True
 
IsDouble :False
IsString :True
IsBool :False
Isint :False
IsDateTime :False
 
IsReadable :True
HasIndexParameters :True
 
FullName :Ch.Heroku.Entities.Role.RoleName
AsFullName :System.String
MaxLenght :100
IsRequired :True
 
GetValueExpression :False
--------------------------------------------------------------------------
PropertyName :ModifedAt
Type :System.__ComObject
IsBindableType :True
 
IsDouble :False
IsString :False
IsBool :False
Isint :False
IsDateTime :False
 
IsReadable :True
HasIndexParameters :True
 
FullName :Ch.Heroku.Entities.Role.ModifedAt
AsFullName :System.DateTime
MaxLenght :8
IsRequired :False
 
GetValueExpression :False
--------------------------------------------------------------------------
PropertyName :Users
Type :System.__ComObject
IsBindableType :False
 
IsDouble :False
IsString :False
IsBool :False
Isint :False
IsDateTime :False
 
IsReadable :True
HasIndexParameters :True
 
FullName :Ch.Heroku.Entities.Role.Users
AsFullName :System.Collections.Generic.ICollection<Ch.Heroku.Entities.User>
MaxLenght :
IsRequired :False
 
GetValueExpression :True
run.ps1
PowerShell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
[T4Scaffolding.Scaffolder(Description = "Test to read attributs from the model in ps and t4")][CmdletBinding()]
param(
[string]$Project,
[string]$ModelType,
[string]$CodeLanguage,
[string[]]$TemplateFolders,
[switch]$Force = $false
)
#Write-Host $Model
$foundModelType = Get-ProjectType $ModelType -Project $Project
if (!$foundModelType) { return }
$primaryKeyName = [string](Get-PrimaryKey $foundModelType.FullName -Project $Project)
 
Write-Host "....................."
foreach($i in $foundModelType.Attributes) {
Write-Host $i.Name
Write-Host $i.Value
}
 
Write-Host "....................."
foreach($i in $foundModelType.Members) {
Write-Host $i.Name
foreach($d in $i.Attributes){
Write-Host $d.Name
Write-Host $d.Value
}
}
 
Write-Host "....................."
foreach($i in $foundModelType.Collection) {
Write-Host $i.Name
Write-Host $i.Value
}
 
#Ch.Heroku.Entities.Role
#Write-Host $foundModelType.FullName
 
 
if ($foundModelType) { $relatedEntities = [Array](Get-RelatedEntities $foundModelType.FullName -Project $project) }
if (!$relatedEntities) { $relatedEntities = @() }
 
$outputPath = Join-Path Output Demo
$namespace = (Get-Project $Project).Properties.Item("DefaultNamespace").Value
 
Add-ProjectItemViaTemplate $outputPath -Template HerokuTemplate `
-Model @{
Namespace = $namespace;
ViewDataType = [MarshalByRefObject]$foundModelType;
ViewDataTypeName = $foundModelType.Name;
} `
-SuccessMessage "Added Heroku output at {0}" `
-TemplateFolders $TemplateFolders -Project $Project -CodeLanguage $CodeLanguage -Force:$Force

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.