VB or C# Example Template

Posts   
 
    
Posts: 2
Joined: 21-Nov-2007
# Posted on: 21-Nov-2007 16:11:39   

Hello,

Is there an example somewhere of a custom template written in either VB or C#? I've been using TDL up to this point, but I've run into a scenario that I don't think it's made to handle - if it is, please correct me!

This is what I want to do:

<[If TypeOfField System.String]> If (mystring.Length > <[SourceColumnMaxLength]>) Then doSomething End If <[EndIf]>

Similarly I would like to test other field types for validity. The template I'm writing generates validator classes for each of my entities, and I want to generate the code that tests data to ensure it meets the constraints of the DB field definitions.

Note: When I open the CHM that comes with Template Studio, I'm unable to navigate to any pages without getting a "Navigation to the page was canceled" error, so I wasn't able to check it for examples.

Thanks in advance!

Leigh

Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 22-Nov-2007 11:03:52   

I;m not an expert in TDL templates, but wouldn't the following do the Job?

<[If TypeOfField System.String]>
    If (<[EntityFieldName]> > <[SourceColumnMaxLength]>) Then
        doSomething
    End If
<[EndIf]>

Note: When I open the CHM that comes with Template Studio, I'm unable to navigate to any pages without getting a "Navigation to the page was canceled" error, so I wasn't able to check it for examples.

PLease check the following link: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=10965

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39774
Joined: 17-Aug-2003
# Posted on: 22-Nov-2007 11:14:34   

I think you can better use an .lpt template in this case. You can mix .lpt templates and TDL templates in one session. You can also use .lpt templates as include templates inside TDL templates if you want to. With .lpt templates you can access the complete object model of the project so you can access any element in the entity/field etc. definition.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 2
Joined: 21-Nov-2007
# Posted on: 22-Nov-2007 19:43:59   

Thanks! Everything works now. I was able to read the CHM file, and after a bit of work, I was able to achieve field length checking. In the TDL template that generates my validator classes, I added an include pointing to a VB.Net .lpt template as follows:

<% Dim currentEntity As EntityDefinition = CType((CType(_activeObject, Hashtable))("CurrentEntity"), EntityDefinition) %>
<% If currentEntity.Fields.Count > 0 Then %>
    <% For Each currentField As EntityFieldDefinition In currentEntity.Fields %>
Private Function ValidateFieldValue<%=currentField.FieldName%>(ByVal involvedEntity As IEntityCore, ByVal value As <%=currentField.DotNetType %>) As Boolean

    Dim toReturn As Boolean = True
    
    <% If (Not currentField.IsNullable) Then %>
    If ( value Is Nothing ) Then
        toReturn = False
    End If
    <% End If %>
    
    <% If currentField.DotNetType Is GetType(System.String) Then %>
    If value.Length > <%=currentField.MappedFieldLength %> Then
        toReturn = False
    End If
    <% End If %>

    Return toReturn
End Function
    <% Next %>
<% End If %>

That code generates a validation function for each field in the entity, where non-nullable DB fields are checked for NULL, and textual DB fields are checked against maximum length. A calling function is reponsible for executing the individual field functions.

Thanks again, and congratulations on a wonderful product that I intend to use a great deal in the future.

Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 23-Nov-2007 09:57:21   

Thank you very much for the feedback and for the positive comments.