My first REAL dive in OData, very cool stuff!
Wednesday, October 24, 2012 10:01 PM
My first REAL attempt at trying, using, getting, and displaying data from an OData feed, it’s not as hard as I thought, check it out!
Last year I wrote a couple of conference apps for Windows Phone. In trying to be smart, I based the speakers and sessions off of XML files. Cool eh? Except I had to scour the conferences website for all that data. Speakers name’s (make sure you spell them right!), bios (don’t cut’n’paste the wrong ones!), social media links (did you find all of them?), etc. You get the idea, there’s a lot of work data mining that stuff and sore fingers of just mind numbing cut’n’pasting!
Well, for my next trick , I would like to try my hand at OData! That’s right! This time around, DevTeach is exposing speaker and session data over OData! Sweet! So, in other words, he’s going to do all the heavy lifting for me! Cool. There are some pros/cons with this.
|Have to learn how to use OData. ||Someone else is responsible for data entry. |
|Forced to use someone else’s schema/format/class structure and naming conventions. ||No worries about data accuracy. (well, not completely correct but for the most part, I’m assuming it’s accurate). |
|Some data will take time to retrieve over the wire (async). ||Focus on WHAT I want to do with the data, not on how to get it. |
|Using current XML files is intellectually easier to do (once you create the schema, figure out how to save the xmls into your project, load xml resources at runtime, parse them at runtime, etc). ||Is a GREAT excuse to learn about OData, using Linq and async. |
| ||Data updates are as far away as a refresh button (countless other schemes exist, on app load, after lock screen, once a day, etc). End result is data changes don’t require application updates. |
| ||Data more dependable. Nothing like “right from the horses mouth.” |
I’m sure you can come up with a few more pros/cons, but in my mind, I’m thinking the pros out weigh the cons. Yes, it was easier to do the early speakers/sessions info by hand, and I learned a lot. But one BIG flaw was when the data was updated……my app needed updating in the marketplace, maybe 4/5/6 days turn around time. Not “internet time” is it? However, THIS time, I hope to learn about OData and therefore have my data source updates when ever the user restarts the app, or when they hit the update button, something that’ll be MUCH less than a week! Sweet!
Yesterday I wrote my first foray into using OData with DevTeach’s feed. Today I would like to show JUST HOW EASY it is to play with the feed!
First off, create a new WPF application. Ya, ya, you COULD do something else, but we have to pick ONE, so that’s the one I picked (truth be told, I tried with a Console app yesterday but for various other reasons had all kinds of issues, today I did WPT and POOF, it worked!).
Next add a Service Reference to the DevTeach OData url feed.
Next type out the right URL: http://www.devteach.com/odata/odataservice.svc/, click GO.
After clicking Go, VS will spin a bit, then report back either an error (check your spelling, DOH! haha ya, happened to me too) then you’ll see DevTeachEntities in Services populate, GREAT!!!! Change the Namespace to something useful for later like DevTeachODataService. Hey, you can put GoHabsGoService in there if you like, just remember what it is.
Next, let’s get some data! In your MainWindow form you’ll need to new up a Loaded event like so.
Ok, adding the Loaded event isn’t rocket science, but neither is the next part….once you KNOW about it! In MainWindow_Loaded, I’m creating a new context variable of a NEW type of DevTeachEntities. HHHHMMM Did I tell you to type that out? Nooooooooo! So where did THAT come from?! GREAT question! Let us finish with this method first, I’ll get to it in a sec.
You’ll notice something also very interesting on that line. You will have to type out the OData feed URL again. That just kind of urks me! I JUST did that thirty seconds ago, and I need to do it again?! Why can’t I some how get to that URL I typed out? Another good question, THAT one I don’t have an answer for just yet. However, it looks like this might be done on purpose. The reason Shawn Wildermuth gives in his MSDN vid, is to allow for devs to redirect the url at runtime. I guess I can buy that, BUT I would still like to have the option to use that service reference I just added.
Ok, now we have our entities….hhhmmm sounds like EF a bit eh? Now that we have our entities, we can now get some data! COOL! At this point, we get very linq-ish and go get VSpeakers. Since we’re using WPF, we can just set the DataContext (ok, I’m cheating here a bit with glossing over data binding, but this is about getting OData feed data, not data binding).
Next, we need to create our UI.
It’s a real simple, automatic column generating grid which will dynamically be the size of the form. Again, not focusing on XAML/DataGrids here, just enough to get us so we can F5 the project. Once you run it, BINGO!!!!!!
Now you’re able to see data! BEAUTIFUL!!!!!!!!!!!!!!!!!!!!! Very cool!
Uh, Peter…….(tap tap tap on the shoulder)….you said you would get back to that question about where the heck did DevTeachEntities come from? GREAT question. When we told Visual Studio about the OData URL, it went out and interrogated the web services and created some proxy classes. Don’t believe me? Check out the directory structure and some of the files in behind the scenes in your project.
Notice the service.edmx….looks very EF-ish eh? The Reference.datasvcmap file contains the URL itself as well as a pointer to the EDMX file. And the References.cs file is the gold mine we’re looking for!!!!!!!!!!!!
This is the file which VS created for us which is our proxy code for what comes back from the OData feed so we can use it natively in our .NET code as….well….. .NET/C# objects and strongly typed objects at that! I encourage you to look at those files, if only to see what magic VS is doing behind the scenes for us.
One last note before I go. The smart dev will see one hiccup with this. Since the proxy code is already coded, what happens if, or more likely, when, the OData feed changes….uh, how do I update my proxies? GOOD question.
Goto the specific OData Service Reference, right click it, and select Update Service Reference. You’ll see a dlg popup, a few things fly by and then you’ll have the most up to date proxies!
A Bonus tip? Now that you know how to get some data from the feed, how do you explore it some more? VS comes to the rescue here with Object Browser. See in that pic above, right below Update Service Reference, there’s View in Object Browser? Click that and now you can poke around and start mining for other golden nuggets!
Now that we know how to get some OData from DevTeach, it’s time to play around with it some more, it’s also time to grab a coffee and get coding!
PCHenry.com: Are you getting into OData but getting STUCK in IE?
DevTeach OData: http://www.devteach.com/odata/odataservice.svc/
DevTeach Speakers OData Feed: http://www.devteach.com/odata/odataservice.svc/VSpeakers
Jean-Rene Roy,SoftDesign Technologies, MVP: http://www.softdesign.ca/Blog/
Open Data Protocol: http://www.odata.org/
MSDN: Consuming OData using .NET by Shawn Wildermuth