From Web Forms to MVC

In the weeks since my last post, I’ve been thrown into the deep end of the pool learning ASP.NET MVC 3 and a number of other associated technologies for a healthcare information management application currently scheduled to deploy this July.  Having developed web applications using webforms since 2003, I’ve found it to be a pretty significant mental shift in a number of ways.

No Controls

There are none of the controls I’ve become accustomed to using over the years.  So in addition to learning the ins-and-outs of MVC 3, I’ve been learning some jQuery as well.

No ViewState

Because there’s no viewstate in MVC, any information you need in more than one view should be available either in the url’s query string, the viewmodel, or be retrievable via some mechanism in your view’s controller.  In the application I’m working on, we use Agatha.

More “Pages”

Each CRUD operation gets its own view (and its own viewmodel, depending on the circumstance).  This actively encourages separation of concerns in a way that webforms definitely does not.

A Controller is a Lot Like Code-Behind

I’ve been reading Dino Esposito’s book on MVC 3, and he suggests thinking of controllers this way fairly early in the book.  I’ve found that advice helpful in a couple of ways:

  1. This makes it quicker to understand where to put some of the code that does the key work of the application.
  2. It’s a warning that you can put far too much logic in your controllers the same way it was possible to put far too much into your code-behind.

Using Agatha has helped to keep the controllers I’ve written so far from being too heavy.

More to Come

This barely scratches the surface of my experience with MVC so far.  None of the views I’ve implemented has been been complex enough yet to benefit from the use of Knockout JS, but future assignments will almost certainly change this.  We’re also using AutoMapper to ease data transfer between our domain objects and DTOs.  In addition to using StructureMap for dependency injection, we’re using PostSharp to deal with some cross-cutting concerns.  Finally, we’re using RavenDB for persistence, so doing things the object database way instead of using SQL Server has required some fundamental changes as well.

Inserting stored procedure results into a table

Working with one of my colleagues earlier today, we found that we needed a way to store the results of a stored procedure execution in a table. He found this helpful blog post that shows exactly how.

One thing we found that the original blog post didn’t mention is that this approach works with table variables as well.  A revised example that uses a table variable is available as a gist on GitHub.

Saving Changes is Not Permitted (SQL Server 2008 R2)

We just upgraded our development VMs at work, and I got bitten by one of the more annoying default settings in SQL Server Management Studio again. I imported some data for use in some queries and needed to change one of the column types. But when I tried to save the change, I got the dreaded “Saving changes is not permitted.” error.

Fortunately, this blog post directed me to the setting I needed to change in order for SSMS to do what I wanted.

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.

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.

  1. 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.
  2. 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.
  3. 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,
  4. 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.
  5. 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.
  6. 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.

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)

In addition to getting packages from Microsoft’s official feed, it’s possible to create your own feed of NuGet packages.  Phil Haack has a post on how to do this.  My demo for work only included a local folder feed source, but I tried Haack’s instructions at home and they worked quite well.

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.