The continuing saga of aligning .NET with rails – FactoryGirl.NET

While working on kinlighten, a side business I am starting up that runs on ruby on rails, I used a popular open source ruby framework for generating objects to use in tests. There are many frameworks available for rails that are popular including factory_girl, machinist, and fabrication.

These frameworks assist with the creation of objects in your application’s domain model that are initialized with state appropriate for tests. They help by allowing multiple test cases to re-use the same objects factories, reducing the lines of object initialization needed by tests.

The framework I used is factory_girl, and you use it by creating a single ruby file for each model’s factory. For example, if my domain had an order, customer, and line_item models, I would have a order_factory, customer_factory, and line_item_factory. When I want an instance of an object from my factory in my test, I just call a single method to “build” one for me.

James Kovacs has created the FactoryGirl.NET class library to allow .NET developers to use factories as well. This is another of many recent steps (bundling and minification, migrations, etc.) to bring ASP.NET MVC productivity up to the level of rails.

In his framework, you would instead create a class for each model in your domain. So if you had an Order, Customer, and LineItem C#/VB class, you would have OrderFactory, CustomerFactory, and LineItemFactory classes that can be used to retrieve objects initialized with state appropriate for testing. These classes go into your Test class library project.

Check out his article on creating FactoryGirl.NET, the github repository (it’s also on NuGet), and a great intro on how to use the original ruby version of factory_girl for more information. This is a first class technology with heaps of success in the rails community and I commend James for working to bring this to .NET.

The Minimalist Development Movement

Over the past 11 years, .NET technology, fueled by Microsoft’s ability to deliver sophisticated development tools, has arguably ruled the enterprise business software landscape. Intellisense, drag-and-drop UI design, XML configuration, dependency injection, unit testing, IDE extensibility APIs for third party controls, continuous integration, and more attempt to ease the use of Agile and SCRUM processes as the Visual Studio IDE supports more and more of these features through wizards and templates. .NET started as a better version of Java, and as such inherited many of the powerful capabilities, but also limitations in that development and deployment approach.

However in the past several years a move towards a minimal development tool mindset has started to occur. This is made possible by the creation of more sophisticated frameworks that establish conventions and set constraints on how to go about implementing things. These constraints reduce the number of API calls to remember, and the number of front end technologies that a typical developer needs to be fluent in. As a byproduct, the required capabilities of development tools is also reduced. Rails, which led to ASP.NET MVC, and JavaScript technologies like the MVC framework being built on Coffee Script as well as mobile frameworks like Titanium Appcelerator all take this approach. They provide the framework and API, and you use the development tool of your choice. Because the framework limits what you can do, but elegantly provides 80% of what you need in most applications, you don’t need an IDE that does so much for you. Extreme minimalists use VIM, Emacs, or TextMate; and Aptana is a popular one with first class support for CSS, HTML, HAML, Rails, JavaScript and many other minimalist technologies that might be a little more approachable to a seasoned .NET developer.

Visual Designers for 20%

However, to take part in this new shift requires a different mindset. What if you had to do all of your user interface development without graphically previewing it first? A dirty little secret in many Microsoft shops is that we rarely use the UI designers anyway. Clients and customers are always asking for features that negate the productivity enhancements touted by RAD design, and force us to the code to do more sophisticated things.  I’ll argue that this is due to an inferior separation of concerns in ASP.NET, and not simply because you’re doing something more complicated. If your framework requires you to break patterns to do something complex, how good of a framework is it? When a development tool only really shines for the minority of projects, your on the losing end of the 80/20 rule. When you design tools to focus on letting developers visually design things, you are continuing to treat UI assets as single units that encapsulate their behavior, presentation, and data. Modern frameworks that separate these concerns make it difficult (if not impossible) to visually represent things as they appear at runtime, but the tradeoff is an increase in productivity due to patterns that decouple responsibilities.

Interpretation vs. Compilation

What if you don’t compile your project to test it out? There are legitimate applications that require compilation due to performance constraints. But if quality is the concern, the efficiency enhancements afforded by these frameworks coupled with a disciplined automated testing approach negate this concern.

Document what you’ve built instead of what you want

What if you don’t create requirements documents, but rather rapidly implement and write tests to serve as the documentation for what the system currently does? We already know from years of SCRUM and Agile debates that documenting a system up front more often than not results in bad designs, slipped deadlines, and stale documentation. Most customers and clients are not system analysts and as such can’t be expected to communicate all of their needs on the first try. A picture is worth 1000 words, and we’ve all been in the meeting where the customer advocate is shown what was built based on their design and they realize things missing that they didn’t communicate. Doesn’t it make sense to use a development process that encourages and adapts to this situation instead of fighting it?

Contrary to popular belief, to pull this off one needs to be a better developer, who follows patterns even more than before. Developers also need to communicate with stakeholders more, and incrementally deliver *tested* features. Increasing the ability for a developer to communicate has all kinds of other benefits as well, such as the ability to clarify requests, think outside of the box, and generally be more pleasant to work with.

If the thought of letting your tests provide your documentation sounds crazy, tell that to Sara Ford.

Get better at learning your framework instead of fighting it

We’ve all been in the code review where someone implemented an API call that’s already in the .NET framework. If we’re honest with ourselves as developers, we really don’t keep much of the .NET technology stack in our head, we just know how to use Google well. If we were able to reduce the number of patterns and APIs used in our solutions, we could retain that knowledge and know the best way to leverage the framework to do what we need to do instead of fighting it. ASP.NET MVC and Rails both exhibit this, and I’ll argue Rails does a better job. ASP.NET MVC won’t complain if you make the mistake of trying to throw a bunch of logic in your view, where in Rails you really have to fight the framework to instantiate classes here. As DHH says “constraints are liberating” (start at 3:50 in).

The challenge

If you could challenge yourself with one technical hurdle this year, would you rather learn another API? Perhaps a way to interface with a new business system? Or would you rather experience a shift in your approach to development that has long lasting, measurable effects on your ability to deliver rapid value and makes you more attractive to established companies and startups? To do so requires several things.

  1. Approach these new patterns and capabilities without attempting to compare them to existing methods. As humans we love to do this, but often we get caught up in analysis paralysis or think we’ve “got it” when we grasp just one of the many innovations in these newer frameworks.
  2. Do not declare competence with these frameworks until we’ve actually grasped them in their entirety. Learning Rails without understanding caching, convention based mocking of tests, or the plugin architecture is like learning C# but ignoring generics and lambda expressions.
  3. Don’t try to figure out how to shim legacy .NET patterns into these frameworks. You wouldn’t expose a low level communication protocol through a web service or REST API where clients are expected to allocate byte arrays, so why would you expect to figure out how to host a third party ASP.NET control in MVC or access a database using T-SQL from an MVC view. Sure, you can do it, but you’re missing the point. And that is to embrace new patterns and learn to abstract the old way of doing things. We’ve been doing it with .NET for years, now let’s see if we can do it when legacy .NET patterns are what we’re abstracting.

I’m glad Microsoft is embracing convention over configuration

I read Agile Web Development with Rails while visiting San Diego a couple years ago and was blown away by how well put together of a framework it was. What the book helps you realize is that if you follow certain naming conventions for your code artifacts (in this case ruby source files), it automatically wires up communication between the different architectural layers of your application.

With the recent release of ASP.NET MVC 1.0, which is Microsoft’s answer to ruby on rails, Microsoft has provided what seems to me to be a simpler approach to web applications and adapts to testability better than the oft-complicated event model of existing ASP.NET web applications.

I also downloaded Silverlight 3 Beta, Expression Blend 3 Beta, and Microsoft’s Rich Internet Application (RIA) toolkit preview. The new version of Silverlight has a ton of controls, and I love that editable forms with built in wiring up to validation are included out of the box!

When you have the RIA toolkit installed, you can create a data model in Entity Framework in your web application, create a special link to it in your Silverlight “client” project, and you can wire up similarly named domain objects to databind to your Silverlight project and the databinding hits the server using REST transparently. It’s very slick.

Late May goodies on Microsoft blogs

Here’s a quick rundown of some stuff going on around the Microsoft blogsphere related to development of applications like those created by the custom development teams here at Catapult Systems:

Microsoft’s support for Ruby, what’s the strategy?

Today I came across a blog post by Charles Nutter that was linked to from a post in the IronRuby mailing list. This fellow has a very good blog and writes an interesting post about the current state of ruby implementations, inlcuding IronRuby, from his point of view. I think for the most part, he is spot on with all his conclusions. He makes the statement that Microsoft would never back an open source framework like rails in preference to its own. This seems to have caused a bit of a stir on the rails mailing list and folks are coming up with replies. Here’s mine.

Microsoft used to write IDEs for C++ before all this framework madness happened (I’m talking before MFC even). Folks stayed with Microsoft’s tools over others because they found them to be of higher quality and the productivity enhancements brought by the IDE saved them time and money. Today Microsoft holds industry control of the runtime and compilers itself for their .NET framework, and so for most folks on Windows, they are the only ones that offer a development IDE for it. Sure there is eclipse support for C#, SharpDevelop, and Microsoft has always touted that anyone with a text editor and command-line can build apps but due to the design and breadth of the class libraries this really isn’t feasible. You don’t see most folks using non-intellisense aware Java IDEs these days when Eclipse is around. Now I don’t really believe that Microsoft planned for Notepad to be a real development environment for .NET, I think this statement was more about proving the opportunity for other tools in the market to target .NET.

Rails is not just another bytecode based, intellisense requiring, class-library-laden layer over your operating system like .NET or Java are. Sure ruby could be argued as such but the knowledge of a small subset of Ruby plus the simple and elegant design of the libraries in rails make up a smaller set of APIs that let someone be very productive in their target space (web applications and services) without an IDE, as Microsoft had originally touted for .NET. While Microsoft has been working on creating tools for generating DSL’s with .NET (domain specific languages, where you only need a subset of APIs to deal with a industry or problem domain when coding against it with .NET) Rails has actually delivered this albeit with the entire runtime platform, and targeting web applications as the “domain”.

Rails and ASP.NET aside, I am personally not impressed with the current state of web applications. AJAX is a hack, and I was doing XML HTTP requests to classic ASP pages back in 98 because Internet Explorer let you create some higher fidelity user interfaces than was possible with Visual Basic, MFC, and the like at the time. I learned early in my career that a sharp user interface outweighs some fancy code (though I’ve learned since that maintainability is right up there, and hence, DRY API design) so going through all this work to get a snazzy HTML UI was worth the effort from an innovation standpoint. Fast forward to today – innovation in the user interface either requires a boatload of javascript in the form of frameworks like prototype, other open source javascript libraries, or Microsoft’s atlas – and custom code that is always a pain to get working on all the browsers. Many shops resort to go with ASP.NET, require the clients to use Internet Explorer, and call it a day. Others that aren’t tied to Microsoft are choosing rails and hitting the ground running. These shops won’t bother with IronRuby or the DLR unless it lets them do things from the UI that isn’t possible with rails, or develop things faster than rails lets you do. The only way Microsoft will draw these folks in is by having a MVC framework that’s as productive as rails and provides a better UI (Silverlight). Serving Silverlight from ASP.NET apps, ASP.NET MVC, and the Dynamic Silverlight projects are a step in the right direction, but not really whole-heartedly solving the problem IMHO.

If you look at Microsoft’s revenue, they are making far more money nowadays on Sharepoint, Exchange, and Windows Servers than on any of their development tools (Team Foundation Server makes them some cash too, but adoption seems relatively low). Many of the features in these products were built on .NET. Should IronRuby produce a high performing, CLR interoperable VM as is its goal, Microsoft encounters its usual dilemma. How do we support previous technology (.NET) and move forward at the same time with a completely different MVC paradigm such as rails enables. Unless you work for Microsoft, and even if you do, some of these strategic conclusions are hard to determine. A possible obvious solution would be that Microsoft will employ IronRuby as a language to use with their ASP.NET MVC framework. They will also use IronRuby, as they have in many demos, to serve Silverlight pages from the server (ala the Dynamic Silverlight effort, again). The problem I see with both of these is that most developers do not make great decisions about which APIs to leverage and not to leverage in .NET when building applications. There needs to be tighter control than just enabling the entire .NET platform’s libraries accessible from these technologies to create maintainable architectures in application’s code bases. I already see a host of browser served, ruby utilizing, .NET framework bastardizing apps that do crazy things from an architectural standpoint resulting in difficult deployment, and wildly different code organization.

One of the successes of rails to me is minimizing the number of APIs you need to do real work, and having standard conventions for placement of things. In the current ASP.NET MVC implementation, there is still a lot of calling C# code to wire up things like routes and opportunities for developers to place code in weird spots. I for one don’t enjoy when I switch from one project to another learning how they decided to do data access, exception handling, and all the other things that every single good ASP.NET app needs. This should be available for free, or though plugins similar to ruby’s mixins paradigm.

Therein lies my dream. I would love to see Microsoft create a MVC implementation that leverages the ruby language’s metaprogramming features to create a framework that uses XAML as its views and a combination of Active Record and REST for its models (whether in a Microsoft-created implementation that has providers for databases other than SQL Server) but is designed from the ground up for using WPF as a MVC runtime and not simply a clone of rails with a XAML view renderer. This framework needs to leverage ruby in its implementation – not just be another .NET framework you can code against with ruby.

I think Blender is cool for creating HI-FI XAML controls, but these should be databound to data served up by this MVC framework and rendered in the context of a controller action so that the same patterns are used as consistently as possible. I’m sick of the browser’s age old, buggy DOM – and if Silverlight is going to support everything in WPF 3.5 at some point, I would much rather code for this and test it on macs and under moonlight going forward. I would also like to see Microsoft ship an application that hosts Silverlight outside the browser on all platforms.

I know most folks probably won’t agree with me, but I think it’s high time that we get rid of some of the bloat that legacy browsers and class libraries bring when we aren’t even utilizing most of it in modern applications if they are designed well up-front. At some point you have to say that supporting the existing APIs in .NET is the responsibility of the .NET platform, and the DLR and Silverlight are new platforms for development that may have minimal interop, but are designed for a wholly different development model. Those who say this is too big of a goal don’t remember the Sun Microsystems and Microsoft from the 90s, who cranked out innovations in the runtime and user interface space every six months!

Mapping Rails to ASP.NET 3.5

If you’ve done some rails work like I have, and are getting started with looking at ASP.NET MVC for your .NET work; or you are a .NET guy, and looking at rails for the first time, you will want to see which technologies are available to do things in each platform. I hope the following chart is helpful:

Feature Rails Technology ASP.NET 3.5 Technology
Model View Controller architecture Rails Core (Action Controllers, Views) ASP.NET MVC
Domain model from relational database Active Record ADO.NET Entity Framework
REST Active Resource ADO.NET Data Services
Markup language for MVC frameworks HAML NHAML
Build platform Rake MSBuild

IronRuby development is taking off…

Prior to my recent move from Milwaukee to Austin, I helped a friend create a web application with Ruby on Rails on the side from my day job as a .NET architect. After 6 years at the architect level on Java and .NET projects, one of the things that struck me instantly as so beneficial was rails’ standard project directories that linked models, views, and controllers together without any configuration. Documenting and tracking adherence to standards in project structure is always a necessary but many times repetitious task that can make the difference between a really maintainable enterprise class solution, and one that simply meets requirements known upon first implementation.

The ASP.NET MVC project that’s currently being developed by Microsoft is capitalizing on the success of rails in a big way. It takes advantage of .NET in some ways that ruby is not able to, and succeeds in bringing some of the rapidity of the rails platform to ASP.NET without forcing developers familiar with the Visual Studio tools to drop their favorite IDE to join the “all I need is a text editor, man!” camp.

I have to admit that ruby is a great language however, and for all the great new things in C# 3.0, the japanese fellow who created it gave us a really clean syntax and it’s obvious that the folks in Redmond agree. The IronRuby project, created by Jon Lam who recently joined Microsoft last year, is a full-blown implementation of ruby that has as its primary goal the ability to run existing ruby programs. The second goal is to provide first class interoperability with the existing .NET technology stack, without losing the spirit of the interpreted language that ruby is.

Jon and the other folks working on IronRuby have succeeded in this by leveraging Microsoft’s upcoming DLR, a parallel evolution of the CLR that provides a standard interface for creating dynamic language runtimes that target .NET.

What does all this mean? Well with the current bits you can write ruby code to create WPF apps, ASP.NET pages, Silverlight applications, and hopefully at some point ASP.NET MVC apps that can access all the goodness of .NET while at the same time having access to the thousands of open source projects in ruby such as those on rubyforge, where the IronRuby project’s course code is currently hosted.

I’ve been following IronRuby, mostly as an observer, and am seeing development start to really pick up this past month. I made some updates to their project wiki the other day to organize things in anticipation of growth and hope that more folks will soon join in to help make this a really great technology that might help you be more productive on your next .NET solution.