Reducing Duplication with Interfaces, Generics and Abstracts

The parts of our application (a long-term service and support system for the state of Maryland) that follow the DRY principle best tend to start with a combination of generic interfaces inherited by an abstract class that implements common functionality.  The end result–specific implementations that consist solely of a constructor.  I was able to accomplish this as well in one of my more recent domain implementations.  I’ve created a sample (using fantasy football as a domain) to demonstrate the ideas in a way that may be applied to future designs.

Let’s take the idea of a team roster.  A roster consists of players with a wide variety of roles that can be grouped this way:

  • QBs
  • offensive linemen
  • skill position players
  • defensive linemen
  • linebackers
  • defensive backs
  • special teams

Since I want specific implementations that are (very) small, I’ll need to find common attributes between these different groups.  Each roster grouping above is just a list of players.  Things that are common to all players (regardless of position) include attributes like:

  • first name
  • last name
  • team name
  • position

The first three attributes are just strings of text, so I treat them as such in my implementation.  Position could be treated that way too, but instead I’ll implement an enumeration with all the positions and leave the implementation of it to position-specific classes I’ll create later.  Going back to the roster grouping idea as a list of players, we can use a generic interface implemented by an abstract class so that implementations of the groups above will differ only by constructor.  Now, when I implement a Quarterbacks group,  the only differences between it and the implementation of an OffensiveLinemen group are the class names and types.  The RosterGroup class contains all the important functionality, including the IEquatable implementation that enables comparison of groups.  I followed a ReSharper suggestion to make IRosterGroup explicitly covariant.

 

How can I become a world-class coder in under three years?

I came across this question on Quora today and decided I would answer it.  There were enough up-votes by people following the question that I’m re-posting my answer below:

I’m not sure what the term “world-class coder” means to you.  But I would actively discourage the notion that there is some point you can reach in the practice of software development (whether it’s 3 years or 20 years) where you can look at yourself and say “Achievement unlocked!  I am a world-class coder at last.”  What may give you more satisfaction over time than the question of “where do I rank” in some mythical best coders on Earth list is “Am I a better developer now than I was last week?  Last month? Last year?”

The things that previous commenters have suggested are great ideas for continuous improvement and refinement of your skills in programming.  Do as many of those things as you can.  Beyond those, I’d suggest the following:

  • Be willing to learn from anyone.  I’ve made my living writing software since 1996 and I regularly learn new things about my craft from people a decade or more younger than me.

  • Keep track of what you learn–and share it.  Whether it’s through blogging, Stack Overflow contributions, or something else–write about it.  You may not encounter the exact problems you’ve solved in the future, but they will often be close enough that what you’ve captured will help you solve them much faster than you would have otherwise.  The ability to explain what you know to others is a very valuable and rare one.  The process of preparing to give a presentation to others on a topic has often been a good forcing function for me to learn that topic to the level where I can explain it well.

  • Learn about subjects beyond programming.  The importance of the ability to understand new domains well enough and deeply enough to design and implement useful software for them cannot be overstated.  I’ve delivered software solutions for news organizations, healthcare companies, marketing companies and defense/intelligence contractors in my career so far.  Making myself familiar with the sort of terminology they use and the way such companies operate (above and beyond a specific project) definitely results in a better end product.  One or more such topics can end up being great fodder for pet projects (which are great vehicles for learning things you aren’t already learning in a job).

Binding Redirects, StructureMap and Dependency Version Upgrades

Dealing with the fallout in failing unit tests from a code merge is one of the most frustrating tasks in software development.  And as one of a (very) small number of developers on our team that believes in unit testing, it fell to me to determine the cause of multiple instances of the structuremap exception code 207 error.

As it turned out, the culprit was a tactic I’ve used in the past to work with code that only works with a specific version of an assembly–the binding redirect.  When the same person is in charge of upgrading dependencies, this tends not to be an issue because if they’ve used binding redirects, they know it’s necessary to update them when dependencies are upgraded.    In this case, the dependencies were upgraded and the redirects were not.  As a result, StructureMap tried to find a specific version of an assembly that was no longer available and threw exception code 207 when it failed.

Replicating Folder Structures in New Environments with MSBuild

I recently received the task of modifying an existing MSBuild script to copy configuration files from one location to another while preserving all but the top levels of their original folder structure.  Completing this task required a refresher in MSBuild well-known metadata and task batching (among other things), so I’m recounting my process here for future reference.

The config files that needed copying were already collected into an item via a CreateItem task.  Since we’re using MSBuild 4.0 though, I replaced it with the simpler ItemGroup.  CreateItem has been deprecated for awhile, but can still be used.  There is a bit of debate over the precise differences between CreateItem and ItemGroup, but for me the bottom line is the same (or superior) functionality with less XML.

Creating a new folder on the fly is easy enough with the MakeDir task.  There’s no need to manually check whether or not the directory you’re trying to create already exists or not.  The task just works.

The trickiest part of  this task was figuring out what combination of well-known metadata needed to go in the DestinationFiles attribute of the Copy task to achieve the desired result.  The answer ended up looking like this:

<Copy SourceFiles="@(ConfigFiles)" DestinationFiles="$(OutDir)_Config\$(Environment)\%(ConfigFiles.RecursiveDir)%(ConfigFiles.Filename)%(ConfigFiles.Extension)" />

The key bit of metadata is the RecursiveDir part.  Since the ItemGroup that builds the file collection uses the ** wildcard, and it covered all the original folder structure I needed, putting after the new “root” destination and before the file names gave me the result I wanted.  Another reason that well-known metadata was vital to the task is that all the files have the same name (Web.config), so the easiest way to differentiate them for the purpose of copying was their location in the folder structure.

In addition to the links above, this book by Sayed Ibrahim Hashimi was very helpful.  In a previous job where configuration management was a much larger part of my role, I referred to it (and sedodream.com) on a daily basis.

Fixing MVC Sitemap Errors

When attempting to manually test a .NET MVC application, I got the following exception from Visual Studio:
MvcSiteMapException

Looking at the inner exception revealed this message:

An item with the same key has already been added.

The sitemap file for our application is pretty long (over 1300 lines of XML), but a co-worker pointed me to the potential culprit right away. There was a sitemap node near the end of the file that had empty strings for its controller and action attributes. As far as I can tell, this generates the default url for the site’s home page. Since it already exists, this results in the exception that’s thrown. Removing the sitemap node resolved our issue.  A couple of threads that I checked on stackoverflow (here and here) provide other possible causes for the error.

Identifying All Bad Mappings with AutoMapper

One of the long-running annoyances we’ve had with our test of AutoMapper configuration validity on my current project is that a test failure only revealed the first mapping that was wrong. I haven’t figured out why this is the case, but I’ve come up with a work-around that displays all the necessary information.

Because the exception thrown if one or more incorrect mappings is found is AutoMapperConfigurationException, my revised test catches that exception in order to print the source type, destination type, and the list of unmapped property names. Re-throwing the exception at the end ensures that the test still reports a failure. The XUnit test which demonstrates this is available as a GitHub gist. If you’re using NUnit or MSTest in your application, minor revisions to this test will give you the same results.

Visual Studio & TFS Behavior Tweaks

One of a few long-running annoyances I’ve had with every version of TFS is one of the default behaviors on check-in. The default is to resolve an open item on check-in, which is virtually never the case the first (or second, or third, etc) time you check in code to resolve a bug or implement new functionality. Fortunately, Edsquared has the solution.

After making this long-overdue change in my development environment, I exported the keys for VS2010 and VS2012 as registration entry files below:

Feel free to use them in your environment.

Freedom From Default Color Themes in Visual Studio 2012

I finally joined the ranks of those who’ve installed Visual Studio 2012 this week. The default Light color scheme is way too bright. The Dark color scheme is better, but the grays aren’t differentiated enough (just like the Microsoft Blend UI). Thankfully, some wonderful soul compiled this blog post, which details the changes necessary to save your eyes from the horrible default themes.

Following steps 1 and 2 will be enough, but you can go even further if you want the Visual Studio 2010 icons back in addition to the color scheme.

The Perils of Renaming in TFS

Apparently, renaming an assembly is a bad idea when TFS is your version control system.

Earlier this week, one of my co-workers renamed an assembly to consolidate some functionality in our application yesterday, and even though TFS said the changes were checked in, they weren’t.

I got the latest code the morning after the change, and got nothing but build failures. We’re using the latest version of TFS and it’s very frustrating that something like this still doesn’t work properly.

Ultimately, the solution was found at the bottom of this thread.

The only way I’ve found to avoid this kind of hassle is to create a new assembly, copy your code from the old assembly to the new one, change any references to the old assembly to use the new assembly, then delete the old assembly once you’ve verified the new one is working.