Sunday, October 28, 2007

Give LINQ a REST

We recently upgraded to FogBugz 6, which sports a new REST styled API.  I decided to play with it by writing a simple .NET client using VS 2008, and take LINQ-to-XML for a spin.  The center piece of LINQ-to-XML is the new XDocument class, which is yet another way to work with XML data in .NET:

 

return XDocument.Load(urlForXmlData);

 

What makes this differ from XmlDocument, XmlReader, etc., is that a LINQ provider has been written around it:

 

var cases = from c in doc.Elements("cases").Elements("cases")

            where c.Element("sTitle").Value.StartsWith(searchText)

            select c;

 

Beyond that, it’s plain easier to use, in my opinion:

 

if (responseElement.Element("error").Attribute("code").Value == "2")

 

With that, it’s fairly easy to come up with a method to send commands and read XML:

 

private XDocument SendCommand(string commandFormatString, params object[] parameterValues)

{           

    var commandUri = this.FogBugzApiUrl.ToString();

    commandUri += String.Format(commandFormatString, parameterValues);

    return XDocument.Load(commandUri);

}

 

This method can then be used to work against the REST API:

 

public bool LogOn(string email, string password)

{

    var responseElement = SendCommand("cmd=logon&email={0}&password={1}", email, password).Element("response");

    if (responseElement.Element("error") == null)

    {

        this._token = responseElement.Element("token").Value;

        return true;

    }

    else

    {

        return false;              

    }

}

 

Where it gets interesting, though, is using LINQ and inline initialization to new up strongly-typed wrappers around the responses:

 

public IEnumerable<Case> GetCases(string search)

{

    var response = SendCommand("cmd=search&q={0}&cols=ixBug,sTitle", search);

    var cases = from c in response.Elements("cases").Elements("case")

                select new Case() { Id = int.Parse(c.Element("ixBug").Value), Title = c.Element("sTitle").Value };

    return cases;

}

 

Hopefully, you can see how powerful LINQ and these other new features in .NET 3.5 are.  In a few lines of code, we can query a REST API and return strongly-typed wrappers around the content.

Tuesday, October 16, 2007

Sandcastle Tip

Sandcastle has come a long way from the early CTPs. It still lacks the slick GUI of good old NDoc, though 3rd party apps provide similar functionality. But really, Sandcastle is designed to be integrated into your build process. The latest CTP ships with a batch file, CompileHelp.bat, that will help you do just that, but there are a few quirks. If yours isn’t outputting any help, or if you’re getting path-related errors, check the following:
  • When supplying the assembly path, leave off “.dll”
  • Call CompileHelp with the current path set to the same folder as your assembly
So, a simple example looks like this: cd c:\projectdir\bin\release CompileHelp.bat vs2005 assemblynamewithoutdotdll

Friday, October 12, 2007

Open Letter to Visual Studio: Please, Please, Fix This Window

Dear Sara, Scott, Scott, and Redmond,

VS2005 made great strides in UI usability for developers. VS2008 makes even more. Microsoft clearly gets this stuff now, and is cranking out don’t-make-me-think UIs like Steve Jobs’ mama. But, somewhere deep in the halls of Redmond sits a developer on the Visual Studio project who answers to no UI Guru, no Usability Cop, no Design Guideline. He wires up his beloved window to SqlDataSource -> Select Query -> Query Builder…, ensures no pane is left usable, turns off ‘maximize’ and ‘persist size’, then clocks out and goes home.

Sara, as a fellow Mississippian, and .NET dev, you owe this to the Magnolia State. Scott and Scott, you’ve thrown yourselves under the train in the community as champions of us little morts banging away code the best we know how. Microsoft, you invented the GUI and Ribbon Interface. Please, Please, Fix This Window:

Sincerely,

Daniel

kick it on DotNetKicks.com

Tuesday, October 2, 2007

Apple iPhone Review - .NET Developer Edition

After the price drop, I got an iPhone “for my wife”.  My 8525 has most of the features of the iPhone and then some.  It has 3G and can stream internet radio.  I have a choice of 3 browsers for it, thousands of other 3rd party apps, and I can write code using the same language I use for my day job.  But I can still do more with less effort on the [wife’s] iPhone.

 

It all comes down to different philosophies on software and hardware.  Apple dictates with iron fist the entire platform from hardware to software, providing developers a limited API for customization.  Microsoft sells the OS and guidelines for hardware, and tools to let developers do just about anything possible with the devices.  Each approach has its merits.  I can sense frustrated Apple developers guiltily sneaking peeks at the .NET Compact Framework after hours, and what .NET dev doesn’t seem a little depressed with their now bulky, unpretty clunker of a phone?

 

Even so, the Windows Mobile platform isn’t dead.  I suspect Microsoft has more than a few developers busy coming up with an answer to the iPhone.  In the meantime, my recommendation to .NET developers considering an iPhone?  Read up:

http://developer.apple.com/documentation/iPhone/Conceptual/iPhoneHIG/

http://developer.apple.com/documentation/iPhone/Conceptual/iPhoneHIG/iPhoneHIG.pdf

Monday, October 1, 2007

Surface

More on Microsoft Surface, including some close up vids of the platform in action.

http://arstechnica.com/articles/culture/surface.ars/1

 

I was a little disappointed that the initial stuff is all in XNA, which is Microsoft’s gaming platform, as opposed to WPF.   Also disappointing is what looks like a pretty closed environment in marketing and developing these.  They’re still saying $5 – 10k, but the initial rollout seems very limited.