Skip to content

Instantly share code, notes, and snippets.

@linuxbender
Created October 8, 2011 21:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxbender/1272922 to your computer and use it in GitHub Desktop.
Save linuxbender/1272922 to your computer and use it in GitHub Desktop.
Read DataAnnotations values from the enity
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
<#@ 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); }
}
#>
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; }
}
}
[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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment