A Brief Introduction to LINQPad
I presented a brief talk on LINQPad at RockNUG a couple of weeks ago. This post will elaborate on that presentation a bit, since the 30 minutes I had wasn’t nearly enough to do justice to a tool I only half-joking called the Swiss Army knife of .NET development.
In addition to enabling developers to write and execute LINQ queries, LINQPad can be used to execute SQL queries as well as compose and run code snippets in C#, VB.NET, and F#. LINQ can query a wide variety of collections and data, including SQL databases and XML. The ability to query XML with LINQ becomes quite powerful when the XML comes from WCF Data Services (an implementation of the OData protocol).
During my presentation, I queried a local version of the Northwind database, as well as the OData endpoint of Netflix. StackOverflow publishes their data via an OData endpoint as well. Additional producers of OData services can be found here, or in the Windows Azure Marketplace.
One of the nice features of LINQPad is the number of export options it provides for your query results. The results of any SQL or LINQ query written in LINQPad can be exported to Excel, Word, or HTML. The Excel and Word export capabilities give you the option of preserving the formatting LINQPad provides, or leaving it out. Once you’ve queried a database with LINQ, the results display allows you to toggle between the equivalent fluent LINQ, SQL and MSIL syntax. I demonstrated this feature by executing a SQL query against the Northwind sample database, then cutting and pasting the equivalent syntax to new query windows and running them to show that the query results were the same.
The LINQPad website pitches the tool as a replacement for SQL Server Management Studio. To test this proposition, I demonstrated LINQPad’s ability to execute stored procedures. I used the TenMostExpensiveProducts stored procedure in the Northwind database, and this script to show one way to use LINQPad to run stored procedures that take parameters.
LINQPad’s capabilities as a code snippet runner are further supported by its ability to reference custom assemblies and namespaces. So instead of dealing with all the overhead of Visual Studio just to write a console application, you could simply write them in LINQPad and reference any custom assemblies you needed.
The latest version of LINQPad also has an extensibility model, if you wanted to query databases other than SQL Server (or different querying sources).
One feature I wished I’d had time to delve into further was LINQPad’s ability to query Entity Framework models defined in Visual Studio. There’s a brief description of that capability here.
All of the query files from my presentation are available on Github.
Mr. Daisey and the Apple Factory
If you haven’t already heard this episode of This American Life, it’s definitely worth your time. I won’t look at any of my “iStuff” the same way again after hearing it. The suicides at Foxconn made the news last year (along with a mass suicide threat earlier this year), but this piece gives a lot of insight into the conditions that could drive people to kill themselves.
I found it difficult to listen to this piece and not feel complicit in how the workers at these plants are treated. I wish I knew how much more per product it would cost to improve working conditions (and hope I’d be a decent enough human being to pay extra).
Ninja UI
Since yesterday’s post about my goals for next year, I heard from my friend Faisal about a jQuery plugin he’s been working on called Ninja UI. It’s on github, so I’ll definitely be checking it out as part of my learning of jQuery for next year. Going beyond using open source tools to being a committer on one would be a big step forward for me.
Another Year Gone
It’s annual review time again, which means this year has gone by even more quickly than usual. Filling out my self-assessment was a good reminder of all the work I had a hand in completing. I’m still deciding on goals for 2012, and I’m posting all of them here so I can look back on them over the course of next year and track my progress.
- Learn jQuery. I got a bit of exposure to it this year through a couple of projects that I worked on, and a .NET user group presentation or two, but haven't done the sort of deep dive that would help me improve the look-and-feel of the web applications I build and maintain.
- Learn a functional programming language. I've been thinking about this more recently since some of our work involves the implementation of valuation models in code. I also came across this article in the November Communications of the ACM advocating OCaml. Since I work in a Microsoft shop, picking up something like F# might have a slightly better chance of making it into production code than OCaml or Haskell. Part of my objective in learning a functional programming language is to help me recognize and make better use of functional techniques in a language like C#, which has added more and more support for the functional programming style of the years.
- Give a few technical talks/presentations. This year, I presented on NuGet at my job, and on Reflector at RockNUG. Having to present on a tool or technology to group has always been a great incentive to do some deep learning of a subject. It's also a chance to exercise some speaking skills (which developers need a lot more than they might think in order to be successful) and to handle a Q & A session. I haven't developed any new presentations yet, but some prospective topics include: LINQPad, elmah,
- Take more online training. We have access to Pluralsight .NET training through work. I watched quite a few of their videos over the course of the year. 2012 shouldn't be any different in that respect. I recently came across free webcasts on a variety of topics from DevelopMentor. Since they're downloadable as well as streamable, I'll definitely use my commute to watch some of them.
- Write a compiler. It's been awhile since I've cracked open "the dragon book", so I'm probably overdue to exercise my brain in that way. I found that suggestion (and a number of other very useful ones) here.
- Practice. I'd heard of the "code kata" idea before, but hadn't really explored it. Dave Thomas of Pragmatic Programmers has nearly a couple dozen here.
LINQ Aggregate for Comma-Separated Lists of Values
A couple of days ago, while pairing with my colleague Alexei on bug fixes to a new feature, we came across a bit of code that attempted to take an integer array and construct a string with a comma-delimited list of the numbers from it. The existing code didn’t quite work, so we wrote a basic for-each loop and used ReSharper to see what LINQ alternative it might construct. Here’s what ReSharper came up with:
int[] numbers = new[] {1, 5, 8, 26, 35, 42};
var result = numbers.Aggregate("", (current, item) => current + item.ToString() + “,");
Before ReSharper served this up, I wasn’t familiar with the Aggregate operator. When I checked out 101 LINQ Samples for it, the vast majority of the examples used numbers.
AppleScript + RSVP Emails = Weddings Guests Address Book Group
I’ve been using Macs as my primary home computers for about seven years now, but hadn’t developed an interest in using AppleScript until very recently. I’m getting married in about six weeks, and my fiancee and I set up an e-mail address where everyone we invited to the wedding and reception could RSVP. In retrospect, figuring out an Apple Mail rule (or rule + AppleScript) ahead of time would probably have been a better idea, but I didn’t think of it until after I had dozens of RSVPs and no convenient way to respond to the guests en masse with additional wedding information, hotel arrangements, parking, etc. So I thought I’d figure out just enough AppleScript to go through our RSVP e-mail box and build an address book group out of the e-mails we received.
With an assist from someone on stackoverflow.com, I came up with a script that did the job. I’ve made it available as a gist on GitHub.
There are probably a ton of ways to improve this script, but for what I needed, this does the job.
About that rapture thing
It’s May 21, and we’re all still here. Not that I expected a different outcome, but there’d been a lot of buzz in the press lately about the latest prediction of the end of the world.
The rapture was even a topic at work on Friday–in jest at first, but more and more serious as the conversation went on. A few of us (two of us Christian, one a sort of agnostic) talked about the crucifixion, the resurrection, the last supper, even Christ in the garden of Gethsemane.
Today at church, our pastor spoke at length on the reactions he’d heard to those who believed the world was ending today–most of them negative. He didn’t agree with their theology, but respected their willingness and seriousness in acting on their faith and sharing it with others. As Seventh-day Adventists (a denomination that came out of a movement that believed Christ would return in 1844), he reminded us that we should be more sympathetic to those with beliefs different than ours.
Introducing NuGet
Today at work, I gave a presentation on NuGet. I’ve suggested they consider it as an option to ease management of the open source dependencies of our major application, so it was natural that I present the pros and cons.
NuGet is a system for managing .NET packages. It’s not unlike RubyGems or CPAN (for Ruby and Perl respectively), and while it has some work to do to be on par with those alternatives, they’re off to a very good start. Today’s presentation focused on just a few the capabilities of NuGet, and I’ll recap a few from my presentation in this post.
The primary use case for NuGet is the management of open source dependencies in a .NET application. There are a number of key open source libraries that .NET developers like me have been using in projects for years. Upgrades were always a pain because of having to manage their dependencies manually. Many of these tools (NHibernate, NUnit, log4net, and more) are already available as NuGet packages at the NuGet Gallery. I used NHibernate and NUnit in my examples today. Another tool that proved quite useful in my demo was the NuGet Package Explorer. Some of its features include:
- Opening and downloading packages from remote feeds
- Opening local packages to view and change their metadata and contents
- Creating new packages (instead of fiddling with XML manually)
I wrapped up my presentation with two different examples of building NuGet packages without a manually-created .nuspec file as a starting point. The documentation provides examples of how to generate a .nuspec file from an existing DLL, and how to generate a NuGet package from a .csproj or .vbproj file. I published the rules engine (which I found in an answer to a stackoverflow.com question), and a test assembly I created to the NuGet Gallery earlier this evening. If you want to check them out, just search for Arpc.RulesEngine in the NuGet Gallery. I still need to publish the rules engine source as a package and/or push it to a symbol server. Once the enterprise story for NuGet becomes a bit clearer, I hope I have an opportunity to present on that as well.
Practical MVVM
Last Wednesday night, I attended a talk at RockNUG on MVVM by Joel Cochran. It’s the best explanation of the Model-View-ViewModel design pattern that I’ve seen so far. I found his talk particularly useful because he focused on the fundamentals of the design pattern instead of a specific framework (of which there are many).
Cochran’s talk was good for second and completely unexpected reason–his use of Mercurial for his code demos. I’ve been to my share of conferences and user groups and seen a lot of demos, but before that talk, I’d never seen a speaker eliminate the inevitable typos and otherwise speed up his presentation that way. When there was some code he wanted to show that exhibited an aspect of his Practical MVVM Manifesto, he simply grabbed a commit from his local Mercurial repository and updated the code in place. The next time I give a talk or do any demos, I hope I can make good use of that trick too.
Wedding Website Up
Here it is: http://ebandscott.tying.the.knot.org/
Thanks again to my friend Sandro for setting this up.
Ballet
This year is the second in a row I’ve gotten to go to a Kennedy Center performance. Last year, my girlfriend (now fiancée) and I went with another couple to see Porgy & Bess. This past Thursday, it was the Alvin Ailey American Dance Theater. Having never seen a ballet before (or anything called modern dance for that matter), I had no idea what to really expect. It turned out to be quite a fun time.
Of the four works they performed (all of which were impressive), my favorite parts were the solo “The Evolution of a Secured Feminine”, and other dances within “Revelations” (the company’s signature work) that had no more than a handful of dancers. I suppose that no one goes to the ballet for the music, but with Duke Ellington music as the backdrop for one of the pieces and gospel for some of the others, I certainly enjoyed that as much as the dancing.
.NET Reflector--No Longer Free, But Still Worth It
Those of us who registered with red-gate.com after they bought Reflector from its creator, Lutz Roeder, got an e-mail on February 2 saying the next version of Reflector would no longer be free. It’s the second time in my recent memory that a free and/or open source package for .NET became closed. The first one was NCover, which was probably the best option out there for determining how much of your codebase was covered by unit tests. Even at a price with subscription of $658, it may be a more cost-effective option than paying for the Visual Studio sku that includes team testing tools.
By contrast, the entry-level price for .NET Reflector is relatively low ($35). As a tool, I think it’s valuable enough to the every-day .NET developer to spend their own money for.
(Finally) Engaged
Back on January 14, I turned my girlfriend Ebony into my fiancée :-) The next day, we went cruising for a week in the eastern Caribbean with some friends (more on that in another post).
My buddy Sandro is setting up another website that will have all the particulars about the wedding.
Introducing .NET Reflector
I gave a presentation on .NET Reflector at the January 12 meeting of RockNUG. I took most of my time demonstrating the product and answering questions, so I had very few slides. So instead of simply posting them here and calling it a day, this blog post will incorporate some of the Q & A that happened during the presentation.
What Is It?
The title slide in my presentation called .NET Reflector an “x-ray machine for (unobfuscated) .NET assemblies”. That little phrase actually understates what .NET Reflector can do. Decompiling .NET assemblies is only one of the three major things it does (and is probably the best-publicized of its capabilities). This tool also provides an excellent class browser and static analysis capabilities.
Why Is It Helpful?
In addition to giving developers to see code for third-party assemblies when they don’t have access to their source files, it can be quite a useful learning tool for seeing what the compiler generates on-the-fly for the source code we write ourselves.
Demos
One of the first features I demonstrated was the ability of .NET Reflector to decompile an assembly into multiple .NET languages. C# is the default, but VB.NET, Delphi, Oxygene, MC++, F#, and MSIL are the other target languages available out of the box. Using the add-in architecture of .NET Reflector, one developer added limited support for decompilation to PowerShell (download the PowerShellLanguage.zip file).
Around this point in the presentation, someone asked if you could cut-and-paste decompiled source into a file. Not only does it work, but with Denis Bauer’s Reflector FileDissassembler plug-in installed, you can decompile an entire assembly into its constituent source code files (though I suspect that Red Gate Software would prefer that you pay for .NET Reflector Pro to get this capability).
I was also able to demonstrate the much-overlooked static analysis capabilities of .NET Reflector. They enable developers to learn what code depends on particular methods, where they’re used, what code exposes them, and where they’re called. It turns out that there’s a free plug-in which extends this capability even further. The Dependency Structure Matrix (DSM) plug-in allows developers to generate and manipulate matrices for determining the level of complexity in a software architecture. Considering that a full-featured tool with DSM capability like NDepend costs hundreds of dollars per seat license, even a fraction of those features from a free .NET Reflector plug-in are well worth the time to learn how to leverage.
More Q & A
When I showed that .NET Reflector pulled in source code comments in its display of disassembled code, one member of the audience pointed out (correctly) that this was only possible because the XML file containing the comments was co-located with the assembly. When I tested the disassembly capability afterwards without the XML file present, the comments didn’t display.
There was also a question from the audience about how .NET Reflector compared with ILDASM (which ships with the .NET Framework). The short answer is that ILDASM is far more limited by comparison. It only decompiles to IL, it lacks the analysis capabilities of .NET Reflector, and most importantly it doesn’t have a plug-in architecture to enable expansion of what it can do.
Conclusion
My presentation on January 12 only scratched the surface of what .NET Reflector can do. I hope this post has added more depth, and piqued your curiosity to use the tool to improve your own development experience. You may find the following links helpful in leveraging .NET Reflector for your own development work:
- Red Gate (http://www.red-gate.com/products/dotnet-development/reflector/)
- Wikipedia (http://en.wikipedia.org/wiki/.NET_Reflector)
- Stackoverflow (http://stackoverflow.com/questions/408167/examples-of-using-net-reflector)
- Codeplex (http://reflectoraddins.codeplex.com/)
- Oxygene (http://en.wikipedia.org/wiki/Oxygene_(programming_language))
- C# in Depth (http://csharpindepth.com/Downloads.aspx)
- Jon Skeet (http://msmvps.com/blogs/jon_skeet/Default.aspx)
- Snippy Reflector Add-In (http://msmvps.com/blogs/jon_skeet/archive/2008/11/23/the-snippy-reflector-add-in.aspx)
Candied Sweet Potatoes
Last month (for Thanksgiving), my dad, sister, a few cousins and I cooked so mom could have a break. The food turned out so well, she asked us to do it again for Christmas–including a candied sweet potato recipe I cobbled together from different ones on the internet. The recipe is below.
Ingredients 4-5 lbs of sweet potatoes 1 20oz can of crushed pineapple 1 1/2 cups of dark brown sugar 1 1/4 cup of I Can’t Believe It’s Not Butter cinnamon, nutmeg, & ginger to taste
Instructions Fill an 8qt pot 2/3rds of the way with water. Bring to a boil. Boil the sweet potatoes for 15 minutes. Drain and set aside to cool for peeling and slicing later.
In a saucepan, melt the I Can’t Believe It’s Not Butter, add the brown sugar and stir. Add cinnamon, nutmeg, and ginger until the sauce tastes how you want it.
After you’ve peeled and sliced the sweet potatoes, lay down a layer of them, drizzle/smear on some of the sauce, and sprinkle on some of the crushed pineapple. Repeat until you’ve filled whatever casserole dish or foil pan you’re using.
Bake this at 425 degrees for 90 minutes.
The problem with exit interviews
The biggest problem with exit interviews is that they’re too little, too late. I had an exit interview recently (since I accepted an offer to go elsewhere), and there wasn’t anything wrong with the questions–it was just that nothing could be done about any of the concerns I raised.
The second major problem with exit interviews is that they focus too narrowly. All the feedback from exit interviews comes from people who’ve decided to leave. Assuming a company has had relatively low turnover for awhile, the feedback could be leaving out information from as much as 90% of its workforce.
If a company is serious about employee retention, they need to get feedback from as much of their workforce as possible on a regular basis. In my exit interview, I got questions about benefits, commute, holidays, and other issues. Regular, anonymous surveys on those issues would probably reveal a lot of useful information about ways benefits could be improved. Gathering this kind of information regularly will mean that at least some (if not most) of the answers you get will be from people who still have a stake in the company’s future.
Deleting TFS Tasks
I changed the state of a TFS task I was working on recently, only to discover the workflow wouldn’t let me return it to it’s prior state. Until today, I didn’t know it was possible to delete TFS tasks if you made a mistake in changing one. But some Googling revealed a blog post that explained how to delete tasks.
The direction of the slashes for the URL can point forward (/) instead of backward () and the witadmin.exe destroywi /Collection:<TFS url> /id:<Task id> command still works.
Filtering Heterogeneous Arrays in .NET
One of the bugs I was recently asked to fix for an application required me to determine whether or not to display one of the members of a list. This proved somewhat challenging since the lists in question were heterogeneous (two difference subtypes of an abstract base class). It turned out that LINQ provides a nice solution to this sort of problem in the form of the OfType<T> method.
Given an IEnumerable collection with elements of multiple types, calling OfType<T> on the collection where T is the desired type will return a collection containing only elements of type T. Before learning about OfType<T>, I’d been using the Cast<T> method. This was fine as long as all the collection elements were of the type T I wanted. The moment this wasn’t the case, my LINQ query threw a cast exception. OfType<T> seems to work similarly to the “as” operator in C#, in that it doesn’t complain if a list element isn’t type T–it simply excludes it from the returned collection.
PowerGUI and .NET Framework 4.0
On my current project, we use PowerShell scripts to automate our UI testing. We’ve been writing and running the scripts in the PowerGUI Script Editor, an excellent tool that’s also free. When we upgraded our application to run on version 4.0 of the .NET Framework from 3.5, we lost the ability to run PowerShell scripts in debug mode from PowerGUI.
The only work-around for this I’ve found (at least until a version of PowerGUI built on .NET 4.0 comes out), is a registry hack that forces all the .NET apps on the machine to use the latest version of the CLR. You can find more details in this user discussion at powergui.org, or this discussion on stackoverflow.com.
New MSBuild 4.0 Features
My current assignment has me working with the application build again. MSBuild 4.0 got a number of new features which I’m only now getting to take advantage of. The coolest one so far is the extensible task factory. It lets you define custom MSBuild tasks right in the project file, instead of having to create a separate DLL for them. Andrew Arnott wrote a custom factory that lets you define custom MSBuild tasks using PowerShell. Because we’re using PowerShell for UI automation testing of our application, we’ll soon be able to integrate those tests into our build system.
Another feature I just learned about is conditional constructs. They provide functionality equivalent to a case statement you might see in other languages. It’s much more flexible than the Condition attribute available on most MSBuild tasks.