Tuesday, March 24, 2009

Scott Hanselman rated higher than Scott Gu

So I've been watching all the Keynotes from Mix 09. Mostly because I’m to busy and to poor to go to these types of things. Scott Hanselman gave this talk about Nerd Dinner, during the talk, well actually at the start of the talk he mentioned that if you searched on Scott, Scott Guthrie would come up as 5th and he would be at 11th place. Just having to know how true to the facts that is, and because posed with an unknown I just need to know the answer. I of course Googled it.

Hanselman actually came in 6th and Scott Guthrie came in 8th. How wacky is that. If I were to totally geek out I would say that Scott and Scott need to have a light saber battle to the death for Google rating supremacy. But I won’t do that.

UPDATE: Thinking about this more, I have to say how sick is it that doing a Google search on the name “Scott”, and considering the number of people named Scott in the world and how many times the word Scott must appear on the internet, that Scott Guthrie and Scott Hanselman are in the top 10 search results in Google. I am curious as to what methodology they use as far as SEO is concerned.

Monday, March 9, 2009

Code Contracts in C#

One of the new technologies Microsoft has coming down the pipeline for .NET 4 is Code Contracts. What are code contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, post-conditions, and object invariants. Contracts act as checked documentation of your external and internal APIs. The contracts are used to improve testing via runtime checking, enable static contract verification, and documentation generation.

But what they are is so much more, they bring design by contract to .NET. Traditionally one would set up some type of argument checking to verify the validity of what was passed in, and probably return some type of invalid argument exception upon failure, like the following:


public class DummyObject
{
public DummyObject(int n)
{
if(0 < n)
throw new InvalidArgumentException("n must be greater than 0.");
}
}


This isn’t the most elegant of solutions, also it is only verified at runtime. With Code Contracts, you now have static checking of your code conditions to see if you violated the contract at compile time. As far as testability this is great. But even better is that we as developers can tailor our code, to make the compiler a little bit smarter. In the code above everything would compile just fine if we initialized a new instance of our “DummyObject” with a 0, only when we ran it would we see an error. Now if we were to rewrite this with code contracts it would look like this:



public class DummyObject
{
public DummyObject(int n)
{
Contract.Requires(0 < n);
}
}


Now with static checking turned on we would see an error at compile time telling us that we have violated the contract. This is one of the simplest examples there is a great deal more that can be done. I recommend reading the Microsoft research site and look for other articles out there.

Thursday, March 5, 2009

Microsoft MVC Digg Sample

So I recently was writing an MVC sample just to familiarize myself with the Microsoft MVC Framework. One of my favorite samples recently was one done by Scott Guthrie for Silver Light. It was a Digg sample so I wanted to make one that was similar to that one. So with no further adieu.


The source code can be downloaded here.

I will be updating it with more features as I build it out and will probably be doing posts to explain the different parts, so keep checking for new entries.

Tuesday, March 3, 2009

Syntax Highlighter

So I got an email this morning asking me about the code blocks in my posts.

Hey I noticed that your code you put in your posts has changed, can you tell me what your using to do that I really want it for my blog.

Basically I had received a number of emails stating that my code was impossible to copy and paste easily, so I started writing my own syntax highlighter and then I poked my eye out! Basically I realized that someone had already done this so why was I spending my time on it. And then I set out on my search through the search result jungle of Google.

After reviewing a ton of different methods I found one I liked the best Syntax Highlighter by Alex Gorbatchev. It’s actually very slick, it post processes the tags in your page and does the decoration after it loads via JavaScript. So I don’t have to worry about any real formatting and the like just wrap my code blocks in some simple tags and done.

Anyhow go check it out and donate to him he has done a lot of work and it is really quite good. 

Monday, March 2, 2009

Microsoft MVC Tip #1

Well here is my first tip for MVC, how nice that I actually hit a head scratcher.

So recently I was doing some Model binding in MVC, and couldn’t get the binding to work. I had a simple object:

public class SimpleObject 
{
public string TestHiddenField;
public SimpleObject
{
TestHiddenField = "2";
}
}

In my Controller I was assigning the instance of my SimpleObject to my Model:


public ActionResult Index() 
{
ViewData.Model = new SimpleObject();
return View();
}

And in my markup I had this:


<%= Html.Hidden("TestHiddenField") %> 

The markup that was produced was this:



 

Now notice the value is blank. The model should have bound the value “2” to this control and that should have been the value. But without knowing it I did something stupid. I have a public field but it seems that the Model Binder expects a property with a getter and a setter. Well at least a getter.


So to fix the problem I had to fix my SimpleObject:


public class SimpleObject 
{
public string TestHiddenField{ get; set; };
public SimpleObject
{
TestHiddenField = "2";
}
}

So in conclusion it seems that under the covers MVC Model Binder looks for properties and not fields on the object. This is interesting since I would think that it would look for both. Though it is a good practice to use properties rather than fields I just didn’t expect it.