Thursday, January 17, 2008

The Texas Tour of ASP.NET MVC Comes to a Close

It’s been an exciting two weeks.  I visited user groups in San Antonio, Houston, Austin, and Fort Worth, and I got to meet and connect with a lot of interested developers about the new ASP.NET MVC framework.

Two of the events were recorded, so if I ever get my hands on the recorded bits, I’ll post them here in an easily watchable format.  (Don’t hold your breath though.)

My presentation had a minimal amount of PowerPoint.  I decided to focus on real code.  And people have thanked me for it! 

I covered:

  • Basics of MVC
  • Default Routes
  • Simple Controller / Action
  • Passing data to the views
  • passing strongly typed view data
  • Custom Routes
  • new/edit form for an entity
  • writing tests for controllers
  • rhino mocks for mocking out dependencies
  • dependency injection for controllers (using Windsor)
  • ajax integration with prototype

This is a lot to tackle in my 1/1.5 hour time slot.  And I do get a lot of blank stares when I talk about mock objects and dependency injection, but I think more presentations need to present realistic code, rather than simple examples.  The feedback I’ve gotten about this has been overwhelmingly positive, and I’m really glad people liked it.

Some questions that were asked during my tour…

“Ben, you are phenominally good-looking?  Are you available?”
umm no, I’m married (and now terrified)

What is that cool zoomey tool you used?  That’s awesome!”
that is called ZoomIt, by sysinternals.

What are you using for data access on your entities?”
It’s called Castle ActiveRecord, which uses NHibernate underneath.

Can I use ASP.NET validators?”
—Well not really.  The validators have viewstate, which won’t work.  Your validation definitely belongs in the Model, so you could just do server-side if you want.  If you want clientside validation, you could accomplish a similar result by using marker css classes and some clever javascript  (think <input type=”text” class=”required” name=”address1” /><span class=”error_message”>This is required</span>

If we use this on IIS6, will performance suffer?”
— Probably.  On IIS 5/6 you have to send all requests through ASP.NET, which can be slower.  I’m not an expert in this area, so definitely do your research before making any rash decisions.  When we have Windows Server 2008 and IIS7, expect vast improvements.

Do you know Justice Gray?”
—Sure do.  I’m a bona-fide potential friend of Justice Gray, and even have the certificate to prove it.  It’s serial number marked 00000000000003.  I carry it with me at all times for just such a question.

Can you use co-exist with webforms?”
—Yes!  If you hit a page with the .aspx extension, the PageHandlerFactory will pick it up and you will be in WebForms Land.  If you instead hit a route, you’ll be in MVC Land.  Learn to know the difference and you’ll be fine.

Isn’t it dangerous to output strings directly like that?”
—Yes it is.  Read my post about html encoding your values.

Your hair is impeccable?  What kind of gel do you use?”
— It’s called Fructis something-or-other… and it’s awesome!

So Microsoft has two methods of developing web applications?  Which should I choose?”
—If you like the rich eventing model and the ability to save state and mimick a smart client ui, then choose web forms.  If you value clean html, urls, separation of concerns, and testability, then choose MVC.  I will be choosing MVC more often than not from here on out.

When will it be released”
—Gawsh, I don’t know?  Fall 2008?  As soon as I know I’ll post it here.

(admittedly, a couple of those questions were not asked — I’ll let you guess which ones)

You can download the code and the presentation here:

File Attachment: save the world with asp.net mvc.zip (1612 KB)

I’d like to thank the user group leaders for having me speak, it was certainly a pleasure.  Where will the next tour be?

Wednesday, January 16, 2008

Thank you ADNUG and FWDNUG

I got the chance to meet an enthusiastic bunch of folks in Austin and Fort Worth this week, and I wrapped up my Texas Tour of MVC.  I really want to say thanks to groups for having me.  It was a pleasure to speak and connect with fellow developers in Texas.

I’ll be posting a massive recap this week that includes the code and powerpoint that I delivered.  I’d appreciate any and all feedback.

Now for that long drive home…

Saturday, January 12, 2008

Visual Studio 2008 CSS Support - Not Quite

One of the often-touted new features of VS 2008 is the improved CSS support.

I like having intellisense for CSS, sure, but I dont’ want to have a warning for classes that don’t exist…. like this:

Vs2008-css-warning

That green squiggly indicates a compiler warning.  It can’t find the CSS class, (which is referenced in the Master Page). 

But there are other reasons why you might want to define a class without defining an associated style.  You might be using it to distinctly select elements from the DOM using javascript. 

The ever-so-awesome prototype framework introduced this syntax:

$$(‘.info’)

which would select all of the elements with a class name of “info” for you to work with.

Just about every other javascript library out there now supports this syntax (or a similar one).

If this bugs you, you can turn it off by checking this box (in Tools -> Options) :

Vs2008-turn-off-html-errors

You may not want to do this, though…  it could potentially save you time if you’re looking for a rendering bug.

Another part of VS 2008 that I don’t really care about is this CSS designer.  I can’t imagine anyone who would actually use this.

I mean, come on… look at it!

Vs2008-css-builder

I guess if you don’t know CSS very well, you might want some options.  But that form is more confusing to me than border-top: solid 1px #999;.

(and one could argue, if you don’t know CSS, should you be defining styles at all?)

I really applaud Microsoft’s attempt to bring a better CSS development experience to the masses, but I think they missed the mark on this one.

Thursday, January 10, 2008

Thank you HDNUG!

I enjoyed giving the presentation on ASP.NET MVC tonight to about 90 eager developers at HDNUG here in Houston.

There were a few people that had played with the framework before, a lot who had heard of it, and about 1/3 of the room had never heard of it.  Awesome!

There’s always a balance between how intro a talk should be, but one of my goals with this talk is to show real application code; to show how you actually test these things, and what types of practices and tools that can enable this and make it easy.

I hope that the talk was well received.  If you went, I’d love to hear your feedback in the comments!

Some links from the talk:

I’m sending the code over to be posted at HDNUG right now.  After my tour ends next week, I’ll post a large recap that includes code and hopefully answers to common questions that I’ve been hearing.

They also recorded the talk, so once I get my hands on the video, I’ll throw it up here so others can have a look.

Thanks for having me HDNUG!

The Über USB Cable

While shopping for a presentation clicker at Best Buy, I ran across this little gem.

It’s a flippin’ USB cable for $41.99!  It’s labeled as a “Microsoft Vista Migration Cable.”

IMAGE_027

IMAGE_028

Too bad it wasn’t gold plated, otherwise I would have jumped all over it.

Wednesday, January 09, 2008

Be careful outputting strings in ASP.NET MVC

During my presentation at Alamo Coders last night, someon mentioned that it is unsafe to output user-entered strings like this:

<h2><%= college.Name %></h2>

The reason is, of course, that the user could be malicious and enter in a string such as this:

“<script>alert(‘I am evil’);</script>”

And it would evaluated on the page and all of your users would get an alert box.  This is leaving your door wide-open to clever attacks known as Cross Site Scripting and is very dangerous.

Instead, we should escape these strings so that they aren’t rendered as HTML or javascript, but rather textual characters.  That means that < will be translated to &lt; and so on.

To do that, the Html helpers that ship with the framework give you an Encode method, letting you do something like:

<h2><%= Html.Encode(college.Name) %></h2>

But the syntax is a bit cumbersome for every outputted value on a page.  I prefer the way Rails handles it… like this:

<h2><%= h(college.Name) %></h2>

And it turns out that you can add this extension method somewhere and be done with it:

public static string h(this ViewPage page, string input)
{
     return new HtmlHelper(page.ViewContext).Encode(input);
}
And that’s it!  Just remember to take this precaution, or you’ll regret it later.

Thank you Alamo Coders .NET!

Last night I kicked off my Texas Tour of MVC at Alamo Coders in San Antonio.

The room was pretty small and they filled it up with about 25 people.  It was a bit more personal and cozy than the Houston .NET User’s Group that I’m used to.

I ran a bit short on time, but luckily I had my ready-baked turkey in another folder so I was able to show my ajax portion of the presentation and I think it went well.

I got some feedback from a few people who found the talk to be covering too much.  (In addition to ASP.NET MVC, I covered inversion of control, mock objects, and a few other things).  I wish I had a bit more time to explain these things in more detail, as it can be like drinking from the firehose, however I think they are important topics, and necessary to understand if you expect to properly unit testing on your controllers.

For those of you that were there, here are the relevant links for more information…

Inversion of Control:

Mock Objects:

The javascript library I was using was prototype, available here: http://www.prototypejs.org.

Thanks again to Alamo Coders for having me!

Sunday, December 30, 2007

ASP.NET MVC Tour of Texas

Want to see what the hype is all about with the new ASP.NET MVC Framework?

I’ll be touring Texas speaking about it.  If you live in Texas, come check out my talk “ASP.NET MVC – Save the Developer, Save the World.”

Here’s the confirmed schedule:

  • January 8th – Alamo Coders (San Antonio)
  • January 10th – HDNUG (Houston)
  • January 14th – ADNUG (Austin)
  • January 15th – FWDNUG (Fort Worth)

We’ll take a look at the new framework, the design goals, and dig into some code.

Hope to see you there!

 

Thursday, December 20, 2007

What I do

Looks like a new blog meme is starting…  I was tagged by my good friend Shey who’s wanting to know what we all do. (sorry it took me so long to respond!)

So…  what would you say… you DO here?

Office-space-what-do-you-do

Well, I’m a consultant, so my role sometimes changes.  Currently I’m a lead developer for an energy company, involved in a Lotus Notes to .NET Migration.

There are roughly 80 applications, some targeted for SharePoint 2007, some custom ASP.NET, and some Windows Forms.

We decided to use SubSonic for data access.  Most of my readers know that I’m a DDD bigot and prefer persistence ignorant business entities and yada-yada-yada.  But sometimes you just have a simple domain model and there’s no reason to go into the effort of introducing NHibernate on a team that’s unfamiliar with ORM’s in general.  (NHibernate just has too steep of a learning curve).  SubSonic shines with simple domain models, and it allows us to be very productive very quickly.

In retrospect I think I would have favored using Castle ActiveRecord.  We wouldn’t benefit from code-gen unless we made some custom templates for CodeSmith, but the main point would be that we can decouple the database from the application code.  It turned out that some database naming standards were in place that favored very common abbreviations for just about any type of database column.  This resulted in polluting our object model with poor names and I implemented a cheap work-around using partial classes and vanity properties.  But the stink is still present .

We’re also using Team System, which is a less than desirable experience in my opinion.  TFS SCC is okay, but it’s no Subversion.  We do our database development with DB Pro and I’ve found that to be both a blessing and a hinderance.  I’m starting to realize that with all of this tooling that MSFT is selling, a better experience can be had with a bit of patience, process maturity, and some open source software.

Where’s all the good info on DB Pro anyway?  All I’ve found are press release/feature list blogs and links to marketing material.

For testing we’re using MSTest, and if I had it my way I’d rip it all out and replace it with NUnit.  MSTest is more annoying than its worth, and we’re not using any of the build quality features anyway, so we wouldn’t be missing out on anything.  I use TestDriven.NET so I never even see the VS Test Runner.

For the build process we use NAnt, but we haven’t setup continuous integration.  Why?  Frankly because I haven’t had the time.  (I can hear the argument that I don’t have time NOT to already).  I have targets setup to build up the database (both incremental and from scratch), generate code with SubSonicCommander, compile all of the source and run the tests.  Continuous Integration is the next logical step, and I hope I can implement it soon.

In summary, here are the tools, frameworks and libraries that I’m using:

  • SubSonic
  • NAnt
  • WCF
  • VSTS 2005 + DB Pro
  • TFS Source Control
  • Prototype / Scriptaculous (for most ajax and dhtml stuff)
  • ASP.NET Ajax / Control Toolkit (for UpdatePanel and the Calendar)
  • MSTest (yuck)
  • Enterprise Library Logging
  • RhinoMocks
  • TestDriven.NET
  • Console2!

To get this thing spreading like wild-fire, I’m going to tag…

Jason Meredith
Evan Hoff
Bil Simser
Justice Gray (keep it professional Justice!  No weekend details!)
Peter Seale

…get to it guys!  What do YOU do?

Tuesday, December 11, 2007

Google Launches a Chart API

Google recently released a URL API for charting.

What's a URL API, you ask?  Well you open an image to a specially formatted url, and google returns you a chart.  You can do some pretty amazing things with this API.

Check out these samples:
       

The last one was created with this url:
http://chart.apis.google.com/chart?cht=p3&chd=s:Uf9a&chs=200x100&chl=A|B|C|D

The urls are a little bit cryptic as you can tell, but this allows you to use one source for charting regardless of your language or platform.  A .NET wrapper is sure to spring up in the next couple days that will allow you to take advantage of this API with a more structured format.

Update:  Boy did I underestimate the community.  Chris Pietschmann already created an ASP.NET 2.0 Server Control to leverage the Google Chart API in .NET.

I'm wondering how they are able to handle all of this.  Not only is it a lot of traffic to handle (but they're obviously pros at that by now) but it takes a significant amount of CPU cycles to generate a graph like that on the fly.  How do they keep their response times so quick?
 

Credit Card Consolidation - Hotel Las Vegas - Wills - Best Credit Cards