views:

460

answers:

1

I'm working on a Silverlight Project with all the features and limitations that entails. This is an update to a previous product. The intent, in order to be quick to market, is to maintain as much of the back-end (webservices, database, etc..) as at all possible. Our mandate it to only touch the back-end if there is no other way. We'll primarily be focused on re-writing the front-end. There's an important industry conference soon where we will want to demo the early look of the product. There may be time prior to the official release to do some re-work, but the back-end may need to wait until V2.

OK, so what I'm trying to do is use the MVVM pattern with data binding for the front-end for whihc I'm responsible (MVVM pattern is dictated from above). I have a pre-existig web service that serves up some XML. A sample of that XML looks like is below:

<CODEBOOKINDEX>
 <ME Words="1" Score="25" Highscore="1">Main Entry Item
  <NM>attack</NM>
  <NM>cardiac</NM>
  <NM>chest</NM>
  <NM>effort</NM>
  <NM>heart</NM>
  <NM>pectoris</NM>
  <NM>syndrome</NM>
  <NM>vasomotor</NM>
  <IE>413.9</IE>

  <M1 Words="1" Score="25">An M1 Item (Same as ME, just first level Child)
   <IE>557.1</IE>
  </M1>

  <M1 Words="1" Score="25">Another M1 Item
  <IE>443.9</IE>
   <M2 Words="1" Score="25">An M2 Item (again same as ME, just a child of an M1 item)
    <CF>Arteriosclerosis,extremities</CF>
    <IE>440.20</IE>
   </M2>
  </M1>
 </ME></CODEBOOKINDEX>

So, my question, since I want to bind this to a UI using the MVVM pattern, it seems to me that I need to translate this into a custom object. As you can see there are a number of "Entry" items, MainEntry (ME) and Subentries (M1 or M2 in this example), these will all contain certain other nodes (they will all have an IE node, for example), they MAY contain 0 or more other node types (for example they MAY or may not contain one or more NM nodes, or they MAY contain one CF node or not). Whihc means (at least to me) that I can't really bind directly to XML because:

  1. It violates the MVVM pattern (I could probably justify this for the demo, but would have to refactor later).
  2. I can't really bind a UI element to an XML node that MAY not be there for a given item.
  3. In some cases Ihave to translate a collection (a bunch of NM items, for example) into a formated strig for display purposes, which I don't THINK is a trivial thing.

So, I'm trying to understand the best way to translate this XML into a bindable object, which in my mind means transforming this XML into an object for the model and then overlaying a view-model on that model.

Can this be done easily with LINQ to XML queries, or am I really moving into the realm of an ORM such as NHibernate or Entity Framework (no holy wars about WHICH ORM please)?
I've only just established what controls I will be using for UI and I need to demonstrate to my manager rather quickly HOW I'm going to handle the translation.

So, the real questions:

  1. Do I NEED an ORM? I'm not against using them, but I want to keep the size of the XAP file small and want to limit the amount of new tech I (and my teammates) need to learn in a single pass.
  2. If I do need one, can I keep the file size down and can I ramp up quickly with either EF or NHibernatge and have a model to show very soon? I'm talking like a week here to have SOMETHING that will take output from the webservice and turn it into an object, even if the map isn't perfect initially, I need to demonstrate some progress.
  3. Is there another option alltogether that I'm not considering that might be easier, limit the need to modify existing code (i.e. the webservice) and product usable results?

As always, thanks to the community for your help.

Steve

+6  A: 

Do I NEED an ORM?

No. You aren't mapping to a relational source, so an object relational mapper won't help.


Get it done with Linq to Xml.

public CustomClass TranslateME(XElement source)
{
  CustomClass result = new CustomClass();
  result.Words = (int) source.Attribute("Words");
  result.Score = (int) source.Attribute("Score");

  XAttribute highScore = source.Attribute("HighScore");
  result.HighScore = (highScore == null) ? 0 : (int) highScore;

  result.NMs = source
    .Elements("NM")
    .Select(x => x.Value)
    .ToList();

  result.IE = source
    .Element("IE").Value;

  result.SubEntries = source
    .Elements("M1")
    .Select(x => TranslateM1(x))
    .ToList();

  return result;
}
David B
Thanks David. This helps me see how LINQ to XML will accomplish the task, which means I still need to get ramped up on it, but it seems to have what I need so I'll take the limited time I have to move in that direction.
Steve Brouillard