Scott Hanselman

Hanselminutes Podcast 140 - Rob Conery learns about Domain Driven Design

December 05, 2008 Comment on this post [7] Posted in Podcast | Programming
Sponsored By

My one-hundred-and-fortieth podcast is up. Neither I nor Rob Conery are experts in DDD (Domain Driven Design.) We hear a lot about DDD, but we hear very "Zen-like" and "soft" terms. As concrete (or fairly so) thinkers, Rob and Scott try to get our tiny minds around DDD. Is it a fad? A religion? Some kind of software design cult? Rob Conery has decided to learn for himself, and I join him for the trip in this episode. We likely accomplished nothing, but we've taken the first step.

The premise of domain-driven design is two-fold:

  • For most software projects, the primary focus should be on the domain and domain logic; and
  • Complex domain designs should be based on a model.

Also, check out Eric Evans on .NET Rocks!

Subscribe: Subscribe to HanselminutesSubscribe to my Podcast in iTunes

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show!

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET and Windows Forms. Enjoy the versatility of our new-generation Reporting Tool. Dive into our online community. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Hanselminutes Podcast 139 - Moonlight (Silverlight on Linux with Mono) with Miguel de Icaza and Joseph Hill

December 05, 2008 Comment on this post [0] Posted in Open Source | Podcast | Silverlight
Sponsored By

Mono Project Logo My one-hundred-and-thirty-ninth podcast is up. Scott chats with with Miguel de Icaza and Joseph Hill, the folks behind Moonlight. It's Silverlight on Linux with Mono and it's Open Source!

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show!

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET and Windows Forms. Enjoy the versatility of our new-generation Reporting Tool. Dive into our online community. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

Technorati Tags: ,,,

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

The Weekly Source Code 38 - ASP.NET MVC Beta Obscurity - ModelState.IsValid is False because ModelBinder pulls values from RouteData

December 04, 2008 Comment on this post [18] Posted in ASP.NET | ASP.NET MVC | Source Code
Sponsored By

I found a bug in an application I'm working on. I couldn't decide if it's a bug in the ASP.NET MVC Framework or a feature. I knew this, however. Both their code and my code runs exactly as it was written. ;)

First, the behavior I'm seeing, then a pile of unnecessary technical context because I like to hear myself talk, then my conclusion. Regardless, it's great fun!

UPDATE: I've been promptly teased/beaten by the MVC team, as they pointed out, pointedly, is the same behavior pointed out by Ayende like 15 years ago. It was prompted fixed/changed, and the new behavior changes the order (listed below) to 3, 1, 2 as I understand it. I hang my head in shame. ;) It's been totally fixed in the Release Candidate, so this post acts only as my own CSI:ASPNETMVC.

The Behavior

My application does CRUD (Create, Read, Update, Delete) on Dinners. When you are entering a new Dinner object, you fill out a form and POST your HTML form.

We take in the Dinner and save it (I've removed goo for clarity):

[AcceptVerbs(HttpVerbs.Post)]
[Authorize]
public ActionResult New([Bind(Prefix = "")]Dinner item)
{
if (ModelState.IsValid)
{
item.UserName = User.Identity.Name;
_dinnerRepository.Add(item);
_dinnerRepository.Save();
TempData["Message"] = item.Title + " Created";
return RedirectToAction("List");
}
}

The behavior I'm seeing is that ModelState.IsValid is ALWAYS false.

Note at this point that I'm not clever enough to go digging into the ModelState object for exactly why. More on my stupidity later. ;)

The Context

The Form POST looks like this (RAW HTTP):

Title=Foo&EventDate=2008-12-10&EventTime=21%3A50&Description=Bar&HostedBy=shanselman&LocationName=SUBWAY&MapString=1050+SW+Baseline+St+Ste+A1%2C+Hillsboro%2C+OR&ContactPhone=%28503%29+601-0307&LocationLatitude=45.519978&LocationLongitude=-123.001934

See how there's a bunch of Name/Value pairs in there? They mostly line up with the names of properties in my class as seen below.

Dinner Class Diagram

However, note that ID isn't in the Form POST. It's not there because it's an identity, and it'll be created when we save the Dinner to the database.

The New() method takes a Dinner as a parameter. That Dinner is create by the system because using the DefaultModelBinder. That binder looks at the values in the HTTP POST and tries to line them up with the properties on the object. Notice that there's no "ID" in the Form POST. Why is ModelState.IsValid false?

If I look in the ModelState.Values, I can see that the first value says "A value is required." ModelState.Keys tells me that's "ID" and that "" was passed in.

Watch Window showing error in ModelState.Values

Where did MVC get an ID from and why is it ""? I don't need an ID, I'm in the middle of making a Dinner, not editing one.

Well, it turns out that the DefaultValueProvider, the thing that, ahem, provides values, to the DefaultModelBinder looks in a few places for its values. From the source on CodePlex, note the nice comments:

public virtual ValueProviderResult GetValue(string name) {
if (String.IsNullOrEmpty(name)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
}

// Try to get a value for the parameter. We use this order of precedence:

// 1. Values from the RouteData (could be from the typed-in URL or from the route's default values)

// 2. URI query string

// 3. Request form submission (should be culture-aware)


object rawValue = null;
CultureInfo culture = CultureInfo.InvariantCulture;
string attemptedValue = null;

if (ControllerContext.RouteData != null && ControllerContext.RouteData.Values.TryGetValue(name, out rawValue)) {
attemptedValue = Convert.ToString(rawValue, CultureInfo.InvariantCulture);
}
else {
HttpRequestBase request = ControllerContext.HttpContext.Request;
if (request != null) {
if (request.QueryString != null) {
rawValue = request.QueryString.GetValues(name);
attemptedValue = request.QueryString[name];
}
if (rawValue == null && request.Form != null) {
culture = CultureInfo.CurrentCulture;
rawValue = request.Form.GetValues(name);
attemptedValue = request.Form[name];
}
}
}

return (rawValue != null) ? new ValueProviderResult(rawValue, attemptedValue, culture) : null;
}

Seems that while I assumed that the Form POST was the only place that a Model Binder would go looking, in fact, it looks in three places:

  1. Values from the RouteData
  2. URI query string
  3. Request form submission (should be culture-aware)

The emphasis is mine. At this point I know I'm not seeing a bug, but rather an uncomfortable side-effect of my overly generic naming. I created an ID property on Dinner, but is also have the default route in my Global.asax.cs:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "List", id = "" } // Parameter defaults
);

Note the default value for ID. This can be confirmed in the debugger at the same breakpoint I used before. The RouteData collection shows an ID name/value pair, where the value is "".

Debuger showing the RouteData object

The DefaultModelBinder saw that an ID was available, but it was set to "", which is different than complete omission. If it wasn't there at all, it wouldn't have been required. If it was set to "A" as in /controller/action/A then I'd have seen a different error of "The value 'A' is invalid." as "A" isn't an int.

The Conclusion

I'm going to change my model to use Dinner.DinnerID instead of Dinner.ID so that there's no reuse of tokens between the Route/URL and the Model's property names. Fun stuff! It was so nice to just check the ASP.NET MVC source code to solve my problem. Kudos also to the ASP.NET MVC team for making the source easy to read.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Dell Mini 9 - Practical Developer's Review

December 04, 2008 Comment on this post [28] Posted in Reviews
Sponsored By

imageSay what you like about Netbooks, or Tiny Notebooks, but I love my Dell Mini 9.

It's tiny. That's the whole point. It weighs like 2 lbs, almost nothing. It is definitely purse-sized (or murse-sized) with a high WAF (Wife Acceptance Factor). It's already saved me twice when I've been out and about and needed to log into servers to fix stuff.

Even if you don't get a Mini 9, as long as your expectations are set appropriately for what the machine can do, you'll love your netbook. Omar got a EeePC and loves it. Others like one of these:

From Dell, on the very low end you can get one for US$349 with a 4GB SSD (Solid State Drive), Linux and a half-gig of RAM. Good for surfing, but little else. On the high end, you can get a gig of RAM and a 16GB SSD, which is more usable.

Specs

Some netbooks have Hard Drives, but the Dell has a silent memory-based SSD. Omar points out that most netbooks have similar specs:

  • 10 inch 1024 x 600 screen
  • 80 GB SATA hard drive
  • Intel Atom 1.6 Ghz processor
  • 512MB – 1 GB of RAM
  • WebCam
  • Bluetooth (Lenovo S10 does not)
  • WiFi (Asus has 802.11n while the others are b/g)
  • Ethernet 10/100

The Mini 9 also has an SD card slot, 3 USB ports, headphone/mic and VGA out. The SSD seems to get a buffered read speed of about 25Mb/sec, while my T60p does about 50Mb/sec. That benchmark number feels about right when compared to the perception of speed.

The speakers are surprisingly loud. I watch a full episode of Heros last night while on the treadmill and had no trouble hearing it. It's also worth noting that it's completely silent when running. It's so quiet it's hard to tell its on. One last thing that's significant, the screen is exceptionally crisp and bright. I didn't expect so small a laptop to have SUCH a clear screen.

I've heard people say it gets hot, but I just haven't see that at all. Some folks complain there is no F11 or F12, key. However, BIOS A02 (the currently shipping BIOs) uses Fn-Z and Fn-X for F11 and F12, so no problem there now.

Upgrades are Cheap and Easy

However, if you check out sites like http://www.mydellmini.com/, there are aftermarket SSDs in sizes like 32GB. I'm sure 64GB is on its way. One strategy is to by the smallest, cheapest Dell Mini, the get a 2GB memory chip from Crucial for ~US$26. The upgrade is literally a 5 minute affair.

I upgraded my to 2GB and bought it with the 16GB SSD. The SSD is slower than a regular HD in my experience, but in weird ways. For example, the thing boots from off to the Windows 7 desktop in about 15 seconds. However, if you have a LOT of things going on writing to the drive it seems to slow down. That said, it's TOTALLY usable.

This guy added a GPS to his Dell Mini 9. I suspect we'll see more cool hacks like this.

Coding

I'm running Visual Web Developer and SQL Server 2008 Express and doing just fine. I'll be taking it with me to South Africa this month as my only laptop. I'll be speaking at two User Groups and running all my demos on this machine. I think it'll work just fine as a slowish laptop. After installing Windows 7, VS.NET Express (not MSDN), and SQL Express, I've got 4.58 Gigs free on the drive, so I'm not feeling cramped.

When you plug in a large keyboard, mouse and monitor, it almost disappears, especially when you're using it at a reasonable resolution.

Performance

I use it while working out to watch Hulu.com and other 480p videos and the when plugged in, the frame rate is just fine, very smooth. It drops frames if you're compiling in the background or something.

I'm getting between 3 and 4 hours of battery life, depending on the brightness level and how hard it's working. It's definitely better than the 2.5 hours I squeeze out of my Lenovo T60p.

I plugged it into my 1600x1080 monitor and amazingly it worked fine. It kept the same color depth and the windows still had transparent Aero glass with my pre-release version of Windows 7. It did seem that the video card was working pretty hard though. When I turn off transparency and themes it moves faster, so you can tune the visual effects to your taste.

I haven't tested Outlook, but as I understand it, the seek pattern of PST/OST files is NOT something that SSDs like. Since the Dell Mini 9 has a SD slot, you can easily add another "drive" by putting a SD card in there. I added a 4GB SD card and made a new folder called "C:\Program Files (SD)" then linked it to the SD card with this command line:

mklink /d "C:\Program Files (SD)" E:\Program Files (SD)"

Then I installed some programs in there. You can do the same with data files like OSTs, but don't expect stellar Outlook performance for now.

Mini 9 Performance Tweaks

There are lots of folks trying lots of tweaks. Try them one at a time and see what works for you. It's very early. I would also point out if you're running a pre-release of Windows 7 that from what I hear, the bosses are big fans of these little netbooks, and if you remove some feature, you might be working against some optimizations that they might be building in. This is conjecture on my part - a gut feeling. I'm not on that team.

That said, I REALLY like running Windows 7 on my Mini. Again, not speedy, but not slow. My Windows Experience Score is 2.3, but that's just the video that's holding it back. The other scores are all 3.0, except the memory which is 4.4.

If you need a backup machine for travel, a "surfing while on the couch" machine, a small laptop for a modest (child, teen, non-technical spouse), or a purse-machine desktop-replacement for a spouse, I totally recommend the Dell Mini 9.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Best Code Syntax Highlighter for Snippets in your Blog

December 02, 2008 Comment on this post [36] Posted in ASP.NET | DasBlog | Javascript | Open Source
Sponsored By

I get a few emails a day of folks asking what Syntax Highlighter I use in my blog for my code samples. Specifically, the newer code samples, as some of the old ones sucked as I was experimenting, trying to find the best one to settle on.

The tool I use is actually called SyntaxHighlighter and it's from Alex Gorbatchev. The trick is that the syntax highlighter is all javascript on the client side.

I was having all sorts of troubles with other code highlighters. First, there were ones that put css classes and stuff all through your code, trying to decorate each keyword. This just bloated my feed and site and made the code look weird in some Feed Readers. Then I tried using images for code, like ScottGu does, but that is just wrong. You can't copy paste the code, you can't search it, it's disrespectful for the blind, etc. Meh.

How I post code to my blog

I use Windows Live Writer to post all my blog posts, and it has a great plugin model. I've actually written a WLW plugin for the CueCat...it's really easy. I use a plugin from DasBlog contributor Anthony Bouch called PreCode that directly targets/supports SyntaxHighlighter from within WLW.

Screenshot of my plugins in Windows Live WriterThat means I see this from inside Live Writer. I slick Insert PreCode Snippet, and paste in my code.

If you're reading this blog post from inside an aggregator or feed reader, the next two code snippets look identical to you. However, if you visit my blog, you'll see that one is different.

// Hello3.cs
using System;

public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}
// Hello3.cs
using System;

public class Hello3
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("You entered the following {0} command line arguments:",
args.Length );
for (int i=0; i < args.Length; i++)
{
Console.WriteLine("{0}", args[i]);
}
}
}

One looks like this, as HTML:

// Hello3.cs
using System;

public class Hello3
{

public static void Main(string[] args)
{

Console.WriteLine("Hello, World!");
Console.WriteLine("You entered
the following {0} command line arguments:",
args.Length );

for (int i=0; i < args.Length; i++)
{

Console.WriteLine("{0}", args[i]);
}
}
}

See the 'class="c#" name="code"' part? Alex's Javascript SyntaxHighlighter is looking for those and parsing them on the client side. I choose to add
breaks, but that's an option in PreCode. Other options for SyntaxHighlighter include line numbering, gutters, copy/paste support, a toolbar and more.

P.S. If you don't use Windows Live Writer (and seriously, stop and ask yourself, WHY NOT?) and use instead a web interface, you can integrate SyntaxHighlighter into your web-based rich text editor. For example, Darren made a SyntaxHighlighter Plugin for the popular FCKeditor. Perhaps we'll put that in DasBlog.

Installing SyntaxHighlighter to Your Blog

You install the SyntaxHighlighter by adding it to your blog's template. It doesn't care what blog engine you run, as it doesn't need anything on the server:









Just add the shCore library and just the languages you require. If you want your blog to feel snappy and you have some control over your server, don't forget to set the files/directories to cache on the client by making them expire far in the future. You don't want your user's browsers to keep asking for these scripts each page view.

Even better, you can create your own plugins for SyntaxHighlighter if you use a language Alex hasn't supported officially. This guy threw together a Scala SyntaxHighlighter file by editing the Java one and adding a regex.

There are a few bugs but I think folks forget that Alex is doing this all alone, so I have to give him mad props for the effort. It can be lonely and unforgiving when you do something awesome and either no one cares, or folks only care to complain.

UPDATE: There's some great un-bundled brushes collected here.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.