Data Transfer Object Pattern Goes VB.NET

Data Transfer Object Pattern Goes VB.NET

Here is a simple example for the Data Transfer Pattern (DTO) Pattern in VB.NET. The task for using this pattern is the following:

The client must not know the details about the structure of business objects on the server side. However, the client must be able to work with all data from the business objects.

We write an example for a business report. The report contains two business objects:

  • ContractObject
  • BusinessPartnerObject

Our solution has a service-oriented architecture and the client gets only ONE object from the service with all datas from both business objects. We call this a Data Transfer Object (DTO).

The DTO looks like this:

Public Class ReportDTO

   Private m_contractname As String    = ""
   Private m_contractdate As Date      = Nothing
   Private m_businesspartner As String = ""

   Public Property ContractName() As String

Get
   Return Me.m_contractname
   End Get

   Set(ByVal value As String)
   Me.m_contractname = Value
   End Set
   End Property

   Public Property ContractDate() As Date

Get
   Return Me.m_contractdate
   End Get

   Set(ByVal value As Date)
   Me.m_contractdate = Value
   End Set
   End Property

   Public Property BusinessPartner() As String

Get
   Return Me.m_businesspartner
   End Get

   Set(ByVal value As String)
   Me.m_businesspartner = Value
   End Set
   End Property
   End Class

And the business objects are here:

Public Class ContractBDO

   Private m_contractname As String = ""
   Private m_contractdate As Date   = Nothing

   Public Property ContractName() As String

Get
   Return Me.m_contractname
   End Get

   Set(ByVal value As String)
   Me.m_contractname = Value
   End Set
   End Property

   Public Property ContractDate() As Date

Get
   Return Me.m_contractdate
   End Get

   Set(ByVal value As Date)
   Me.m_contractdate = Value
   End Set
   End Property


End Class



Public Class BusinessPartnerBDO
   Private m_businesspartner As String = ""

   Public Property BusinessPartner() As String

Get
   Return Me.m_businesspartner
   End Get

   Set(ByVal value As String)
   Me.m_businesspartner = Value
   End Set
   End Property

   End Class

The client is now able to fill the DTO with necessary data and send it back to the service. The service has an assembler that is able to transport the date from the DTO to the expected business object and the reverse.

And, here is our assembler:

Public Class ReportAssembler

   Public Shared Function MakeContractObject(ByVal dto As ReportDTO) _
          As ContractBDO


   Dim obj As New ContractBDO
   obj.contractname = dto.contractname
   obj.contractdate = dto.contractdate
   Return obj

   End Function

   Public Shared Function MakeBusinessPartnerObject(ByVal dto _
          As ReportDTO) As BusinessPartnerBDO

   Dim obj As New BusinessPartnerBDO
   obj.businesspartner = dto.businesspartner
   Return obj

   'Reverse Methods go here

   End Class

The service call from the client side looks like this:

Public Class ReportService

   'Example for writing the objects from the DTO
   Public Sub WriteObjects(ByVal dto As ReportDTO)

   Dim trans As Transaction    'Example only

   Dim objContract As ContractBDO = _
       ReportAssembler.MakeContractObject(dto)
   Dim objBusinessPartner As BusinessPartnerBDO = _
       ReportAssembler.MakeBusinessPartnerObject(dto)

   trans.AddWriteObject(objContract)
   trans.AddWriteObject(objBusinessPartner)

   trans.Execute()

   End Sub

   End Class

The advantage is that the client gets only one object and only the service task has to fill the two database relations. The client is always stupid and knows nothing about the data structure in the background.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Some of the statistics about the growth of enterprise mobility are surprising, not the least of which is the 4X increase in mobile malware from 2013 to 2014. Many employees today not only expect to be able to use their own personal devices at work, but will even contravene policies that restrict personal device use for work.  IT must find the right approach that balances the benefits of mobility with the risks involved.  This on-demand webcast offers steps toward developing a winning mobile strategy, and …

Most Popular Programming Stories

More for Developers

RSS Feeds