VB.NET DTO Template for v2.5

Posts   
 
    
Posts: 3
Joined: 24-Oct-2007
# Posted on: 14-Nov-2007 14:00:49   

After looking through the forums, I found a DTO template for C# but not one for VB. Here is the existing C# template translated over to VB for those who would like to use this in a VB.NET project.

Frans, any change of getting these DTO templates included in the next release since we have both languages now? :-)


Imports System
Imports System.ComponentModel
Imports System.Collections
Imports System.Runtime.Serialization
Imports <[RootNamespace]>.EntityClasses

Namespace <[RootNamespace]>.DtoClasses
    ''' <summary>
    ''' DTO class for the entity '<[CurrentEntityName]>'.
    ''' </summary>
    <Serializable()> _
    Public <[If UsePartialClasses]>Partial <[EndIf]>Class <[CurrentEntityName]>Dto<[ If IsSubType ]>
        Inherits <[ SuperTypeName ]>Dto<[ EndIf]>

        #Region "Class Member Declarations"
<[Foreach EntityField CrLf]>        Private _<[CaseCamel EntityFieldName]> As <[If IsNullable]><[If IsValueType]>Nullable(Of <[TypeOfField]>)<[Else]><[TypeOfField]><[EndIf]><[Else]><[TypeOfField]><[EndIf]><[NextForeach]>
        #End Region
    
        ''' <summary>
        ''' CTor
        ''' </summary>
        Public Sub New()
        
        End Sub

        ''' <summary>
        ''' CTor which initializes the DTO with values from its corresponding entity
        ''' </summary>
        ''' <param name="entityInstance">The entity instance which holds the values for this DTO</param>
        Public Sub New(ByVal entityInstance As <[CurrentEntityName]>Entity)
<[Foreach EntityField CrLf]>            _<[CaseCamel EntityFieldName]> = entityInstance.<[EntityFieldName]><[NextForeach]>
        End Sub
                
        ''' <summary>
        ''' Creates a new entity instance and copies over the values of this DTO
        ''' </summary>
        Public <[If IsSubType]>Overloads <[EndIf]>Function ToEntity() As <[CurrentEntityName]>Entity
            Return ToEntity(new <[CurrentEntityName]>Entity())
        End Function
        
        ''' <summary>
        ''' Copies over the values of this DTO into the entity passed in.
        ''' </summary>
        Public <[If IsSubType]>Overloads <[EndIf]>Function ToEntity(ByVal toFill As <[CurrentEntityName]>Entity) As <[CurrentEntityName]>Entity
<[Foreach EntityField CrLf]><[If Not IsReadOnly]>           toFill.<[EntityFieldName]> = _<[CaseCamel EntityFieldName]><[EndIf]><[NextForeach]>
<[If IsSubType]>            MyBase.ToEntity(toFill)
<[ EndIf]>Return toFill
        End Function

<[Foreach EntityField CrLf]>
        ''' <summary>The <[EntityFieldName]> property of the Entity <[CurrentEntityName]></summary>
        Public <[If EntityFieldOverrides]>Overrides<[Else]>Overridable<[EndIf]> <[If IsReadOnly]>ReadOnly <[EndIf]>Property [<[EntityFieldName]>]() As <[If IsNullable]><[If IsValueType]>Nullable(Of <[TypeOfField]>)<[Else]><[TypeOfField]><[EndIf]><[Else]><[TypeOfField]><[EndIf]>
            Get
                Return _<[CaseCamel EntityFieldName]>
            End Get<[If Not IsReadOnly]>
            Set(value As <[If IsNullable]><[If IsValueType]>Nullable(Of <[TypeOfField]>)<[Else]><[TypeOfField]><[EndIf]><[Else]><[TypeOfField]><[EndIf]>)
                _<[CaseCamel EntityFieldName]> = value
            End Set<[EndIf]>    
        End Property
<[NextForeach]>
    End Class
End Namespace

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-Nov-2007 15:15:36   

Thanks for sharing.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 15-Nov-2007 12:05:52   

We'll add both to the 3rd party section in the customer area. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 19-Nov-2007 12:00:27   

I've added links to both to the FAQ.

Frans Bouma | Lead developer LLBLGen Pro
jelling
User
Posts: 22
Joined: 26-Oct-2007
# Posted on: 21-Jan-2008 01:21:24   

Thanks for the great template.

One thing I learned while implementing it was that web services don't expose read-only properties. This makes sense for updates but it was hurting my cause when I was returning data to the client. What I did was remove the read-only parts of the template so that my properties remained read only in the DAL but were exposed via the DTOs. Our current plan is to do our updates via parameterized methods so hopefully this will work out.

Here's my revised <ForEach> section:


<[Foreach EntityField CrLf]> ''' <summary>The <[EntityFieldName]> property of the Entity <[CurrentEntityName]></summary> Public <[If EntityFieldOverrides]>Overrides<[Else]>Overridable<[EndIf]> Property <[EntityFieldName]> As <[If IsNullable]><[If IsValueType]>Nullable(Of <[TypeOfField]>)<[Else]><[TypeOfField]><[EndIf]><[Else]><[TypeOfField]><[EndIf]> Get Return _<[CaseCamel EntityFieldName]> End Get Set(value As <[If IsNullable]><[If IsValueType]>Nullable(Of <[TypeOfField]>)<[Else]><[TypeOfField]><[EndIf]><[Else]><[TypeOfField]><[EndIf]>) _<[CaseCamel EntityFieldName]> = value End Set End Property <[NextForeach]>