Archive for November, 2007

30
Nov

Give It A REST! A WCF Love Story

When WCF was first released we all wallowed in its magnificent glory. It was so chocked full of features it would make any modest girl blush (including yours truly). But despite its voluminous power, some couldn’t help but feel like it was missing something. Something simple but extremely powerful. WCF thought these naysayers were crazy and insisted that not only was he all that, but that he also had a bag of chips.

One evening WCF was lounging at the bar, as he did most nights, and a beautiful little something in the corner caught his eye. She was clean and pretty, and appeared to be very successful and likable. In fact she was surrounded by an entourage of dread lock toting individuals who appeared to worship her like it was some kind of religion. WCF was besides himself in jealously and decided he needed to be a part of this circle of friends. So he walked up to the lady and asked for her name. “I’m REST” she said, “and these are the RESTafarians“. One thing led to another, and long story short, WCF and REST ended up hooking up. Who’s the winner of this match made in heaven? We all are :)

So the question is, how exactly can a developer begin to create RESTful services, or even append this new found functionality to existing ones? Thankfully enough its ridiculously easy (even I can understand it). What I’m going to develop is a service that will report what the currently playing song is in iTunes, which other people could call (for whatever reason). Making it RESTful adds volumes to the ease of use, and leaves the windows of possibilities wide open.

First I’m going to make a simple service contract:

wcf1.JPG

Obviously nothing special is going on here. So how do we make this thing work in a RESTful fashion?

wcf2.JPG

Notice the difference? All we did was salt the GetCurrentTrack method with a new attribute called WebGet. This attribute simply marks a method as being callable via an HTTP GET operation, namely a REST call. There are more properties for the WebGet attribute, which we’ll examine later, but for now suffice it to say that our service contract is now configured for accepting REST requests.

Now let’s create our service implementation:

wcf3.JPG

Once again, nothing special is going on here. The service simply calls a class to retrieve the current iTunes track. I’m not going to go into how I actually determine the playing track in this article, but if you’re curious you can examine the source code.

So now that we’ve got our service completed all we need now is to create our host, which we’ll implement as a console application for this demonstration:

wcf4.JPG

Notice that I’m not creating a ServiceHost but rather a WebServiceHost. This is a new class added in .NET 3.5 that serves the purpose of hosting WCF services that make use of the new HTTP GET/REST functionality. It is basically a wrapper of the ServiceHost that sets some property values so that we don’t have to.

The last peice of this equation is the configuration file:

wcf5.JPG

The only point of interest here is the binding being used. WebHttpBinding is a new binding added in .NET 3.5 specifically for HTTP GET/REST services.

And that’s it. Pretty simple huh? Surprisingly, we’ve only introduced 3 new classes: WebGetAttribute, WebServiceHost, and WebHttpBinding. Everything else is just plain old WCF. So let’s see this thing work. I start up the host and then navigate to “http://localhost:8731/GetCurrentTrack” in my browser:

wcf6.JPG

As you can see I’m currently getting my chill on to some wonderful Joy Wants Eternity. I change tracks and refresh:

wcf7.JPG

So you can see the service works beautifully. A client could easily subscribe to this and parse the XML at will.

Now what if we wanted this service to be exposeable to our fellow JavaScript enthusiasts out there? All we have to do is tweak the service contract:

wcf8.JPG

All we’ve had to introduce is a single enumeration to our arsenal, but the power of this service just went up ten fold. Let’s run the host and hit the service:

wcf9.JPG

With one ridiculously trivial change to our contract we just got JSON serialization absolutely free. Now anyone could easily use their favorite JavaScript library, make a RESTful call to our service, and use the returned object.

The last thing I’ll show in this article is the ability to add a URI template to your RESTful service contract. If you wanted to modify the URL of the service method (to make it prettier for instance) you can easily achieve this using the UriTemplate property of the WebGet attribute:

wcf10.JPG

By doing this, instead of navigating to “http://localhost:8731/GetCurrentTrack”, we can now navigate to “http://localhost:8731/ThisIsSoCool/IKnowRight/GetTrack”. Being able to build flexible, clean URLs for your method can add a lot to your service when needed.

What if we wanted to pass parameters to our RESTful service?

wcf13.JPG

The UriTemplate let’s us easily map method parameters to values from the incoming URL, so I could now call the service at “http://localhost:8731/ThisIsSoCool/IKnowRight/GetTrack/Jonathan” and it would fire the GetCurrentTrack method passing it “Jonathan”. Could it get any sweeter than that? I think not!

So WCF and REST went on to live a life of prosperity and joy, infecting .NET developers around the world with their intuitive coding model and flexible interface. People couldn’t be happier, and it would seem that their union couldn’t have been more timely. Was it your everyday love story? Not quite. What is better than Moonstruck? I’d sure like to think so…

Download the source code for this sample here.

29
Nov

ReSharper Quick Start

One of the few tools a .NET developer absolutely shouldn’t live without is ReSharper (R#), a Visual Studio add-in created by the wonderful people at JetBrains. It surprises me how many people I meet or talk to who either aren’t familiar with it, or haven’t taken the time to try it out. Well for all those people needing a little motivation, tonight I stumbled upon an absolute goldmine in the form of an extensive tutorial that showcases the power of ReSharper and why it truly is so beneficial. Joe White created 31 individual tutorials for individual features that collectively teach the majority of its functionality in this post. Anyone not currently using ReSharper owes it to themselves to spend the 30 minutes (unless you’re a speed reader) it takes to read it. I wish I would have known about this post when it was first made (January of this year), it would have helped me out a lot :)

A 30-day trial of ReSharper can be downloaded here.

If you’re running Visual Studio 2008 you can download a version of ReSharper compatible with it here.

28
Nov

Instacalc = Instasweet

It is few and far between that I find something on the internet that I genuinely think is a cool idea. Something that once you discover you immediately form an attachment to and wonder how you ever lived without it. Well I was trolling the net today and stumbled upon a very interesting and cool web application called Instacalc developed by Kalid Azad, who is a math guru and all around cool guy.

“What is Instacalc and why should I care?” you might ask. Well, imagine a calculator that spoke English and allowed you to formulate equations in a fluent and intuitive way. A calculator that understands the notion of currency and time and volume. A calculator that allows you to easily project your results into the form of a piechart or a bar graph. Sound awesome? If so then you’re going to love Instacalc.

Kalid has developed a ridiculously sophisticated calculation engine that is all JavaScript driven and responds immediately as you type. The way it works is you simply start typing your equation into a textbox and it evaluates it on the fly. You can do simple stuff like basic math like so:

You might say that there isn’t anything really special going on here, and you’d be right, but the beautiful thing about this is that typing this equation was substantially smoother in Instacalc than it would have been using a calculator software (like Windows calc) or an actual calculator.

The real power of Instacalc begins to show when you start to get creative with it. Suppose I’d like to know how long it would take to download a 30gb file if I had a 15mbps connection. Sure you could easily calculate that that out if you knew the proper formula, but why bother when I could do this?…

If you don’t think that is just cool as hell then either you don’t have a pulse or you can’t appreciate anything of value.

Now what if I got wrapped up with the wrong crowd, had a few too many drinks, one thing led to another and next thing you know I’m in deep with the Yakuza for 500,000 yen. How in the world am I going to pay that when I don’t even know how many dollars that equates to?

Hmm, well $4,300 sounds a lot better than 500,000. Thanks Instacalc!

Suppose I was offered a hundred bucks to chug down 1,000 teaspoons of vinegar oil. That doesn’t sound too bad. I mean, a spoonful is pretty small right? Well, maybe I should consult Instacalc about this first…

instacalc5.JPG

Umm….Yeah so I might as well sign my deathwish and spare us all the trouble because 5 liters of vinegar oil would wreck me pretty horribly. Luckily Instacalc had my back on this one.

Now I’m being offered a chance to participate in one of those charity runs that are for a good cause. My sponsor tells me all I have to do is run a million inches, which to be frank sounds like childs play :D I mean a million inches is nothing right? I’ve probably eaten that much in Subway sandwiches in the last month. Well, the day of the race comes and I decide to figure out exactly what my casual jog for charity will entail…

instacalc6.JPG

You know that feeling you get in your stomach when you know you’ve made a very poor decision but yet know there is no way out?…

At this point you may be asking yourself “Is there anything Instacalc can’t do?” and sadly the answer is yes. I attempted the following equation earlier today to no avail:

instacalc7.JPG

Why was I trying to figure out what my waist size would be in ladies jeans? That’s between me and Instacalc ;)

Seriously though, this is a really great and fun app to use/mess around with. It rivals Google’s calculator in terms of functionality, but I think is much nicer to use. The only thing that initially stood out to me that Google has over Instacalc is it’s ability to translate currency by country insead of currency name. For instance, with Google I could say “5 British pounds in South Korean money” without knowing what the name of South Korean currency is actually called. With Instacalc you have to know the currency name/abbreviation.

Anyways, this is just a smidgen of the features offered by Instacalc. I haven’t even gotten into variables and charting. Hopefully this gives you all enough incentive to go out and give it a shot.

25
Nov

DDD? MDD? WTF?

I got an email earlier today asking me for clarification on the differences between Domain-Driven Design (DDD) and Model-Driven Design, which I was elated to receive since I think that is a great question.

As I was about to formulate an extremely convoluted response touting my DDD prowess (not really) I realized that I bet the founding father of DDD, Eric Evans, has already crossed this bridge, and I was right.

I’ve gotten quite a bit of reception from readers showing interest in DDD patterns as well as general design patterns so I think I’m going to be putting together some material in coming weeks on this.

If anyone has any specific questions around this space they’d like me to cover please feel free to contact me otherwise I’m going to just pick a card out of a hat :)

24
Nov

Two Quick Notes

  1. Don’t let the last couple non-technical related posts fool you. Stay tuned in the coming days/weeks for lots of juicy .NET/software development related posts as that will be the focus of ths blog. I’ve just been trying to build up some cool content quickly to acclimate the new setup.
  2. I hope Kansas loses to Missouri tonight :)

Let it be known that I did call the Kansas upset this evening. Chase Daniels is just too damn good.