Introducing AutoPoco

I first learned about AutoPoco from this blog post by Scott Hanselman.  But it wasn’t until earlier this spring that I had an opportunity to use it.  I’d started a new job at the end of March, and in the process of getting familiar with the code base on my first project, I came across the code they used to generate test data.  I used AutoPoco to generate a much larger set of realistic-looking test data than was previously available.

Last week, I gave a presentation to my local .NET user group (RockNUG) on the framework.  The slide deck I put together is available here, and there’s some demo code here.  The rest of this blog post will speak to the following question (or a rather rough paraphrase of it) that came up during a demo: is it possible to generate instances of a class with an attribute that has a wide range of values, save one?

The demo that prompted this question is in the AddressTest.cs class in the demo code I linked to earlier.  In that class, the second test (Generate100InstanceOfAddressWithImpose) gives 50 of the 100 addresses a zip code of 20905 and a state of Maryland.  The possible objective of the question could be to generate a set of data with every state except one.

After taking a closer look at the documentation, and a review of the AutoPoco source code for generating random states, I came up with an answer.  The Generate1000InstancesOfAddressWithNoneInMaryland test not only excludes Maryland from the state property, it uses abbreviations instead of the full state name.  The implementation of CustomUsStatesSource.Next adds a couple of loops (one if abbreviations are used, one if not) that keep generating random indexes if the resulting state is contained in the list of states to exclude.

The ability to pass parameters to custom datasources in order to control what type of test data is generated is an incredibly useful feature.  In the work I did on my project’s test generator, I used the capability in order to create a base datasource that generated numeric strings with the length controlled by parameters.  This allow me to implement new datasources for custom ids in the application by inheriting from the base and specifying those parameters in the constructor.

Because AutoPoco is open source, if your project has specific needs, you can simply fork it and customize as you wish.  Another value-add of a framework like this could be realized if you write multiple applications that share data.  In such a scenario, test data becomes a corporate resource, with different sets generated and made available according to the scenarios being tested.

Another advantage of AutoPoco for test generation is that its use of plain old CLR objects keeps it independent of specific database technologies.  I’m currently using AutoPoco with RavenDB; it will work just as well with the database technology (or ORM) of your choosing–Entity Framework, NHibernate, SQL Server, Oracle, etc.

AutoPoco is available via NuGet, so it’s very easy to add to whatever test assemblies you’ve currently got in your solutions.  As long as you have public, no-arg constructors for the CLR objects (since AutoPoco uses reflection to work), you can generate large volumes of realistic-looking test data in virtually no time.

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.

Please Learn to Code

I came across this post from Jeff Atwood in my Twitter feed this morning. It even sparked a conversation (as much of one as you can have 140 characters at a time) between me and my old co-worker Jon who agreed with Jeff Atwood in far blunter terms: “we need to cleanse the dev pool, not expand and muddy the water”.

While I understand Jon’s sentiment, “cleansing” vs. “expanding” just isn’t going to happen. Computer science as an academic discipline didn’t even exist until the 1950s, so it’s a very long way from having the sort of regulations and licensure of a field like engineering (a term that dates back to the 14th century). Combine that with the decreasing number of computer science graduates our colleges and universities graduate each year (much less the elimination of a computer science department), and it’s no surprise that people without formal education in computer science are getting paid to develop software.

While it does sound crazy that Mayor Bloomberg made learning to code his 2012 new year’s resolution, I’m glad someone as high-profile as the mayor of New York is talking about programming. When I was deciding what to study in college (way back in 1992), computer science as a discipline didn’t have a very high profile. While I knew programming was how video-games and other software was made, I had to find out about computer science from Bureau of Labor Statistics reports.

Jeff’s “please don’t learn to code” is counterproductive–the exact opposite of what we should be saying. Given a choice between having more people going into investment banking and more people going into software development, I suspect a large majority of us would favor the latter.

I also don’t believe that the objective of learning to code has to be making a living as a software developer in order to be useful. The folks at Software Carpentry are teaching programming to scientists to help them in their research. People who test software should know enough about programming to at least automate the repetitive tasks. If you use a computer with any regularity at all, even a little bit of programming knowledge will enable you to extend the capabilities of the software you use.

We need only look at some of the laws that exist in this country to see the results of a lack of understanding of programming by our judges and legislators. I think that lack of understanding led to software patents (and a ton of time wasted in court instead of spent innovating). The Stop Online Piracy Act and the Protect IP Act are other examples of dangerous laws proposed by legislators that don’t have even the most basic understanding of programming.

As someone who writes software for a living, I prefer customers who understand at least a bit about programming to those who don’t, because that makes it easier to talk about requirements (and get them right). They tend to understand the capabilities of off-the-shelf software a bit better and understand the tradeoffs between it and a custom system. In my career, there have been any number of times where an understanding of programming has helped me find an existing framework or solution that met most of a customer’s requirements, so I and my team were able to focus our work just on what was missing.

Thanks Again StackOverflow!

About a month ago, I wrote a brief post about starting a new job.  In it, I tipped my hat to StackOverflow Careers, for connecting me with my new employer.  Yesterday, I received a package from FedEx.  I was puzzled, since I didn’t recall ordering anything recently.  But upon opening it, I discovered a nice StackOverflow-branded portfolio, pen and card with The Joel Test on it.  In the pocket was a version of my profile printed on high-quality paper.

I appreciate the gesture, and thank StackOverflow Careers and the StackExchange team not only for the portfolio (which I’ve already replaced my previous portfolio with), or for creating a great site for connecting developers with employers that value developers, but for the whole collection of Q & A sites that make software development (and many other fields of endeavor) easier to learn.

 

 

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.

New Gig

Tomorrow will be my first day with FEi Systems.  They design, build and maintain healthcare IT systems, including health information exchanges and electronic health record (EHR) systems.  I’ll be helping their efforts in those areas as a senior developer.  I’m looking forward to being back in the healthcare sector (I worked for another healthcare IT company earlier in my career).  I think it can benefit a great deal from the deployment and usage of well-designed IT systems.

I wouldn’t have been aware of this company, or this opportunity without Stack Overflow Careers.  I was a beta tester on the original site (Stack Overflow), and since it launched it’s been a big help to me in solving a number challenging technical problems on a variety of projects.  Both the career site and Q&A site are worth your while if you write code for a living.

Retraction

Just came across this story relating to my January 25th blog post on Mike Daisey’s story of his visits to factories in Shenzhen. As it turns out they were indeed stories–large portions of his monologue have been revealed as complete fabrications.

This American Life has fully retracted the story, so I thought I would do the same since I linked to it when it first aired.

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.