I have been playing with the new ASP.Net MVC Framework that is part of the ASP.NET 3.5 Extensions CTP release.  I really like how simplified the MVC Framework has made things.  One thing I wanted to try out is how would you leverage master pages for things that needed to be rendered on every page.  Something like a product category for an online store that only shows products that are in a given category when the user clicks on the desired category.  I wanted to be able to place this category list on the master page so it could be maintained in one place.  This blog post explains how I managed to accomplish this.

First make sure you download the ASP.NET 3.5 Extensions Preview and also the MVC Toolkit.  This example uses the Northwind database.  There are many other blog posts that explain how to create the MVC Solution so I will not go through those details here.  Basically I created an MVC Web Application and a separate Class library to hold the data model.  In the model I only added the following tables from the Northwind database: Categories, Orders, Order Details, Customers and Products.  I also made sure I had the MVCToolkit.dll referenced in my web project so I could use some of the advanced Html Helper classes.

In the web application project under the Models folder create a new class called DefaultDataView and add a property that can hold a list of Categories.  This class will be the default strongly typed data container that all other data containers will be derived from.

image

Create another data container class and call it ProductDataView in the Model folder of the web project.  This class will derive from the DefaultDataView.  Add a property to the class to hold a list of products.

image

Create a public partial NorthwindDataContext class in the model project and add a method to return the categories as a list.

image

Create a ProductController class to handle displaying the list of filtered products based on a passed in category id.  Notice how I am using the subclass ProductDataView to hold the list of categories as well as the filtered list of products.

image

Now lets get the master page to display a list of product categories.  Add a new div tag above the maincontent div tag in the Site.Master page. The logic just iterates over the list of categories emitting a link to the products list action.  Notice the use of the ActionLink method on the Html helper class.  Since the List Action on our Product Controller accepts a category Id as a parameter I can just append the category Id to the end of List and the parameter will be injected into the controller List method.

image

Create a view to display the list the products.

image

I also modified the Index and About methods on the HomeController to use the strongly typed data container and load the categories.  Since neither of these actions load any other view specific data I used the DefaultDataView as the object to transport data to the view for rendering.  

image

The end result when a user clicks on one of the categories is as follows.

image

Conclusion

As you can see it is very easy to split out common rendering logic into a master page even in the MVC Framework.  I used strongly typed data container objects as a way to send data to the view but I could have used the un-typed method just as easily.  I generally like to stay strongly typed as much as possible though.  I want compile time checks for heavy refactorying while developing in an agile environment.

Another interesting concept here was that I was able to stay away from using any code behind logic or server controls.  It is a big debate these days on if code behind should be used or not.  If you don't use code behind you will end up with some C# code mixed in with HTML which I am not convinced is good or bad.  In a lot of ways I think mixing HTML and C# makes the code hard to read.  I guess I have to try it for a while before I decide one way or the other.  The VS 2008 Intellisense support and the MVC Html helper class is what will make a mixed HTML and C# solution a success.

All in all I really like the MVC Framework and I think it will be a great tool for building complex and maintainable web applications.  I am certainly going to use this framework on future projects and I am looking forward to it's production release.

Here is the solution if you want to download it.

MvcApplication1.zip (63.25 kb)

I have been meaning to blog about one of the coolest projects I have been involved with for a while now but I have been too busy to do so.  Back in June of 2006 I got involved with Bob Pitzer (Botbash), Chris Harriman and Joel Meine, in providing a software/hardware solution for the FIRST Robotics Competition for their upcoming 2007 season.  FIRST is a non profit organization that is dedicated to teaching young minds about science and technology through several fun filled robotics competitions.  Make sure you check out their web site to see how you might be able to get involved in this great program.  Also check out the 2007 video archive to see how exciting these events can be.  Make sure you pay attention to the computer graphics that are superimposed over the live video because that is what we built! 

The software and hardware we built was used to manage multiple 3 day events over a months time frame.  This ended up being around 40 events across the US in roughly 30 days.  Each event had anywhere from 30 to 70 teams competing.  Each day of the event had to be executed in an efficient manor in order to complete the tournament style competition.  The system was designed at a high level to do the following:

  • Lead an event coordinator through the steps of managing a tournament
  • Maintain a schedule of matches over a 3 day period
  • Inform the audience of match scoring in real time
  • Broadcast live video mixed with real time match score information to the web
  • Inform the other competitors in the Pit area of upcoming matches, match results, and real time team ranking details as each match completes.
  • Control the field of play from a central location
  • Gather scoring details from judges located on the field
  • Provide periodic event reports for online viewing as the tournament progresses

All of this was done using Microsoft .Net along with various open source .Net projects to speed up the development process and provide a robust system that was easy to use by various volunteers. 

I used various pre-built components in order to build this system.  Since this application needed to interface with multiple hardware components and provide a rich user interface a Windows Forms application was going to be required.  I chose the Patterns and Practices Smart Client Software Factory (SCSF) as the basis for the Windows Forms framework.  I had used this Framework in a previous project and I knew it would give me the modular design I needed to accomplish the functional goals of the design.  I wanted a solution that allowed for me to do a lot of Unit Testing as I was going to be the only developer doing the work and the amount of QA testing was going to be very small.  Since the SCSF used a Model View Presenter pattern I knew that testing would not be an issue.  Also SCSF uses a dependency injection pattern that would also lend itself very well for unit testing.  Another benefit of the dependency injection pattern was that I could mock out some of the hardware interfaces so that I did not have to have a fully functional robot arena in my home office!  I actually developed the hardware interface without ever connecting to the hardware on my development machine.  This was done by establishing a good interface and using a mock implementation of this interface to complete all the business logic without having any hardware.  Then at a later date we implemented the real hardware layer and even to this day I use the mock implementation for all development work since I do not have an arena in my office. 

For the data access layer I choose to use SubSonic as it provided a very fast way to generate the data access layer from a database schema.  Using SubSonic gave me the flexibility to grow the data model really fast as the solution emerged over time.  The database back end was SQL Server Express 2005.  Since the solution only required a small set of clients and it had to be disconnected from the Internet  SQL Server Express was right for the job. 

Deployment of the application was done with ClickOnce in a full trust environment.  This enabled me to make changes to the application throughout the tournament and the software on each playing field computer remained at the most recent version.  The click once deployment also managed the upgrade process for any database changes as well. 

The central control of the field of play was handled by a single .Net Win Forms application that interfaced to Programmable Logic Controllers (PLC) via a third party managed library.  This library allowed for me to set PLC memory locations as well as monitor locations without having to worry about the TCP/IP communications protocol.  This was a great time saver as I could concentrate on high level business value rather than low level communications.  Since the low level communications was not required I did not have to spend a lot of time with debugging hardware/software integration problems.

Another key area of integration was providing a Hardware UI that consisted of LCDs and Buttons that enabled a field operator to manage the match process without using the computer keyboard or mouse.  This was done using a serial port communicating to a BX24 from Netmedia.  The user would actuate buttons on the hardware UI in order to start or stop the match as well as many other tournament related functions.  The hardware UI would lead the operator to the next step by flashing the most appropriate button for the current point of the match.  This made the operators job a lot easier. 

The audience needed to be informed about what is going on during the event.  An announcer was always present at these events but the audience also needed visual cues that made it apparent what was going on.  So I created a win forms audience display application that would provide the detail the audience needed.  This detail was not only displayed to the live audience but it was also broadcast over the Internet to individuals that where not able to attend physically.  This audience display showed live video as well as match statistics mixed together on one screen (you can see this in action in the video links I mentioned above).  This screen was projected up onto a huge screen so all audience members could see with great ease.  The live video mixing was done using a green screen technique that is often used with the weatherman on local news stations.  Basically a green color is used in a color keying process to superimpose the live video over the green color.  The screen snapshots below give you an idea of the type of information that was presented to the audience as well as the web broadcast.

 

2007-02-21_135125      2007-02-21_135226

AlliancePairingSample      2007-02-21_135201

Well I could go on and on about details of the application we wrote to make the 2007 FIRST FRC event a great success but I think I will save it for a set of later posts.  Also I have been working on the software for the 2008 season that uses WPF for an even more richer user experience (can anyone guess I used some animations!).

I have been doing some BX24 development again lately.  I have also been reading a lot about the new shell support that Microsoft has pre-released called PowerShell (formerly known as Monad).  Well since I have been using the same batch files and VBScript files to manage my build process for BasicX source since 2001 I thought it might be time to look at another alternative. 

I need to be able to do the following:
  • Perform command line compiles of the BX24 project
  • Allow for the source to reside anywhere on the hard drive and still be able to compile.
  • Initiate a compile of all BX24 projects so I do not have to do them one at a time
  • Parse the BasicX.err file to determine if the compiler found errors
  • Launch an editor that shows the BasicX.err file only when an error exists
  • Be able to manage some registry entries specific to the BasicX IDE
  • Have a limited set of scripts that do not require any changes to support the build process
  • Allow for multiple project files to co-exist in the same folder. This means I need to save off the BasicX.err file into another file if I want to preserve what the results where from the compile.

After reading some about PowerShell it was very apparent that it would support anything I needed to do.  The main huddle I needed to over come was learning the syntax that revolved around PowerShell.  Fortunately it is based on the .Net framework so the majority of it was fairly easy to adjust to. 

Since I already had a VBScript file that did most of the above tasks I started dissecting what it did first.  The last time I touched this script was in 2001.  The script did the pieces around changing the registry entries and launching the compiler but it had no support for parsing the error file and managing many project files.  Here is the script that I ended up with:

param ([string]$WorkingDirectory)
# Define some script variables$chip_type="BX24"
# Save the current dirrectory so we can return to it
Push-Location
# If a working directory was passed in lets change to it
If ($WorkingDirectory){Set-Location $WorkingDirectory}
# Get the project files to process
$projectFiles = Get-ChildItem *.bxp 
foreach ($project in $projectFiles){$project_file = $project.name.split(".")[0]
# Use the current directory as the working directory
$work_dir = $project.DirectoryName
# Set some registry entries for the basicx IDE
$configEntry = "hkcu:\software\vb and vba Program Settings\basicx\config"
Set-ItemProperty ($configEntry) -Name Chip_Type -value 
$chip_typeSet-ItemProperty ($configEntry) -Name Work_Dir -value 
$work_dir
# determine from the registry where the basicx executable is installed
$program_dir = Get-ItemProperty ($configEntry) -Name Install_Directory
# Map the P drive to the basicx install directory for convieniance
if (Test-Path p:) {}else {subst P: $program_dir.Install_Directory}
# Remove the error file if it exists
if (Test-Path basicx.err){del basicx.err}
if (Test-Path ($project_file + ".err")){del ($project_file + ".err")}
# Launch the compiler
P:\basicx.exe $project_file /c
# Wait for the compiler to finish
$processToWatch = Get-Process basicx$processToWatch.WaitForExit()
# Unmap P: drive
if (Test-Path p:){subst P: /d}
# Check for errors and launch the error file if some do exist
$CompileResult = get-content basicx.err
If (($CompileResult -match "Error in module").Length -gt 0){notepad basicx.err}
# Copy the error file off so it does not get overwritten when multiple
# projects are being compiled in a single directory
copy-item basicx.err -destination ($project_file + ".err")} 
# Restore the original location
Pop-Location

Well that was pretty painless.  I basically had a script that managed processing all BasicX project files in a given folder.  Next I needed to have another script that found all the project folders for a given folder.  This also meant processing projects in sub folders.  This higher level script would launch the script above to do the compile.  I ended up with the following script:

# Save the current dirrectory so we can return to it
Push-LocationSet-Location ..\
# Get a list of all projects
$project_Files = Get-ChildItem -recurse -include *.bxp | sort $_.DirectoryName$lastDir=""
foreach($project in $project_Files)
{
# Since we can have multiple projects in a folder and we send the
# working folder to the build script we want to skip folders we already
# processed
if ($lastdir -ne $project.DirectoryName)
{./tools/build $project.DirectoryName  $lastDir = $project.DirectoryName}}
Pop-Location

Well that too was pretty easy.  I am beginning to really respect the power of PowerShell.  I can do so much more than what I was able to do with VBScript and do it easier.  Later I will but together a sample BX24 project showing how I use these scripts and the folder structure I place them in.