使用t4模板确定属性的类型

时间:2016-03-24 20:20:46

标签: vb.net entity-framework templates t4 devart

我正在使用Devart在其Entity Developer产品中提供的模板。我真的不确定这些是纯T4模板,但它们看起来确实很接近。我要做的是为我的实体创建部分类实现IDataErrorInfo并删除它们实现所需的基本例程。模板设置为在VB.NET中生成代码

我希望在IDataErrorInfo接口的实现部分中看到的输出类型示例如下:

Default Public ReadOnly Property Item(columnName As String) As String Implements IDataErrorInfo.Item
    Get
        Select Case columnName
            Case "CustomerId"
                Return If(CustomerId is Nothing, "A Customer Must Be associated with the Order.", Nothing)
            Case "InvoiceAddressId"
                Return if(InvoiceAddressId Is Nothing, "An invoice address is required.", Nothing)
            Case "OrderDate"
                Return If(OrderDate Is Nothing Or OrderDate > Today, "A Valid order date that is not in the future is required.", Nothing)

        End Select
    End Get
End Property

Public ReadOnly Property [Error] As String Implements IDataErrorInfo.Error
    Get
        Return If(Me("CustomerId") IsNot Nothing OrElse Me("InvoiceAddressId") IsNot Nothing OrElse Me("OrderDate") IsNot Nothing, "Correct values", Nothing)
    End Get
End Property

我目前在模板中有以下内容:

#Region "IDataErrorInfo Implementation"

    Default Public ReadOnly Property Item(columnName As String) As String Implements IDataErrorInfo.Item
        Get
            Select Case columnName
<#    
For Each prop As EntityProperty In cls.Properties #>
                Case $"{<#= prop.Name #>}"
         <# if Property.GetType() Is TypeOf(String) Then #>
                    Return If(<#= prop.Name #> Is Nothing,"Add Message here.",Nothing)
         <# Else #>
                    Return If(<#= prop.Name #> = Nothing,"Add Message here.",Nothing)
         <#End if 
Next
#>
            End Select
        End Get
    End Property

    Public ReadOnly Property [Error] As String Implements IDataErrorInfo.Error
        Get

        End Get
    End Property

#End Region

Item属性的定义中,如果我删除&#34; if&#34;应该确定列表中引用的当前属性的类型的条件,然后我最终得到一个完全存根的select case语句,但是当被引用的属性不是字符串时,那么当涉及到构建时代码失败的项目因为&#39;是&#39;不是一个很好的比较语句,例如当前在循环中的属性恰好是整数或双精度。

任何人都可以发现我在模板中发生的任何明显错误。

0 个答案:

没有答案