0

I have a WCF SOAP webservice which communicates with ip devices, each device gives me a message which includes the types of every variable they are going to use:

  <tns1:Device wstop:topic="true">
    <tnsavg:Logs wstop:topic="true">
      <tt:MessageDescription IsProperty="true">
        <tt:Data>
          <tt:SimpleItemDescription Name="BaseId" Type="xsd:integer"/>
          <tt:SimpleItemDescription Name="BootCount" Type="xsd:integer"/>
          <tt:SimpleItemDescription Name="SystemLogId" Type="xsd:integer"/>
          <tt:SimpleItemDescription Name="AccessLogId" Type="xsd:integer"/>

I might get about 20 to 30 different items, which use mainly int, boolean, and string types. This is and example of the message i can receive:

<wsnt:Message>
  <tt:Message UtcTime="2011-01-01T00:02:14.946Z">
    <tt:Source/>
    <tt:Data>
      <tt:SimpleItem Name="BaseId" Value="3332536653"/>
      <tt:SimpleItem Name="BootCount" Value="2525"/>
      <tt:SimpleItem Name="SystemLogId" Value="73"/>
      <tt:SimpleItem Name="AccessLogId" Value="9"/>
    </tt:Data>
  </tt:Message>
</wsnt:Message>

The main problem here is that i want to show this messages in a property of my controller but i do not know a way of showing them so that the person who is going to use the controller can use them to get information in an easy way. I had thought of creating a class which shows the items in this way:

 public class DataClass
    {
        public string Name;
        public string Value;
        public string Type;
    }

Other option i thought of was using ExpandoObjects, but that means the user would have to hardcode everysingle option, and there might be 40 different messages per device the controller handles.

Is there a better way of doing this so that the controller user can implement it in a easy way?

  • It appears your DataClass is a Data Transfer Object (DTO)--which is a recommended pattern for doing what you've asked. Having said that; I've taken to using things like Expando (or the use of the dynamic keyword) to dynamically generate objects with the properties the message requires to avoid hand-crafting many DTO classes. – Peter Ritchie Mar 11 '15 at 13:49
  • I would use Expando if the number of items with 3 devices was not 120. That means a lot of work for the one implementing the controller. I guess i will stick to DTO, seems the more reasonable, even if my boss will keep thinking it is too complex an object :( . – Borja Güiles Quintana Mar 11 '15 at 14:26
  • Could always code-gen the DTOs, if there's meta-data you can use... – Peter Ritchie Mar 11 '15 at 14:43
  • I thought of that, but if i generate code for each different message, how do i map adterwards each message to its object? i would have to code every message to the object it needs right? – Borja Güiles Quintana Mar 11 '15 at 14:46
  • If the mapping can be convention-based (i.e. both have same property name) http://automapper.org/ – Peter Ritchie Mar 11 '15 at 14:49
  • Will that work if some objects have the same properties but different object name? ej: TempInside and TempOutside have the same properties inside. – Borja Güiles Quintana Mar 11 '15 at 14:59
  • It should, you can have custom mapping if you need to. – Peter Ritchie Mar 11 '15 at 15:05

0 Answers0