July 2010 Entries

Notes on Templates and Data Annotations in MVC 2

Brad Wilson has an excellent series of 5 blog posts on model metadata and templates you can use to get started with templates and metadata in ASP.NET MVC 2. Here are a couple notes I've made around some of aspects that commonly confuse developers (including me, it seems). 1. Although most of the metadata attributes live in the System.ComponentModel.DataAnnotations namespace, there are a few exceptions. If you aren't seeing something you need in Intellisense, then try adding System.ComponentModel (for the popular DisplayName attribute) and System.Web.Mvc (for the popular HiddenInput attribute). 2. Some of the confusion around...

Prototypes and Inheritance in JavaScript

Forget everything you know about object-oriented programming. Instead, I want you to think about race cars. Yes – race cars. That's the intro for my latest article @ Script Junkie - Prototypes and Inheritance in JavaScript. I hope you enjoy reading it.  

Delegated Model Binding

In the last post we saw the recursive nature of the default model binder in MVC. Now, let's look at the following class: public class User { public string Name { get; set; } public string Email { get; set; } /* * bunch more simple stuff */ public AuthorizationClaims AdditionalClaims { get; set; } } Imagine AdditionalClaims are something that the default model binder will not understand. For whatever reason, you'll need to use a...

Recursive Model Binding

In the last post we saw how a model binder has to show up for work even when all we need is a simple int parameter on an HTTP GET request. Now it's time for a pop quiz! Given this route definition: routes.MapRoute( "SearchRoute", "Home/Browse/{category}", new {controller = "Home", action = "Browse"}); And this class definition: public class BrowseRequest { public int StartYear { get; set; } public string Manufacturer { get; set; } public string Category { get; set;...

Hard Working Model Binder

The job of a model binder in ASP.NET MVC is to take pieces of data in an HTTP request and place them into objects. It's easy to sense a model binder at work when you use one of the Update API's. bool success = TryUpdateModel(movie); // here // .. or ... try { UpdateModel(movie); // here, too! } catch (InvalidOperationException ex) { // ... model binding errors ... } If the first line of code with TryUpdateModel could speak, it would say something like "Here is a movie. I want you to look around and find information to populate the...

Ruby: initialize and super

One of the reasons to like Ruby if you come from a C#/C++/Java background is how certain constructs will tend to "just make sense" even though Ruby will throw in a twist here and there. Consider the case of inheritance and initialize methods. class Employee < Person end emp = Employee.new Let's say you run the code and get an error on the line of code creating the Employee object : "wrong number of arguments". Seems odd, because Employee doesn't look like it needs an argument for creation, but then you poke around and find the definition for Person (the...

OData and Ruby

The Open Data Protocol is gaining traction and is something to look at if you expose data over the web. .NET 4 has everything you need to build and consume OData with WCF Data Services. It's also easy to consume OData from outside of .NET - everything from JavaScript to Excel. I've been working with Ruby against a couple OData services, and using Damien White's ruby_odata. Here is some ruby code to dump out the available courses in Pluralsight's OData feed. require 'lib/ruby_odata' svc = OData::Service.new "http://www.pluralsight-training.net/Odata/" svc.Courses courses = svc.execute courses.each do |c| puts "#{c.Title}" end ruby_odata builds types to...

Multithreaded Robocopy

I was working with a fresh install of Windows Server 2008 when I noticed a new parameter in Robocopy's usage output:             /MT[:n] :: Do multi-threaded copies with n threads (default 8).                        n must be at least 1 and not greater than 128.                        This option is incompatible with the /IPG and /EFSRAW options. /MT is available in both 2008 and Win7. I did an informal benchmark against 12 GB of data spread over 300 files. The copy source was on the other end of a 100Mb switch. With...

Unit Tests and LINQ Queries

It’s easy to test LINQ queries when the LINQ queries always execute entirely in memory – like with LINQ to objects or LINQ to XML. All you need to do is put some in-memory data together and pass it to code executing the query. It’s an entirely different scenario when you work with a remote LINQ provider (like NHibernate, Entity Framework, WCF Data Services, LINQ to Twitter, and all those types of technologies). Take the following simple query as an example. var movies = _ctx.Movies .Where(movie => movie.Title.Contains("star")) ...

Named Arguments versus Object Initializers

The object initializer syntax introduced in C# makes it easy to work with "configuration" type objects. public class TemperatureSetting { public float Value { get; set; } public float Variance { get; set; } public float Threshold { get; set; } } // ... var settings = new TemperatureSetting { Value = 104f, ...

Are Code Generated POCOs Really POCO?

We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely. - from MF’s definition of POJO The .NET equivalent to a POJO is a POCO (plain old CLR object). POCO is a popular term in .NET development today, having caught the momentum POJOs started generating 10 years ago. This isn’t because .NET is 10 years behind the curve. POJO developers were revolting against the tyranny of...

What a Difference 8 Years Makes (In Code)

I spent some time this weekend rewriting 8 year old code. It's interesting to compare the pieces where the core logic resides. Original: New:   Now I'd like to jump ahead 8 years to see the code rewritten again (and bring it back with me).

ActionFilter Overkill

We've seen not one, but two posts this week where we used an ActionFilter as the solution to a problem. ActionFilters are powerful. ActionFilters can inspect and modify action parameters and action results. They can cover almost any cross-cutting concern in ASP.NET MVC: caching, security, auditing, etc. But, should you use them everywhere? [Log, UnitOfWork, PreParseParams] [CommonData, VerifyRedirects, HandleError] public class BusyController : Controller { } Think about the other options: HttpModules see the big picture. They can hook into the beginning of a request before a request even knows it's destined for a controller. Modules are...