This week I've got the "luxury" of working from home. After my last 1-2 week stint of this back in March, I've decided to tackle things a little differently.

Firstly, I'm not breaking my routine! I've been going to the gym every morning for the past 7 weeks. It's really helped me feel better about myself. Last time I was working from home, I decided to give the gym a break for a little. What happened next was that I stopped going all together for about 3 months. So, breaking my routine is not going to happen this week.

Secondly, have a five minute break every hour. This is something that really got lost for me last time. I ended up sitting in my chair for 6 hours straight every day, not having anything to eat and relying on a bottle of water to keep me going. This is very bad behaviour and will really affect me now that I'm not changing my gym routine. Taking a quick walk around the house for five minutes every hour and ensuring I get some food into me every 2 hours is goal I will be actively pursuing.

Lastly, switch off the computer at 6PM every night, even if it doesn't stay off all night. This essentially reboots my brain. :)

So, what will I be doing this week...? Today I'm doing some more PD around the ASP.NET MVC. Hopefully I'll get some time to hit Silverlight 2, but at this stage it's not looking likely. Tomorrow I'll be doing some work on developing our new Professional .NET Course.

I'll try to keep active on my blog and I'll be twittering away as much as possible.

Last week I posted about my PD experience, including work on the new ASP.NET MVC. In this post I mentioned that there is a bug in the MVC that doesn't allow you to access control on an MVC content page from the code-behind.

Well, today I found a post on Troy Goode's site (that I should have found a week ago... :-)) that shows you how to implement the MVC Template Fix to get around this bug by using the "Convert to Web Application" function in VS2008 or by fixing the template files included in the MVC. This fix basically involves adding a designer code file in the page templates and referencing it in the vtemplate file.

Apparently this will be fixed in the next release of the MVC, for those who can wait...

Ok, so I just posted less than an hour ago, but now I've found something worth talking about. :)

After working my way through Phil Haack's blog on TDD with DI using StructureMap, I found a few things that I thought may be helpful.

Firstly, don't try to use a LINQ to SQL data context as your concrete implementation of an interface. Apparently that doesn't work. :) I tried this and received a StructureMapException:

StructureMap Exception Code: 155 - An exception occurred while trying to create an InstanceFactory for PluginType MvcApplication.Models.IPostRepository,MvcApplication

The inner exception was:

StructureMap Exception Code: 200 - Could not find an InstanceMemento for the requested InstanceKey "LINQToSQL" of of PluginFamily MvcApplication.Models.IPostRepositor

Nasty... As soon as I swapped in a concrete implementation called PostRepository that simply called the methods I had created on top of the data context, everything worked.

Secondly, following the code for the StructureMapControllerFactory object will lead to a compile error because you need to specify a return type from the ObjectFactory.GetNamedInstance method. I used the generic version because I'm too lazy cast now days. That makes the code inside the try block:

return ObjectFactory.GetNamedInstance<IController>(controllerType.Name);

Lastly, if you want to use code instead of a configuration file for the StructureMap configuration, add the following to your Global.asax Application_Start event handler:

StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;

StructureMapConfiguration.BuildInstancesOf<IPostRepository>()

    .TheDefaultIsConcreteType<InMemoryPostRepository>;

StructureMapConfiguration.BuildInstancesOf<IController>()

    .TheDefaultIsConcreteType<BlogController>;

So now I have a functional website that uses DI and is unit testable. Hope this helps someone else out there...

After a great start to the new year, I've taken the first three days back at work as PD days to catch up on a few things I've been missing out on. I've had to push my learning curve to the limit, but I've managed to cover off quite a few topics and summarise it all into one not-so-short post.

The gold resource for the week has been the new VS2008 Training Kit that includes a set of presentations, hands-on labs and demos. The presentations I've looked at haven't been much of a help to me at all, but the hands-on labs and demos have been key to me while learning about these technologies.

The first topic I was concerned with was the new features of C# 3.0 and .Net 3.5. I've heard and seen a lot about them, but I've never had a chance to sit down and get to know them. The What's New in C# 3.0 lab in the training kit covers these topics and gives an excellent introduction to how to use them. It takes about an hour to work through an introduces the following concepts:

  • automatically implemented properties - public int CustomerID { get; private set; }
  • object and collection initialisers - Customer c = new Customer { Name = "John", Location = "London" }
  • implicitly typed local variables and arrays - var complexList = new SortedDictionary<string, List<DateTime>>()
  • extension methods - public static List<T> Append(this List<T> a, List<T> b) { ... }
  • lambda expressions - customerList.FindAll( c => c.Location == "London");
  • expression trees - Expression<Func<int, int>> addOneExpression = n => n + 1;
  • anonymous types - var customer = new { Name = "John", Location = "London" };

Next up was AJAX. I've had some experience in the past with very very very early AJAX (i.e. XmlHttp... :-)) and know about how it all works, but I've never had the chance to put together a website that utilises it. Once again, the training kit labs came in very handy here. The Introduction to ASP.NET AJAX lab was a great introduction to implementing an the ScirptManager and UpdatePanel controls in an existing website to add AJAX functionality and introduced other concepts such as connecting to WCF services from JavaScript, using LINQ data sources and the AJAX Control Toolkit. The second lab on the topic was Building AJAX/JSON Services Using WCF, which showed the power of the combination of AJAX and JSON. However, I think it was a bit confusing in that it utilised the AJAX Control Toolkit too much. I would have liked to actually see some JSON strings flying around. :-)

I've also been concentrating some effort on the new ASP.NET 3.5 Extensions. If you haven't heard about it before, it's basically a collection of toolkits that new functionality being added to ASP.NET 3.5 and ADO.NET in 2008. These functions include:

  • ASP.NET MVC
  • ASP.NET Dynamic Data
  • New additions to ASP.NET AJAX
  • ADO.NET Entity Framework
  • ADO.NET Data Services
  • Silverlight Controls for ASP.NET

The download website has a few videos on it introducing all of these functions along with a link to the quickstarts that do a great job of diving deeper. I actually used ScottGu's blog more... :)

The area I've concentrated on is the MVC. I started by going through the a few posts ScottGu made a little while back (all in this post). These are an excellent starting point for someone wanting to get their hands dirty. However, there are a few small issues with some of the code that is probably due to it's pre-release nature (or Works on My Computer syndrome :-)). The major issue was that controls on an MVC content page are not accessible in the code-behind. You'll see this if you follow the sample in Scott's Part 1 blog post and try to render the Category list using a ListView control because when it comes time to set the list's data source there is no list in Intellisense. Also, the TestViewEngie class mentioned when developing tests for the controller using an IViewFactory is not available in the MVC framework. Phil Haack has blogged some excellent methods for TDD with DI and Testing Routes using Rhino Mocks and StructureMap.

One thing to watch out for in URL routing is that the routes are taken in the order they are created. For example, in Scott's walk through he mentions tweaking the routing rules to add the ability to route /Products/List/Beverages to pass the category to the List action in the Products controller. This would be done easily by adding the following route code to the Global.asax file's Application_Start event handler:

RouteTable.Routes.Add(new Route
{
    Url = "/Products/List/[category]"
    Defaults = new { controller = "Products", action = "List", category = (string)null },
    RouteHandler = typeof(MvcRouteHandler)
});

I added this under the "/[controller]/[action]/[id]" and whenever I went go to /Products/List/Beverages I would receive an error because the category parameter for the List action was null. Moving the route to the top of the method (i.e. so that it is the first added to the list) fixed this. This behaviour is mentioned in the quickstarts:

"The order in which Route objects appears in the Routes collection is significant. Route matching occurs from the first route to the last route in the collection. When a match occurs, no more routes are evaluated. Typically the default route will be the last route."

By the way, I really suggest watching the video on Dynamic Data available on the download website because it just rocks! It's very simple and very powerful. When you first run a project it builds all the controls and pages requires by the data model. It then allows you to easily extend your data model by using partial methods and add validation and rendering hints using attributes on partial classes. You can also go through and modify all the generated pages. Definitely one of the next stops on my PD roadmap... :-) Once again, Scott's got good blog post with a walk-through and some excellent links.

I'm going to finish off on a cool little download I found the other day. It's a two page poster of some of the most useful default key bindings (i.e. keyboard shortcuts) available in C# with Visual Studio 2008 available at the Microsoft Download Centre.