18 simple rules

A friend blogged this recently and it struck such a chord that I thought I’d repeat here in entirety rather than simply link. At the start of the new millennium the Dalai Lama apparently issued eighteen rules for living. Here they are.

1. Take into account that great love and great achievements involve great risk.
2. When you lose, don’t lose the lesson.
3. Follow the three Rs:
Respect for self
Respect for others
Responsibility for all your actions.
4. Remember that not getting what you want is sometimes a wonderful stroke of luck.
5. Learn the rules so you know how to break them properly.
6. Don’t let a little dispute injure a great friendship.
7. When you realize you’ve made a mistake, take immediate steps to correct it.
8. Spend some time alone every day.
9. Open your arms to change, but don’t let go of your values.
10. Remember that silence is sometimes the best answer.
11. Live a good, honourable life. Then when you get older and think back, you’ll be able to enjoy it a second time.
12. A loving atmosphere in your home is the foundation for your life.
13. In disagreements with loved ones, deal only with the current situation. Don’t bring up the past.
14. Share your knowledge. It’s a way to achieve immortality.
15. Be gentle with the earth.
16. Once a year, go someplace you’ve never been before.
17. Remember that the best relationship is one in which your love for each other exceeds your need for each other.
18. Judge your success by what you had to give up in order to get it.

One-Pot MVC

I’m going to be giving a talk at “The Stack” in August. If you’ve never heard of it, this is a monthly user group for developers in the Liverpool area who use Microsoft technologies. I’ll be talking about some of the extensibility points in the ASP.NET MVC framework, and hopefully my summary for the evening’s agenda will give you a “taste” of what I’ll be covering:

One-Pot MVC: Deliciously Extensible

“Take a rich stock of MVC, and stir in some validation rules. I suggest regular model annotations are substituted with FluentValidation libraries, for added flavour. While your controller is simmering prepare unit tests, but be careful your project manager doesn’t boil over! Choose your favourite IoC container (NInject works well) and mix in concrete dependencies as required.
Finally, add a pinch of MvcContrib and serve on time and in budget.”

I’ll provide another update with dates and details of other speakers as it gets finalised.

TechDays 2011

Today a contingent from the Mando Group programming team took an excursion to the Microsoft TechDays event in Fulham to hear about a cross section of innovations in the Microsoft Web platform. There will be further presentations over the next few days on the Windows Azure platform, Silverlight and Windows Phone 7, and some of the team will be blogging about these topics later in the week. Those of us present for today’s Web track heard talks on a broad mixture of web related subjects. The highlights for me were:

Bruce Lawson gave an outstanding overview of the development of the HTML 5 standard, covering the history, politics and relationships between the various individuals and working groups involved, then moving on to explain what HTML 5 really is. The key take home messages for me were that HTML 5 standard succeeded over XHTML 2 because

  • it is backwards compatible,
  • is pragmatic (vs XHTML 2’s idealistic, puritan stance),
  • puts the web user ahead of the author, developer or any other party
  • allows for errors in authored markup. This is an inevitable consequence of human developer error, content management system limitations and 3rd party plugins, but importantly the standard describes exactly how the browser parser should respond to these errors, removing the problem of inconsistent DOM rendering between browser implementations.

Bruce also helped to dispel the concerns about HTML5 being an ‘unfinished’ standard by pointing out that while some parts of the standard are still under development, others are completed and already being used across the web in browser implementations and sites.

Martin Beeby gave a presentation on developing the TechDays website using Umbraco, an open source .NET Content Management System which we work with at Mando Group, and hosting the solution on the Windows Azure platform. The most exciting aspect of this is that Umbraco v5, due for release in June, will make this hosting configuration considerably easier to set up and scalable support for cloud deployment will be baked into the core product. We also learned that the website for the latest “Take That” tour was powered by Umbraco, and at peak times when ticket sales were announced the Umbraco site continued to perform while the separate ticketing site (not using Umbraco) crashed!

The other session which I personally found really interesting was the MVC3 update by Steven Sanderson. In contrast to last year’s MVC2 update, the new additions in MVC3 are not so much around the core framework, but more around the tooling and development infrastructure surrounding ASP.NET, but these are major improvements in their own right. The addition of scaffolding (another tip of the hat to Rails) and Entity Framework code first development make it faster than ever to get a data-driven application up and running, and the speed with which you can architect apps by pulling in components using the Nuget extension in Visual Studio will save many hours of work for developers.

The example app developed by Steve used EF4 with the repository pattern to separate data access concerns, and NInject to resolve dependencies within the app, exactly the architecture we have already used successfully at Mando Group for our last major MVC implementation for a client. It’s lovely to see how the ASP.NET MVC approach naturally leads the developer down the route of better architected web apps. Steve also covered some of the basics of Razor syntax, SQL Server CE (Compact Edition) and IIS Express (based on IIS 7.5). Again, all great additions to the Microsoft web stack which mean the developer has to spend less time thinking about underlying setup and infrastructure, and more time writing code which solves real business need.

Extension-less URL’s in Umbraco

Umbraco has built-in support for human-readable, SEO-friendly URL’s and in addition supports the omission of the .aspx extension, so that for example instead of http://www.mysite.com/news.aspx you could have the URL http://www.mysite.com/news/

In order for the CMS to generate links in the navigation which don’t have the .aspx extension, you need to find the following key in the appSettings element of the web.config and set the value to true:

<add key="umbracoUseDirectoryUrls" value="true" />

Once you set this key, the CMS generates aspx-free URL’s within all navigation templates. However depending on the version of IIS you’re running on, these extension-less requests won’t necessarily be processed by ASP.NET correctly without a little further work. If you’re running IIS7 in integrated mode, it should work automatically. However, IIS 6 will think you’re just requesting a folder or file, and won’t invoke ASP.NET to process the request.

A more detailed article on this can be found here: Setting up Umbraco for friendly URLs

Wildcard mapping in IIS6 works, but is a sledgehammer approach. It means that ALL requests get sent from IIS to ASP.NET. ASP.NET will be slower to process requests for things like images and files, and therefore it’s generally better to let IIS deal with these where possible.

A better approach is to use an ISAPI extension such as Helicon’s “ISAPI_Rewrite”, and to add a URL rewrite rule that follows the logic “if this request has no file extension, then add .aspx on to the end before IIS gets hold of it”.

Here’s an example set of rules (taken from this article about clean URL’s on the Umbraco forums):

[ISAPI_Rewrite]

#exclude the /umbraco folder from this filter
RewriteRule ^/(umbraco.*)$ /$1 [L]
#include everything else from the website
#everything without extension will have .aspx
RewriteRule ^/([^.?]+)/?(?.*)?$ /$1.aspx$2 [L]

 

Additional Umbraco specific appSettings

“umbracoXsltPath” – by adding this to appSettings you can specify a new/different folder for storing your xslt files and still be able to edit them within Umbraco. Default folder is ~/xslt.

“umbracoUsercontrolsPath” – by adding this to appSettings you can specify a new/different folder for storing your UserControls. Default folder is ~/usercontrols.

“umbracoMasterPagesPath” – by adding this to appSettings you can specify a new/different folder for storing your Masterpages. Default folder is ~/masterpages.

“umbracoCssDirectory” – by adding this to appSettings you can specify a new/different folder for storing your css-files and still be able to edit them within Umbraco. Default folder is ~/css.

“umbracoMediaPath” – by adding this to appSettings you can specify a new/different folder for storing your media files. Default folder is ~/media.

“umbracoScriptsPath” – by adding this to appSettings you can specify a new/different folder for storing your javascript files and still be able to edit them within Umbraco. Default folder is ~/scripts.

Customising the RTE (Rich Text Editor) in Umbraco

Umbraco uses TinyMCE for the rich text editor data type (the same JavaScript RTE as many other CMS’s including some of the Enterprise products).

In terms of customising the RTE, generally most of what’s in the TinyMCE Wiki will apply to Umbraco installations. However, there are also plenty of examples of Umbraco-specific customisation:

  • Adding custom styling options to the ‘Styles’ drop-down:

Customised TinyMCE Editor to Allow Heading Tags to be applied

  • Allowing users to insert their own micro-templated regions within the RTE:

Enabling the ‘Template’ plugin for TinyMCE

How to apply a toolkit theme in Silverlight 4

I just had to apply a theme to a simple Silverlight 4 application I’m developing for Labs, and it took me a bit of research to find what I was looking for, although the steps turned out to be very simple in the end. I thought it would be worth sharing what I’d learned in a single post.

The Silverlight 4 Toolkit contains some great controls and themes that extend what’s already available out of the box with Silverlight. If you’re not aware of what the toolkit offers, a great starting place is this online interactive demo which shows everything that’s included and gives code samples showing how to implement.

So the steps I took to apply one of the toolkit themes to my app were as follows:

  1. Download the toolkit from Codeplex and install it on your development workstation. By default, the toolkit files will be installed in the directory “C:Program FilesMicrosoft SDKsSilverlightv4.0ToolkitApr10″.
  2. In your Silverlight application project, right-click the ‘References’ folder and click ‘Add Reference’
  3. Add a reference to “System.Windows.Controls.Theming.Toolkit.dll” in the “Bin” subdirectory
  4. Add a reference to the specific theme .dll file you wish to use in your application. For example, to use “Rainier Orange” add a reference to “System.Windows.Controls.Theming.RainierOrange.dll” in the “Themes” subdirectory.
  5. In your App.xaml file, add the following namespace declaration to the outermost “Application” element: xmlns:toolkit=”http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit”
  6. Finally, to actually use your chosen theme throughout your application, add the following declaration below the xmlns declaration above:

toolkit:RainierOrangeTheme.IsApplicationTheme=”True”
And that’s the most straightforward route to apply a theme from the toolkit globally across your Silverlight 4 applications. There are many other options of course, and credit here to David Anson’s blog post which goes into more detail on the release notes, and helped me with steps 5 and 6 above.

DevWeek 2009

On Tuesday I caught an early train to London and arrived at the Barbican estate for ‘DevWeek’, a 3-day series of technical seminars. Let’s get one thing out of the way; the Barbican estate is architectural lunacy. I never expected such an entertaining venue. Huge modernist residential blocks enclosing an open precinct, with yet more flats erupting out of a vast central lake on concrete pillars. In theory, it’s a monstrosity. In the flesh, it was a tranquil backdrop for some inspiring tech-talk.

Tuesday’s highlight was the ASP.NET Model-View-Controller framework, which was released at the Mix ’09 conference last week. There’s nothing new about the MVC pattern, which was first conceived and documented in the 70’s, and a large number of web frameworks already operate around this pattern. Microsoft seem to have been ‘watching the tail lights’ of others such as Rails before committing to an implementation of their own. The same could be said for the ADO.NET Entity Framework as an O/RM toolset and ASP.NET Dynamic Data for scaffolding, all of which featured during the conference. As regards the MVC framework I was surprised by the number of speakers, even from within Microsoft, who conceded that the ASP.NET web forms model is not well suited to delivery of clean, accessible HTML markup, and makes unit testing extremely difficult. They may be late to the party, but the ASP.NET MVC framework is a massive leap in the right direction for ASP.NET developers and makes all these problems go away.

The high point of Wednesday was a seminar delivered by ThoughtWorks’ Neal Ford on the mechanics of programmer productivity, although in fact most of the subject matter was applicable to non-programmers. I’d urge anyone who has an opportunity to attend one of Neal’s talks to jump at the chance. The piece that really struck a chord was the reference to the psychological phenomenon of flow (aka being ‘in the zone’), and what practical steps can be taken to aggressively guard this most productive, and most fragile, states of mind.  Neal also gave a raft of practical advice to accelerate the process of getting stuff out of your head and into your computer, such as memorising keyboard shortcuts and switching your mouse to the opposite hand to discourage yourself from using it!

The take-home message from Thursday was Kevin Jones’ session on designing code to facilitate unit testing. Kevin demonstrated how to recognise code which would make unit testing difficult, and how to refactor to avoid this problem. He also covered design patterns such as inversion-of-control which can make code unit test-friendly from the ground up, and tools such as Microsoft’s Unity which take the legwork out of dependency injection.

Interestingly, a lot of people were Twittering during and after the seminars using the #devweek hashtag. Despite being initially sceptical of the micro-blogging platform, I was fascinated with the way a small virtual community sprung up around the physical event and I was able to read their session feedback in real time. My only regret from the whole conference is that I couldn’t be in all 8 sessions which were running at any one time, so I’ve already started reviewing the slides from the ones I missed. See you at DevWeek 2010!

Referencing parent repeater items from server controls

This is something I’ve had to do a couple of times in the past, and someone else in work had the same requirement last week. The situation this resolve is that you’ve got a repeater on your page, but within this you have other controls which need to be aware of their context, for example to amend their behaviour according to which item is being rendered.

In this example, I wanted the last item in my unordered list to have a different look/feel to the other items, which was achieved using a couple of different placeholders and attaching a different style to the last item only. The “Visible” property of each placeholder is then set accordingly:

<asp:Repeater id=”myRepeater” Runat=”server”>
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<asp:PlaceHolder ID=”normalItem” runat=”server” Visible='<%#Container.ItemIndex != ((ICollection)((Repeater)Container.Parent).DataSource).Count – 1%>’><li><%Eval(“SomeField”) %></li></asp:PlaceHolder>
<asp:PlaceHolder ID=”lastItem” runat=”server” Visible='<%#Container.ItemIndex == ((ICollection)((Repeater)Container.Parent).DataSource).Count – 1%>’><li class=”last”><%Eval(“SomeField”) %></li></asp:PlaceHolder>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

Web.config security issue

A colleague raised an interesting security ‘gotcha’ related to web.config files last week. Apparently this particular gem was raised at a recent Microsoft conference in London Village and there was a collective ‘gasp’ from the audience as a roomfull of developers realised that at some point they’d probably left at least one of their web applications open to the world’s easiest hack.

In a nutshell, never rename your web.config files to web.config.old, or web.config.bak etc. Unless you’ve explicitly prohibited access to files with this extension, IIS will quite happily serve these files up as readily as it would .html or .jpg files, potentially leaving all the database connection strings, server addresses and other goodies you put in your config files unprotected. Apparently hackers are already well onto this one, and have developed bots to scour sites looking for these renamed files. You have been warned!