Preparing to Speak

I’ve been revisiting my presentations on ASP.NET MVC and NuGet, as well as preparing my dojo-style course on web development (a deep dive session in MVC).  I’ve got two engagements booked this fall (SDEC in September and PrDC in November) and want to make sure I knock them out of the park.

As such, I’ve reviewed my notes on what technical content didn’t pan out, which jokes worked well, projects that I forgot to reset and the like. 

Here are some of the things that I try to do during a presentation, and something I’m working to improve in my existing talks:

  • Ride a theme. Creating a story arc that helps you tie content together keeps the audience engaged and allows for you to keep building on past references. If you’re trying to pick a metaphor for the work you’re doing, don’t force it. Jokes become fluid in this space too, especially if you can back-reference.
  • Use humor.  Sometimes you can plan for this, sometimes it just happens. I got hit with a funny and only realized it once when it was up on the screen in front of the whole room. Content+Context can sometimes be hilarious.  The other thing is that laughter draws folks in. If someone’s drifted off and they hear everyone laughing, they want to get right back into listening because they feel like they’ve missed something.
  • If you use humor, land it.  Don’t just make a joke, but make a relevant joke, and then use that to drive home an important point.  People are listening after you make them laugh, so plan to use that attentiveness.
  • Pay dividends.  Remember that your audience is investing their time in you and that they do have expectations.  Help them fulfill those by creating appropriate abstracts (to help them plan which session to attend), delivering content that is worth their time (to help them learn), equip them with things they can use right away (to get their buy-in) and follow up with post-talk support, code, links etc. (to provide participants with a landing).

 

I’ve dug up some posts that have been helpful in that planning and cover a lot of the bases for things you want to do when addressing a technical audience.  Below are some tips of some of my favorite presenters that I have seen in-person and really admire their effort and the skill with which they execute when presenting.

Scott Hanselman

 

Phil Haack

 

Some other great tips come from Kirk Evans, Venkatarangan and Jon Galloway.  Finally, head over to Presentation Zen and watch some great presentations. Lots of material there.

Setting the Resolution Using the Camera API in Windows Phone 7.1 Mango

I’m working on an app that, believe it or not in today’s ultra-high-res-world, doesn’t require much resolution at all. The images are used as part of a simple on-screen sequence, and while they may ultimately be saved to local storage, they’ll only ever be displayed in a 100x100 box on the display.

Changing the resolution is fairly easy: we simply pick one of the resolutions available on the device and assign it to our PhotoCamera instance.  Note that you can’t pick a random resolution and set it on your own whim, it has to be a resolution supported by the hardware/config of the actual handset.

You can find code on MSDN similar to the following that walks through the PhotoCamera.AvailableResolutions to discover what is available on the user’s device:

image

In my case, however, I’m not really interested in iterating through the list; I want the smallest resolution possible. LINQ to the rescue!

image

Likewise, if you want to use the highest available resolution, modify the query to use OrderByDescending instead of the straight OrderBy.  You can, of course, make that ultra-terse by making a direct assignment.

You must first initialize the camera instance before you set the resolution.  That means two things: you’re going to have to assign the camera instance to the source of a VideoBrush and you’re going to have to write an event handler for your Initialized event on the camera. As you spin up the form, you’re find yourself doing something like this:

image

…and then in the event handler you’re going to want to move back to the UI thread and set the resolution:

image

And there you have it!

Side Note of Developer Love: The Windows Phone 7 APIs Make Me Happy Inside

I just want to mention this at this point: the WP7 APIs are awesome.  I am helping to mentor a friend of mine as he broadens his skill set and I totally dig the opportunity to teach him how to write good code. We’ve the weirdest mentorship set up where I, a .NET guy for a decade now, am teaching a guy to program in Java on the Android dev stack.  Needless to say, we’re both doing a lot of learning.

So here’s the deal…unless I’m missing some magic cloud of awesomeness that the Android folks have cleverly buried away, WP7 devs seem to be at a distinct advantage.  Case in point: Camera API.  Want to display a live preview on WP7?  Create a camera instance, assign it to a brush.  Done. Android?  Get out the fairy dust and sacrifice a unicorn.  Sure there’s a ton of samples out there, but good luck finding one that follows the same kind of convention that any other sample uses, and better luck still you manage to get it to run as-is on your version of the dev tools.

At any rate, my buddy’s plugging through the Android stuff and I’m having a blast helping him through the learning curves of everything from properties and constructors through to web service access and hardware APIs.  I’m developing the app in the WP7 world and loving the experience, and he’s drooling over Blend and slugging it out in Eclipse.  He’s come along way since “what’s a class?” and it’s only been a couple of months…today we were even having a conversation about threading!  I’m looking forward to having another person in these parts whose face doesn’t glaze over when I mention “protocol” or “inheritance”.

Current Development Must-Haves for ASP.NET MVC 3

I recently wanted to replace the exhaust manifold on my vehicle. I looked at the part online, and there were maybe 8 bolts that I needed to remove/replace.  As it turns out, those 8 bolts are part of a 112 step process that takes an experienced mechanic 5-6 hours to complete. If he has the right tools.

The ASP.NET 3 Framework gives us a lot of help in achieving our web application development chores, but it’s best to have the right tools.

Here’s a current checklist (July 2011) of the tools you should have in your kit:

  • A copy of Visual Studio 2010 (trial, express, free)
  • *   <div align="left">This one's important!&nbsp; VS 2010 SP1 gives you the most updated tooling for MVC.</div> <li>The NuGet Package Manager ([plugin](http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c), [console](http://nuget.codeplex.com/releases))
    *   Also important! NuGet lets you access great libraries for your project, including MvcScaffolding, Ie9ify and more  <li>It's installed in SP1, but make sure you get the latest updates <li>The Visual Studio 2010 Productivity Power Tools ([plugin](http://visualstudiogallery.msdn.microsoft.com/d0d33361-18e2-46c0-8ff2-4adea1e34fef))  <li>An LED [Binary Clock](http://www.thinkgeek.com/homeoffice/lights/59e0/). It won't help you with MVC development, but it looks cool on your desk  <li>The Entity Framework [version 4.1](http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=26825)
    *   The "project version" binary is pulled down by new projects in MVC 3 through NuGet, but the full version adds other tooling and templates 
    

 

Also, check out this post I put together with a ton of handy links for your MVC 3 adventures.

Missing anything? What do you use on a day-to-day basis that’s not on this short-but-powerful list?

By the way, the only thing in my mechanic toolkit is a hammer and a set of pliers.  I left the exhaust manifold for the pros.

Cheers!

Questions about the ASP.NET MVC 3 Framework

Here is a collection of the most common questions I’ve run into when working with developers who are new to application development using the ASP.NET MVC 3 Framework.  I will continue to expand this list as questions come up, feel free to ask yours in the comments below!

 

Creating New Projects

What if I don’t like the look of the default project?

You really have two options here; you can edit the _Layout.cshtml file located in Views\Shared\ to make it your own (the Site.css stylesheet is in the Content folder), or you can opt to start with an empty project. You’ll get a very clean _Layout.cshtml at this point, and a really reduced Site.css to work with. Remember that getting your project going is as easy as adding controllers and views, but you won’t have the authentication bits that the Internet Application template gives you.

Is it okay to use the ASPX View Engine instead of Razor?

Of course! If you’re worried about learning the pattern as well as a new view engine, you can start with a syntax you’re more comfortable with. You will have a harder time finding source samples, however, as most folks on the MVC Framework are using Razor. Also note that there is a different paradigm at play in MVC. ASP.NET Web Forms use a PageController pattern and make use of view state. You don’t have those or page events in the same way in MVC, so don’t be surprised if you don’t find them!

Why should(n’t) I use HTML 5 semantic markup?

HTML5 and CSS 3 aren’t fully supported on all browsers and the implementations that are there aren’t yet consistent. What’s important to remember is that HTML5 is HTML4 plus an assortment of new features. If you’re targeting HTML5 and using the new features you’ll still need to write alternate views (degrade gracefully) unless you have a fixed deployment environment (like corporate intranet), however, it’s important to remember that the semantic tags _do_ add value to your page today, they are backwards compatible (especially with Modernizr) and are now starting to be better read and understood by search engines.

 

Controllers and Views

Do controller actions have to return Views?

Not at all! You can return plain text, images, PDFs, page fragments (typically called partial views), XML or JSON…pretty much whatever you like.

Navigation isn’t something that can be fully free, but it can be easy. We could create a link in our _Layout.cshtml file, and this would be a great exercise for the reader. If you’re working from a clean project, you want to add a third LI element to the NAV’s UL. In the new LI, you can follow the pattern of the other menu items and use the HtmlHelper extension method to render links. Using Visual Studio’s IntelliSense you can figure out which order to pass in the label of the link, the action and the controller you’re looking for. Or just re-read that sentence.

When I type return View() how does it know which view to return?

This, again, is part of the beauty of convention over configuration.  The view that will be returned is the one that has the same name as the method in the controller. If you have a method in the Food controller with the following implementation:

image

…then you’re going to need a view named SpicyPickles in the Food folder inside of Views.

You’re not bound by this, however, and you can return alternate views if you like by passing the name of the view as a string:

image

 

Models

Why are my model classes not showing up in the MVC tooling?

The most likely reason is that you have not built you application. The MVC tooling looks at types in your assemblies. If you haven’t built your app since you added those model types to your project (or modified them), the tooling won’t pick up all the correct bits for you.

I’m having trouble binding my model when submitting data to my controller. How do I debug the request?

There’s a couple of things you should try to do. First, get a copy of Firebug running (in Firefox) or use the developer tools in IE9 or Chrome.  You need to examine the message you’re sending to the server. Check to make sure that your parameters are named correctly, conform to the correct type and are actually capturing the correct values from the client. Set a breakpoint in your controller and examine the object. Make sure the correct action is being invoked.  Glimpse is also coming a long way in revealing how your request is seen by the server and can easily be installed via NuGet.

 

Authentication

Do I have to use the ASPNETDB.MDB that is created for me for authentication?

No, you can setup your own database following these steps.

Setting up an Alternate Data Source for your ASP.NET Authentication Provider

imageThe authentication provider that is baked in to ASP.NET for free is a great start to managing users on your site, and gives you full capabilities around roles, profiles and authorization within your application.  The default project template for internet applications includes functionality designed around it. I’m a big fan of these free jump-starts as they’re also easy to swap out.  For the purposes of this post, I’m using Visual Studio 2010, the ASP.NET MVC Framework and SQL Server 2008 Express.

By default, if you try to create an account without prior configuration, the ASP.NET bits happily go about creating a local MDF for you with all the required tables and procs.  This is great, if you never intend to save anything else in your database, you don’t wish to rename it, and you don’t want to use an existing data source, but it’s also not the only way to go about getting a data source for your authentication needs.

If you’re scratching your head after creating a user account, wondering where the MDF ended up, look no further than Visual Studio’s magic show-me-my-flipping-files button.  The data file will be in your App_Data directory, but not included by default in the project.

image

 

Provisioning Your Own Data Source

Thankfully, the good folks at Microsoft have provided us with a tool to create these tables and procs in our own databases as well.

First, we’re going to need to get a landing spot for our data, so fire up SQL Server Management Studio and connect to your favourite database server. This can be any SQL Server edition (including Express, which is free), running locally or out on your network.  Create a new database, I called mine NotJustAnAuthDatabase.

Open a Visual Studio Command Prompt as Administrator and type the following:

image

While you can pass in arguments from the command line, there’s also a wizard that launches in the absence of them.  This makes it easy to select the database and apply the authentication data model. 

Step through the wizard.  We’re going to Configure SQL Server for application services.  Notice the option to remove the service information?  That’s why I said it’s easy to swap!

image

Next, we specify the server and database name that we want to add authentication support to:

image

Click next a couple times, then finish.  Your data source is all setup!  You can check your handiwork in SSMS:

image

 

Getting ASP.NET In Tune With Our Work

There are only a couple of small steps left to point our provider at the new database.  First, we’ll need to adjust our ApplicationServices connection string.  Pop back into Visual Studio and open up web.config.  You’ll need to work out whatever your connection string will be for your server and database. Mine looked like this:

image

Next, you can delete the MDF file that was created for you (if you had one created).  Remember to use the Show All Files button to reveal the database if it does not at first appear in Solution Explorer.  Oh, and a note here: you’re deleting a database.  It may hold data, so think that through if you want something in it.

When you re-run your app you will now be attached to the freshly-configured data source for authentication, allowing registration to your new backend.

A Few Cleanup Points

First, authentication isn’t the only thing that’s covered in Application Services, there is a lot of goodness baked in there. You might consider using profiles as well.  Once a user is authenticated, you can make use of the very simple authorization attributes in ASP.NET MVC, so there’s all kinds of peripheral benefits too, not just the benefit of not writing registration/role/authentication code for users on your site.

Secondly, this isn’t new.  In fact, the first bits of these services were shared with us in late 2005 when we were introduced to the .NET 2.0 Framework.  So why am I posting about it now?  As easy as it is to use, as comprehensive as the toolkit seems, and as mature as the components are, I still run into senior developers who have never used it or explored it.  File –> New, people! File –> New!!!

Another reason is that there are lots of new developers out there who aren’t sure by looking at VS2005 docs if that material is relevant any more. Some of it isn’t. Some is, but the IDE looks different.  It may seem cumbersome at first glance to get all this going, but I’m hoping that by showing you all you have to do is run a wizard and update a string that you’ll consider giving it a try.

Finally, just remember that if you’re mid-stream on a project and you make a switch out you’re going to need to first back up your data. Setting up the service components in an alternate location doesn’t port your users or roles and if you nuke your existing data store that data will be gone.

Best of luck in your authentication adventures!