chris carter's web log

Home |  Contact |  Admin
 

I Want My. I Want My. I Want My Segway

Posted on October 30, 2008

Evil Exception Handling

Posted on October 29, 2008

Grrrrrrrrrrrrrrr..........here's what I started this morning with(click to enlarge):

Can anyone spot the exception? the real exception?

Ya, me neither, I have no clue what happened because of these lines:

After commenting out the exception squishing code above and rebuilding, here are the images of the results I see now(I cut the screen shot into two because it was too big for one). 

Now can you spot the actual problem?

The problem was simple, my local copy of the database for this app is out of date, I just need to update. But to figure this out I had to crack open the solution, comment out lines of code that added zero value(actually reduced value), rebuild and then see the issue. Frustrating.

The Downside of Anonymous Types As Arguments

Posted on October 25, 2008

So far the only gotchya in upgrading to ASP.NET Beta was Html.TextBox.  It used to have this signature:

public string TextBox(string name, object htmlAttributes)
1

and now it has this signature

public string TextBox(string name, object value)
1

notice anything?

Yep, now that object means something different, so my calls that looked like this:

<%= Html.TextBox("whatever",new { @class="something", size=8 }) %>
1

started rendering this

<input type="text" id="whatever" value="new { @class="something", size=8 }" />
1

apparently I need better testing coverage, cuz you won't catch those at compile time.

Testing JsonResults in ASP.NET MVC

Posted on October 23, 2008

I needed a way to test values in a JsonResult to verify an action was behaving properly.  In my action i return something like this:

public JsonResult DoSomething(){
  //do stuff here
  return new JsonResult {
    Data = {
      success = true,
      message = "Yay, it worked!"
    }
  };
}
1
2
3
4
5
6
7
8
9

So in my test for the Action i need to verify the values of success and message but they're in an anonymous type so I need to pluck out the values.  That's where this extension method comes into play:

public static T Value<T>(this JsonResult jsonResult, string propertyName)
{
  T result = default(T);
  PropertyDescriptorCollection props = TypeDescriptor.GetProperties(jsonResult.Data);
  PropertyDescriptor prop = props.Find(propertyName, true);
  result = (T)prop.GetValue(jsonResult.Data);
  return result;
}
1
2
3
4
5
6
7
8

Now in my test I can do stuff like this:

JsonResult json = controller.DoSomething();
Assert.IsTrue(json.Value<bool>("success"));
Assert.AreEqual("Yay, it worked!", json.Value<string>("message"));
1
2
3

 Pretty cool, huh?

Huh, There's a Windows 7 Blog

Posted on October 23, 2008

Finally, Notebooks with 3+ GHz processors

Posted on October 23, 2008

Well specifically thinkpads.  I'm liking where the W series is going.  17 inch monitor, which I'm still on the fence.  1 gig dedicated frickin video card.  And now you can get a 64 gig SSD as the primary and a zippy 7200rpm secondary drive.  Now, if only I could get it all for half of the price....or win the lottery.  My only request is that Windows 7 ships before I get stuck upgrading to Vista..

Check out the specs:

Optional Parameters In JavaScript

Posted on October 20, 2008

I've found more and more uses for the optional parameters in javascript.  I have no idea if there's an actual name for this other than optional parameters, so i'm goin with it.

jQuery and prototype and I'm sure most if not all of the other cool "web 2.0"  libraries have a way of extending objects and have had these methods for a long time, so this is nothing new, I just am now finding more uses for it.  It usually entails copying the properties of one object over the properties of another.  I wrote a stand alone function copied from  prototype's Object.extend implementation that looks like this:

function extend(destination, source){
  for(prop in source){
    destination[prop] = source[prop];
  }
  return destination;
}
1
2
3
4
5
6

So let's say I have a function that has has 3 possible values that can be passed into it.  Most of the time I see methods like this:

function f(width,height,title){
//do stuff
}
1
2
3

The problem with this method is that all of params are required and none of them have default values.   Another way is using the built in arguments object (description taken from the ecmascript spec here-it's a pdf) like this:

function f(){
  var width = arguments[0] || 200;
  var height = arguments[1] || 500;
  var title = arguments[2] || 'Default Title';
  //do stuff
}
1
2
3
4
5
6

This accounts for not passing in all of the parameters and getting some default behavior, but they are based on position.  So in order to override the title, you'd need to pass in values for the first two params.

Optional Params Using extend

function f(){
  var options = extend({
    width: 200,
    height: 500,
    title: 'Default Title'
  }, arguments[0] || {});
  return options;
}
1
2
3
4
5
6
7
8

Using the extend method now, we can call the above method with any, all, or none of the parameters like this:

f(); //this would use all of the default values

f({ width:400 }); //override just the width

f({ title:'new title' }); //override just the title

f({ height:150,width:600 }); //override in any order
1
2
3
4
5
6
7

Here's a little test page that exercises this stuff: DefaultParams.htm

ASP.NET MVC Beta Source Code Is Posted On CodePlex

Posted on October 20, 2008

Yay, i was downloader number 7! Get it here.

Politics And Money

Posted on October 19, 2008

Are You Fucking Kidding Me?

I'm usually not into politics, actually almost never.  Sadly, this is the first year I am voting.  I do not support Obama.  I just don't want the old white guy to make it(gee, guess who I'm voting for).  However, today I realized why I don't like either candidate.  Apparently Mr. Obama raised 150 million in September.  According to the article, it puts Obama at 605 million bucks raised for his campaign so far.  Let me repeat...605 MILLION DOLLARS.

Wouldn't The Best Candidate Be The One...

... who could do the most with the least?  I'll prolly never go to a democratic or republican convention.  Why not? Better question: why? There's absolutely nothing one of those conventions can provide me or anyone else that will affect the way I feel about either candidate or party.  Nada.  Zip.  They do one thing, waste money.   Sure, they "create jobs"...for a week, but how bout taking that money and pouring it into something like schools or hospitals or something useful?

I'll prolly never go to a political fund raising dinner either, because I don't have any money I feel like lighting on fire and wasting.

Candidates, don't tell me how you're gonna fix the economy, show me by running the slimmest campaign, not the fattest.

Don't Mess With My TV Schedule

This one pisses me off the most. Stop putting those debates on TV. At least not on the weekends, or Monday through Friday, between the hours of 5am and midnight. Seriously, put that shit on YouTube, NBC, or somewhere online, and only the highlights please. 

Internet Debate

What would that take? An internet debate, online, realtime, open to anyone and everyone on earth to ask questions.  Put one candidate in one booth, the other in his booth, they don't need to see each other, and a host who can see both.  You'll need bandwidth(nbc could donate that), the host to ask the questions, and one of us geeks to run the internet show, and a small handful of people to screen questions.  Done. 

How much would that cost? 605 million? Nope, not even close.  The candidates are free, right? they sure as hell don't(shouldn't) get paid for this shit.  The host(I vote for John Stewart) would do it for free as well, it's an honor and a service for the country right?  Bandwidth isn't free, but that's where well placed commercials come in, kinda like how NBC does it now. 

 

OK.  I'm done.

Unity....

Posted on October 15, 2008

Check this out, VMware 6.5 has this thing called Unity, this is pretty f*cking cool.  In the pic below(click to enlarge), that's a view of the desktop of the host machine.  The IE browser instance is running in the host.  The firefox instance is running from an Ubuntu vware image hiding somewhere....

I Heart Balsamiq Mockups

Posted on October 5, 2008

This post is a little info on how I went about coming up with a design for a winform app I'm building.

OK.  I still use paper.  This is what I started with today(very bad quality photo of paper):

Although simple, it took me awhile to figure out where to start on the UI.  Once this was on paper, I had a better idea of where I wanted to start with Balsamiq Mockups.  After a pretty short amount of time(like maybe 30 minutes), I came up with this(click to enlarge):

Balsamiq Mockups

I spent very little time in the winform designer once i mocked up what I wanted. I ended up with this as a first cut:

Part 2: Test Driven Development In Practice

Posted on October 3, 2008

OK. Here's part 2 of the TDD in practice dealio. I decided a screencast would be appropriate. I'm just getting over a cold so my voice is shot, this is a silent movie.

In part 1 I typed in code that I want, to solve my problem. Most of the code did not exist. So in part 2, we need to create everything that's missing and make the test pass. Play the video to see how I did it(I used all CodeRush features to generate code - with a little manual intervention; but most of this can also be done with raw Visual Studio shortcuts).

Part 1: Test Driven Development In Practice

Posted on October 3, 2008

Go here for part 2.

Ok, so I've been meaning to do a screencast on this, i've done one but it's lame.  I figured I'd do more a photo slash journal-ish type thing and describe my real world(yes this is for an actual project) usage of TDD.  So here goes, I just typed the following into my test, this is what I want to do:

And that's pretty much it.  Notice the red squiggly lines? Yep, I haven't created that stuff yet, cuz this is what I want to do, not what I've done.  Next part will be making those red lines go away...

Muppet F*cker

Posted on October 3, 2008

That's one of the funniest terms I've heard in awhile.  I heard it during the brilliant presentation by Giles Bowkett recorded at RubyFringe.

IModelBinder with AutoBinder

Posted on October 2, 2008

Today I noticed this post on the ModelBinder attribute that ships with Preview 5 of ASP.NET MVC. I looked at the example and it doesn't make any sense.  I had come up with something I call AutoBinder and until today didn't think it was anything worth mentioning.  I could swear I saw somewhere that something like this would be included in the RTM of MVC but I can't remember where I saw that.

IModelBinder

IModelBinder is a simple interface with one method to implement, it looks like this:

public interface IModelBinder {
  object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState);
}
1
2
3

AutoBinder

AutoBinder simply implements that interface like this:

public class AutoBinder<TModel> : IModelBinder where TModel : class, new(){
  public object GetValue(ControllerContext controllerContext, string modelName,
    Type modelType, ModelStateDictionary modelState){
    return AutoBinder.UpdateFrom<TModel>(controllerContext);
  }
}
1
2
3
4
5
6

And AutoBinder.UpdateFrom looks like this:

public static T UpdateFrom<T>(ControllerContext controllerContext) where T : class, new(){
  T result = new T();
  NameValueCollection form = controllerContext.HttpContext.Request.Form;
  foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(result)){
	object value = null;
	//if the property type is boolean, make sure it's not an array, the
	//default behavior of Html.CheckBox is to create the checkbox, plus
	//a hidden field with the same name as the checkbox, to cover the behavior
	//of when a checkbox is unchecked, it won't get posted
	if (property.PropertyType == typeof(bool)){
	  string[] values = form[property.Name].Split(',');
	  if (values.Length > 0){
		value = ConvertUtils.ChangeType(values[0], property.PropertyType);
	  }
	}else{
	  value = ConvertUtils.ChangeType(form[property.Name], property.PropertyType);
	}
	property.SetValue(result, value);
  }
  return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Using AutoBinder

Using this requires either registering the types with their appropriate binder at app startup or adorning your models with the ModelBuilder attribute like so:

[ModelBinder(typeof(AutoBinder<Customer>))]
public class Customer{
//guts of Customer here
}
1
2
3
4

Now in a controller we might have a method that looks like this:

[AcceptVerbs("POST")]
public ActionResult Edit(Customer customer){
//blah blah blah
}
1
2
3
4

Assuming that all of the fields on the form have the same name attribute as the properties on the model, the customer arg will be mapped up with the posted form values automagically through the ModelBinder attribute.

Anyway, here's the spectacular example: ModelBinder.zip.  It assumes that you have Preview 5 of ASP.NET MVC installed.