Link to home
Start Free TrialLog in
Avatar of LandyJ
LandyJFlag for United States of America

asked on

Complex Classes Style question...

(please forgive me if there are syntax errors. I just started typing, i hope you can get the gist of what I'm talking about)  Which style is the best/generally more accepted way of doing the following? I haven't dealt with creating REST services, so I'm curious...

Ex: I have an application where I have a user and two address: Too and Fro. Creating the classes and the WCF interface,  I come up with something like this.


<DataContract()> _
Public Class AThing
      Public User As User
      Public Fro As Fro
      Public Class Too
            Public Address As Address
      End Class
End Class

<DataContract()> _
Public Class Fro
      Inherits Address
End Class


<DataContract()> _
Public Class Address
      Private _Street As String = ""
      <DataMember()> _
      Public Property Street() As String
            Get
                  Return _Street
            End Get
            Set(ByVal value As String)
                  _Street = value
            End Set
      End Property

      Private _City As String = ""
      <DataMember()> _
      Public Property City() As String
            Get
                  Return _City
            End Get
            Set(ByVal value As String)
                  _City = value
            End Set
      End Property

      Private _State As String = ""
      <DataMember()> _
      Public Property State() As String
            Get
                  Return _State
            End Get
            Set(ByVal value As String)
                  _State = value
            End Set
      End Property

      Private _PostalCode As String = ""
      <DataMember()> _
      Public Property PostalCode() As String
            Get
                  Return _PostalCode
            End Get
            Set(ByVal value As String)
                  _PostalCode = value
            End Set
      End Property

End Class

And a XML string that looks like:
<?xml version="1.0"?>
<AThing>
      <User>
            <UserName>Me</UserName>
      </User>
      <Fro>
            <Street>123 Main St</Street>
            <City>Somewhere</City>
            <State>TX</State>
      </Fro>
      <Too>
            <Address>
                  <Street>987 Main St</Street>
                  <City>Somewhere Else</City>  
                  <State>FL</State>
            </Address>
      </Too>
</AThing>


When implementing a REST interface, which is the better way to do it? Or as a friend puts it: makes you look like you actually know what you're doing?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't know what you are really asking for, but here are some thoughts:

1) Use an interface to define the contract

2) Use auto properties where possible:

Public Property State() As String

vs.

Private _State As String = ""
      <DataMember()> _
      Public Property State() As String
            Get
                  Return _State
            End Get
            Set(ByVal value As String)
                  _State = value
            End Set
      End Property

3) Public Property From As Address
    Public Property To As Address
Avatar of LandyJ

ASKER

Oops. Sorry. I guess it was more clear in my mind than on screen. I was asking which way is better to deal with the address fields for the string that would be sent via the URL.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LandyJ

ASKER

Thanks. that link answered another question I had, but I'm trying to implement the ability to do both JSON and XML via URL. I was using the way MapQuest is set up (http://www.mapquestapi.com/geocoding/) as a guide. I already have the Service References interface working the way I want it.

Evidently, what I'm really asking is which is the better way to set up the address structures so that when  things are serialized/deserialized, customers will send and I will get the correct Address data into the correct Address object. In this app, I must know which is which. I can't just have two Address records and guess which is which. Also, there is the distinct possibility that I am over-thinking this W-A-Y too much.

I appreciate your help.
You can use both XML and JSON in requests, but you will need to specify the message format.

JSON example:

[OperationContract]
[WebInvoke(Method="GET", UriTemplate="/GetDataJson", 
    RequestFormat=WebMessageFormat.Json,
    ResponseFormat=WebMessageFormat.Json)]
string GetDataJson();

Open in new window


XML example:

[OperationContract]
[WebInvoke(Method="GET", UriTemplate="/GetDataXml", 
    RequestFormat=WebMessageFormat.Xml,
    ResponseFormat=WebMessageFormat.Xml)]
string GetDataXml();

Open in new window

Avatar of LandyJ

ASKER

Didn't answer the original question, but it helped overall.