2013-06-19 45 views
1

我試圖通過加載另一個類的模型來生成一個類,這裏​​是我使用T4腳手架 - 如何檢測可空類型

using System; 
using System.Text; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 


namespace Sma.Domain.Model { 

    public class Smartistdetails { 
     public Smartistdetails() { } 
     [Key] 
     public int ArtistID { get; set; } 
     public string BandName? { get; set; } 
     public string LoginName { get; set; } 
     public string Password { get; set; }  
    } 
} 

類\模型,你看到有一些可空的類型,如public string BandName? { get; set; }這是我遇到問題的地方。我可以成功地做出輸出類,但是當模型的屬性可以爲空時,我想檢測它並採取相應的行動,但我不知道該怎麼做。

這裏是Map(x=>x.<#= property.Name #>).Column("<#= property.Name #>");之前我T4模板

<#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #> 
<#@ Output Extension="cs" #> 
<#@ import namespace="System.Collections" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System" #> 
<#@ import namespace="EnvDTE" #> 
<#@ assembly name="System.ComponentModel.DataAnnotations" #> 
<#@ assembly name="System.Core" #> 
<#@ assembly name="System.Data.Entity" #> 
<#@ assembly name="System.Data.Linq" #> 
<#@ import namespace="System.Data.Linq.Mapping" #> 
<#@ import namespace="System.Linq" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System.Data.Objects.DataClasses" #> 
<#@ import namespace="System.Reflection" #> 
<#@ import namespace="System.Collections" #> 
<#@ import namespace="System.Text.RegularExpressions" #> 

using System; 
using System.Collections.Generic; 
using System.Text; 
using FluentNHibernate.Mapping; 
using Sma.Domain.Model; 
<# 
    var modelType = (CodeType)Model.ModelType; 
    var modelName = modelType.Name; 
    var properties = modelType.VisibleMembers().OfType<CodeProperty>();  
    var primaryKeyProperty = modelType.VisibleMembers().OfType<CodeProperty>().Single(x => x.Name == Model.PrimaryKey); 
#> 

namespace <#= Model.Namespace #>.Mapping 
{ 
    public class <#= Model.EntityName #>Map : ClassMap<<#= Model.EntityName #>> 
    {  
     public <#= Model.EntityName #>Map() { 
     Table("<#= Model.EntityName #>"); 
     LazyLoad();  
<# var type = ""; 
var typeName = ""; 
foreach (var property in properties) 
{ 
type = GetType(property.Type); 
typeName = GetTypeName(property.Type); 
if(isType(type)) 
{ 
if(Model.PrimaryKey == property.Name) 
{ 
#> 
     Id(x => x.<#= property.Name #>).GeneratedBy.Identity().Column("<#= property.Name #>"); 
<#  
} 
else 
{ 
#> 
     Map(x=>x.<#= property.Name #>).Column("<#= property.Name #>");  
<#} 
} 
else 
{ 
#> 
     References(x=>x.<#= property.Name #>);    
<# 
} 
} 
#>  } 
    } 
} 

<#+ 
    string GetType(EnvDTE.CodeTypeRef type) 
    {                                    
     string str = ""; 
     if (type.UnderlyingTypeIs<byte>()) 
      str = "byte"; 
     else if (type.UnderlyingTypeIs<sbyte>()) 
      str = "sbyte"; 
     else if (type.UnderlyingTypeIs<int>()) 
      str = "int"; 
     else if (type.UnderlyingTypeIs<uint>()) 
      str = "uint"; 
     else if (type.UnderlyingTypeIs<short>()) 
      str = "short"; 
     else if (type.UnderlyingTypeIs<ushort>()) 
      str = "ushort"; 
     else if (type.UnderlyingTypeIs<DateTime>()) 
      str = "DateTime"; 
     else if (type.UnderlyingTypeIs<long>()) 
      str = "long"; 
     else if (type.UnderlyingTypeIs<ulong>()) 
      str = "ulong"; 
     else if (type.UnderlyingTypeIs<double>()) 
      str = "double"; 
     else if (type.UnderlyingTypeIs<bool>()) 
      str = "bool"; 
     else if (type.UnderlyingTypeIs<string>()) 
      str = "string"; 
     else if (type.UnderlyingTypeIs<decimal>()) 
      str = "decimal"; 
     else 
      str = type.AsFullName.ToString(); 

     return str; 
    } 

    string GetTypeName(EnvDTE.CodeTypeRef type) 
    { 
     string str = type.AsFullName.ToString(); 
     return str; 
    } 

    bool isType(string _type) 
    { 
     if(_type == "byte") return true; 
     else if(_type == "sbyte") return true; 
     else if(_type == "int") return true; 
     else if(_type == "uint") return true; 
     else if(_type == "short") return true; 
     else if(_type == "ushort") return true; 
     else if(_type == "DateTime") return true; 
     else if(_type == "long") return true; 
     else if(_type == "ulong") return true; 
     else if(_type == "double") return true; 
     else if(_type == "bool") return true; 
     else if(_type == "string") return true; 
     else if(_type == "decimal") return true; 
     return false; 
    } 
#> 

我要檢查,如果該屬性爲空,如果不是我想指定Map(x=>x.<#= property.Name #>).Column("<#= property.Name #>").NotNullable();

怎麼辦呢

只是 - 這裏是我的.ps1 powershell腳本

[T4Scaffolding.Scaffolder(Description = "Enter a description of ExtMapping here")][CmdletBinding()] 
param(  
    [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$ModelType, 
    [string]$Project, 
    [string]$CodeLanguage, 
    [string[]]$TemplateFolders, 
    [switch]$Force = $false 
) 

$mynamespace = "Sma" 
$projectFor = $mynamespace + ".Infrastructure.Data" 
$namespace = $projectFor # (Get-Project $projectFor).Properties.Item("DefaultNamespace").Value 
$outputPath = "Mapping\" + $ModelType 
Write-Host "Scaffolding $ModelType ExtMapping..." 

$useModelFromProject = $mynamespace + ".Domain" 
# get the modelType object from the name 
$foundModelType = Get-ProjectType $ModelType -Project $useModelFromProject -BlockUi 
$primaryKey = Get-PrimaryKey $ModelType -Project $useModelFromProject 
$modelOutputPath = "Mapping\" + $ModelType + "Map" 

Add-ProjectItemViaTemplate $modelOutputPath -Template ExtMapping ` 
    -Model @{ 
    Namespace = $namespace; 
    ModelType = [MarshalByRefObject]$foundModelType; 
    PrimaryKey = $primaryKey; 
    EntityName = $ModelType } ` 
    -SuccessMessage "Added ExtMapping output at $projectFor" ` 
    -TemplateFolders $TemplateFolders -Project $projectFor -CodeLanguage $CodeLanguage -Force:$Force 

回答

1
(Get-ProjectType $ModelType).Children | Where-Object{ $_.Type.CodeType.Name -eq "Nullable"} | ForEach{"$($_.Name),$($_.Type.AsString)"} 

這個片段將提取nullables並從包含在$ ModelType

類的名字顯示他們是「PropetyName,NullableType」