Reputation: 15797
Say I have a Data Transfer object like the one below:
Public Class Person
Private _Name As String
Private _Age As Integer
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _Age
End Get
Set(ByVal value As Integer)
_Age = value
End Set
End Property
End Class
This is an object representation of the following table:
CREATE TABLE Person (PersonID int identity primary key, Name varchar(30), age int)
Say I wanted to create an order table in the database as follows:
CREATE TABLE Order (OrderID int identity primary key, PersonID FOREIGN KEY references Person(ID), OrderDate datetime)
I believe I could simply add two more instance variables and properties to the Data Transfer Object (assuming that there is always a maximum of one order per person). Is this advisable or should you always have one DTO per database table?
Upvotes: 0
Views: 1401
Reputation: 40393
Your assumption of one order per person doesn't really seem right. It may be true now, but the fact that you've got them separated out into two tables with a one-to-many means that you've designed it so that you can have multiple orders per person.
I'd stick with separate classes, but if you want to get everything at once, have complex DTOs, like:
class PersonDTO
String Name
Integer Age
OrderDTO() Orders
' or
class OrderDTO
Integer OrderID
PersonDTO Person
DateTime OrderDate
You'd have to build your SQL accordingly, either using JOINs or an ORM to get the complex object back in a single query, or just make multiple queries, but this way a single DTO would get you everything you need, while still allowing you to keep them separate.
I'd probably avoid having both classes be complex - you'd have a circular reference, which is tricky when you're serializing this stuff, so I'd just determine which one would be more valuable.
Upvotes: 1