<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Professional</title>
        <link>http://blog.colinmackay.net/category/1.aspx</link>
        <description>If you only ever want posts related to software development, computing in general, notices of developer events or any of my articles or presentations subscribe to this feed.</description>
        <language>en-GB</language>
        <copyright>Colin Angus Mackay</copyright>
        <managingEditor>colin.mackay@gmail.com</managingEditor>
        <generator>Subtext Version 1.9.0.27</generator>
        <item>
            <title>My reading list #1</title>
            <link>http://blog.colinmackay.net/archive/2010/03/01/My-reading-list-1.aspx</link>
            <description>&lt;p&gt;I’ve decided that I need a quick and easy way to remind myself of the useful articles that I’ve read recently or are on my stack to read. Since I use my blog as an aide memoire anyway, I thought why not just put up a blog post once in a while after reading something useful. So here’s the first one…&lt;/p&gt;  &lt;h2&gt;Web&lt;/h2&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://dotnetslackers.com/articles/aspnet/The-Kayak-Framework-An-easy-way-to-speak-HTTP-with-NET.aspx"&gt;&lt;strong&gt;The Kayak Framework: An easy way to speak HTTP with .NET&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;Kayak is a lightweight HTTP server for the CLR, and the Kayak Framework is a utility for mapping HTTP requests to C# method invocations. With Kayak, you can skip the bulk, hassle, and overhead of IIS and ASP.NET. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven"&gt;&lt;strong&gt;REST APIs must be hypertext-driven&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;What needs to be done to make the REST architectural style clear on the notion that hypertext is a constraint? In other words, if the engine of application state (and hence the API) is not being driven by hypertext, then it cannot be RESTful and cannot be a REST API. Period. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://exyus.com/articles/rest-the-short-version/"&gt;&lt;strong&gt;REST – The Short Version&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;Getting a clear handle on the definition of the REST architectural style can be daunting. While there is no shortage of descriptions available, I did not find many of them helpful at first. Also, as I began talking about REST to colleagues, I often had a difficult time producing clear descriptions for the key points. Over time, however, I sharpened my summary into a version that seemed to make sense to most of my listeners. I offer here my rendition of the REST model. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://www.nordsc.com/blog/"&gt;Applying the Web to Enterprise IT&lt;/a&gt;&lt;/strong&gt;       &lt;br /&gt;This is a blog that contains a number of useful article on ReST. &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.engageinteractive.co.uk/blog/2008/06/19/tutorial-building-a-website-for-the-iphone/"&gt;&lt;strong&gt;Building a website for the iPhone&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;This tutorial will cover the basic setup and creation of a web page for the iPhone that will detect and change the content based on the phones orientation. &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Professional Development&lt;/h2&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://successfulsoftware.net/2010/02/16/unskilled-and-unaware-of-it/"&gt;&lt;strong&gt;Unskilled and unaware of it&lt;/strong&gt;&lt;/a&gt;       &lt;br /&gt;People tend to hold overly favourable views of their abilities in many social and intellectual domains. The authors suggest that this overestimation occurs, in part, because people who are unskilled in these domains suffer a dual burden: Not only do these people reach erroneous conclusions and make unfortunate choices, but their incompetence robs them of the metacognitive ability to realize it. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://www.skorks.com/2010/02/did-your-boss-thank-you-for-coding-yourself-to-death/"&gt;Did your boss thank you for coding yourself to death?&lt;/a&gt;&lt;/strong&gt;       &lt;br /&gt;Here is some perspective, you're not doing this for yourself, you're doing it for "the man". Admittedly he might be a nice man, but you don't owe him slavish commitment. Here is even more perspective, how often are you actually playing with interesting problems and cool tech and how many times are you churning out code desperately trying to get something delivered and meet some arbitrary deadline that someone has assigned to you?       &lt;br /&gt;&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blog.colinmackay.net/aggbug/11624.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2010/03/01/My-reading-list-1.aspx</guid>
            <pubDate>Mon, 01 Mar 2010 07:58:28 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/11624.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2010/03/01/My-reading-list-1.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/11624.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Integrating Exceptioneer with OpenRasta</title>
            <link>http://blog.colinmackay.net/archive/2010/02/09/Integrating-Exceptioneer-with-OpenRasta.aspx</link>
            <description>&lt;p&gt;[NOTE: This post was created using OpenRasta 2.0 RC (rev 429)]&lt;/p&gt;  &lt;p&gt;One service I’ve found to be increasingly useful is &lt;a href="http://www.exceptioneer.com/" target="_blank"&gt;Exceptioneer&lt;/a&gt; by &lt;a href="http://www.pixelprogramming.com/" target="_blank"&gt;Pixel Programming&lt;/a&gt;. As I’m about to start a new project using &lt;a href="http://www.openrasta.com/" target="_blank"&gt;OpenRasta&lt;/a&gt; I wanted to be able to use Exceptioneer with it in order that I can log any exceptions effectively.&lt;/p&gt;  &lt;p&gt;For a basic 404 error it was very easy. Just following the instructions on the Exceptioneer site for the ASP.NET integration worked a treat.&lt;/p&gt;  &lt;p&gt;However, a little more work was required for when something like a Handler in OpenRasta threw an exception that didn’t get caught. In this case I had to set up an OperationInterceptor in order to catch the exception and send it to Exceptioneer.&lt;/p&gt;  &lt;p&gt;Here is the ExceptionInterceptor class:&lt;/p&gt;  &lt;pre&gt;class ExceptionInterceptor : OperationInterceptor 
{
    readonly IDependencyResolver resolver;

    public ExceptionInterceptor(IDependencyResolver resolver)
    {
        this.resolver = resolver;
    }

    public override Func&amp;lt;IEnumerable&amp;lt;OutputMember&amp;gt;&amp;gt; RewriteOperation
        (Func&amp;lt;IEnumerable&amp;lt;OutputMember&amp;gt;&amp;gt; operationBuilder) 
    {
        return () =&amp;gt;
        {
            IEnumerable&amp;lt;OutputMember&amp;gt; result = null;
            try
            {
                result = operationBuilder();
            }
            catch (Exception ex)
            {
                Client exceptioneerClient = new Client();
                exceptioneerClient.CurrentException = ex;
                exceptioneerClient.Submit();
                throw;
            }
            return result;
        };
    }
} &lt;/pre&gt;

&lt;p&gt;Note that you have to include &lt;code&gt;using Exceptioneer.WebClient;&lt;/code&gt; at the top of the file.&lt;/p&gt;

&lt;p&gt;What this gives us is the ability to log any exception that is left uncaught from the Handler, log it then allow OpenRasta to continue on as it would have normally.&lt;/p&gt;

&lt;p&gt;All that remains is to wire this up. In the Configuration class (if you’ve used the Visual Studio 2008 project template, or what ever your IConfigurationSource class is called otherwise) the following is added to the Configure method:&lt;/p&gt;

&lt;pre&gt;ResourceSpace.Uses.CustomDependency&amp;lt;IOperationInterceptor,
    ExceptionInterceptor&amp;gt;(DependencyLifetime.Transient);&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Now any time a handler has an uncaught exception, it will be logged and sent off to Exceptioneer.&lt;/p&gt;

&lt;p&gt;Further reading:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blog.robustsoftware.co.uk/2009/12/better-actionresult-open-rasta-edition_15.html"&gt;A better ActionResult: Open Rasta edition (part 2)&lt;/a&gt; by Garry Shutler &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blog.exceptioneer.com/blog/using-exceptioneer-to-log-handled-exceptions/" target="_blank"&gt;Using Exceptioneer to log handled exceptions&lt;/a&gt; on the Exceptioneer Blog &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blog.colinmackay.net/aggbug/11408.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2010/02/09/Integrating-Exceptioneer-with-OpenRasta.aspx</guid>
            <pubDate>Tue, 09 Feb 2010 23:46:52 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/11408.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2010/02/09/Integrating-Exceptioneer-with-OpenRasta.aspx#feedback</comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/11408.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Tip of the Day #17: Duplicate input fields</title>
            <link>http://blog.colinmackay.net/archive/2010/02/06/Tip-of-the-Day-17-Duplicate-input-fields.aspx</link>
            <description>&lt;p&gt;Don’t allow duplicate input fields into your form.&lt;/p&gt;
&lt;p&gt;The other day I was trying to debug a bug in an application that I maintain. The code created a set of pagination buttons at the top of the page with previous and next buttons. At some point a request had come in that the buttons needed to be replicated at the bottom of the page. Since the HTML was being built up in a string and dumped in a literal control in the first place the developer that was tasked with making the change just dumped the string into two literal controls, the original at the top of the page, and the new one at the bottom of the page. The previous and next buttons use hidden input field to tell the application which actual page number the buttons correspond to. And these were now duplicated and as a result the previous and next buttons ceased to work.&lt;/p&gt;
&lt;p&gt;Here is an example of something similar:&lt;/p&gt;
&lt;pre&gt;&amp;lt;input id="first-hidden-field" value="123" type="hidden" name="some-name" /&amp;gt;&lt;br /&gt;&amp;lt;input id="submit-button" value="Submit" type="submit" /&amp;gt;&lt;br /&gt;&amp;lt;input id="second-hidden-field" value="456" type="hidden" name="some-name" /&amp;gt;&lt;/pre&gt;
&lt;p&gt;When the form fields are returned to the application and the field “some-name” is queried the result back is a combination of the two fields with the duplicate name. In this case:&lt;/p&gt;
&lt;pre&gt;string someName = Request.Form["some-name"];&lt;/pre&gt;
&lt;p&gt;will result in the value of “123,456” being stored in the string. Basically, it is the comma separated form of all the input fields with the given name.&lt;/p&gt;&lt;img src="http://blog.colinmackay.net/aggbug/11368.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2010/02/06/Tip-of-the-Day-17-Duplicate-input-fields.aspx</guid>
            <pubDate>Sat, 06 Feb 2010 22:01:38 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/11368.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2010/02/06/Tip-of-the-Day-17-Duplicate-input-fields.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/11368.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Visual Studio / SQL Server install order on Windows 7</title>
            <link>http://blog.colinmackay.net/archive/2009/10/11/Visual-Studio--SQL-Server-install-order-on-Windows-7.aspx</link>
            <description>&lt;p&gt;Quite a while ago I blogged about the &lt;a href="http://blog.colinmackay.net/archive/2008/08/03/3206.aspx"&gt;Visual Studio / SQL Server install order on Windows Vista&lt;/a&gt;. I’m about to go through a similar exercise on Windows 7 and given the issues I had then I thought that it would be only right to document the procedure in case any problems arose.&lt;/p&gt;  &lt;p&gt;Last time, it would seem, the best solution was to install things in the order in which Microsoft released them with the notable exception of the operating system. So this time, that is the strategy that I’m going to take. Windows 7 is already installed on my laptop. Then I’m going to install Visual Studio 2008, then SQL Server 2008, then any patches for either and we’ll see how we get on. &lt;/p&gt;  &lt;p&gt;I’m also going to ensure that I do NOT install SQL Server Express Edition on Visual Studio 2008 as I’ve had problems with that before. Essentially, the problem last time was that the SQL Server installer mistook Visual Studio’s SQL Server Express installation has having installed certain things. The SQL Server installation therefore didn’t want to repeat what it didn’t need to so it refused to install the client tools.&lt;/p&gt;  &lt;h2&gt;Install Order&lt;/h2&gt;  &lt;ul&gt;   &lt;li&gt;Visual Studio 2008, excluding SQL Server 2005 Express Edition &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a title="vs2008-installer-no-sql-express by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3998065131/"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" border="0" alt="vs2008-installer-no-sql-express" src="http://farm3.static.flickr.com/2562/3998065131_db70ee5207_o.png" width="766" height="587" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;MSDN Library (This is optional – I installed it because I’m occasionally developing on the road with no or limited connectivity) &lt;/li&gt;    &lt;li&gt;Visual Studio 2008 Service Pack 1 (this is required in order to install SQL Server 2008 – the installation will fail otherwise) &lt;/li&gt;    &lt;li&gt;SQL Server 2008 Developer Edition &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt; &lt;a title="sql-server-2008-compatibility-issues by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3999036490/"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" border="0" alt="sql-server-2008-compatibility-issues" src="http://farm4.static.flickr.com/3496/3999036490_171190fea1_o.png" width="602" height="298" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Install SQL Server 2008 SP1&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;That’s it – Job done. And it only took me two attempts to get it right this time. My stumbling block here was the order in which I applied the service packs.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e433c6e1-2626-40ef-82da-07c2b90bb701" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/sql+server" rel="tag"&gt;sql server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/sql+server+2008" rel="tag"&gt;sql server 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/visual+studio" rel="tag"&gt;visual studio&lt;/a&gt;,&lt;a href="http://technorati.com/tags/visual+studio+2008" rel="tag"&gt;visual studio 2008&lt;/a&gt;,&lt;a href="http://technorati.com/tags/installation" rel="tag"&gt;installation&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.colinmackay.net/aggbug/9407.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2009/10/11/Visual-Studio--SQL-Server-install-order-on-Windows-7.aspx</guid>
            <pubDate>Sun, 11 Oct 2009 10:50:22 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/9407.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2009/10/11/Visual-Studio--SQL-Server-install-order-on-Windows-7.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/9407.aspx</wfw:commentRss>
        </item>
        <item>
            <title>NMA - Top interactive agencies</title>
            <link>http://blog.colinmackay.net/archive/2009/10/10/NMA--Top-interactive-agencies.aspx</link>
            <description>&lt;p&gt;This years New Media Age top interactive agencies list is out. I’m quite please to see that I work for a company that is doing very well in the list.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/colinangusmackay/3997178205/" title="Equator-Scotland-Top5 by Colin  Angus Mackay, on Flickr"&gt;&lt;img width="516" height="282" border="0" src="http://farm3.static.flickr.com/2655/3997178205_81c689bdfe_o.jpg" alt="Equator-Scotland-Top5" style="border: 0px none ; display: block; float: none; margin-left: auto; margin-right: auto;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I work for &lt;a href="http://www.eqtr.com"&gt;Equator&lt;/a&gt; which is currently the top ranking agency in Scotland, and #41 in the UK.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/colinangusmackay/3997178207/" title="equator-synopsis by Colin  Angus Mackay, on Flickr"&gt;&lt;img width="516" height="637" border="0" src="http://farm3.static.flickr.com/2654/3997178207_91382d02cf_o.jpg" alt="equator-synopsis" style="border: 0px none ; display: block; float: none; margin-left: auto; margin-right: auto;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The bit I'd like to point out, just to blow my own trumpet, is the last paragraph:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.flickr.com/photos/colinangusmackay/3997948574/" title="Colin-Mackay-Key-Hire by Colin  Angus Mackay, on Flickr"&gt;&lt;img width="516" height="163" border="0" src="http://farm3.static.flickr.com/2608/3997948574_4cb2c2a9fc_o.jpg" alt="Colin-Mackay-Key-Hire" style="border: 0px none ; display: block; float: none; margin-left: auto; margin-right: auto;" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;"Key hires include &lt;a href="http://www.scottishdevelopers.com"&gt;Scottish Developers&lt;/a&gt; chairman Colin McKay [sic] as senior developer and Robert Graham as director of application development." - NMA&lt;br /&gt;
&lt;/p&gt;&lt;img src="http://blog.colinmackay.net/aggbug/9386.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2009/10/10/NMA--Top-interactive-agencies.aspx</guid>
            <pubDate>Sat, 10 Oct 2009 12:49:11 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/9386.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2009/10/10/NMA--Top-interactive-agencies.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/9386.aspx</wfw:commentRss>
        </item>
        <item>
            <title>My First OpenRasta Project - Part 2 (Resource Templates)</title>
            <link>http://blog.colinmackay.net/archive/2009/09/30/My-First-OpenRasta-Project--Part-2-Resource-Templates.aspx</link>
            <description>&lt;p&gt;To get started see &lt;a title="Getting started with OpenRasta (part 1)" href="http://blog.colinmackay.net/archive/2009/09/28/My-First-OpenRasta-Project--Part-1.aspx"&gt;part 1&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For this part the Invoice class has been expanded to include another property, you'll see why in a moment. For now, it now looks like this:&lt;/p&gt;
&lt;pre&gt;public class Invoice
{
    public string Reference { get; set; }
    public DateTime Date { get; set; }
}&lt;/pre&gt;
&lt;p&gt;Up to this point we are just showing some simple XML based on one of the built in codecs that ship as part of OpenRasta. There is a single URI and it always returns the same thing. So far there is nothing much going on.&lt;/p&gt;
&lt;p&gt;You can keep on adding resources to the ResourceSpace when you are configuring the site, but that is hardly a scalable solution when so many applications are based on dynamic data. You need a way to define a resource template.&lt;/p&gt;
&lt;p&gt;The way this is done is by adding place holders into the URI. These placeholders are defined by a set of curly braces with a parameter name inside. This is a bit like the string.Format method, except you can use meaningful names instead of the ordinal position of the parameter.&lt;/p&gt;
&lt;p&gt;The configuration of the ResourceSpace in the Configuration class is changed to:&lt;/p&gt;
&lt;pre&gt;ResourceSpace.Has.ResourcesOfType&amp;lt;Invoice&amp;gt;()
    .AtUri("/invoice/{reference}")
    .HandledBy&amp;lt;InvoiceHandler&amp;gt;()
    .AsXmlDataContract();&lt;/pre&gt;
&lt;p&gt;As you can see the only difference is that the parameter on the AtUri method is changed.&lt;/p&gt;
&lt;p&gt;OpenRasta will then look in the handler for a method that matches the HTTP verb and the parameters defined in the template.&lt;/p&gt;
&lt;p&gt;The InvoiceHandler now has a method that looks like this:&lt;/p&gt;
&lt;pre&gt;public Invoice Get(string reference)
{
    Invoice result = InvoiceRepository.GetInvoiceByReference(reference);
    return result;
}&lt;/pre&gt;
&lt;p&gt;Don't worry about the InvoiceRepository. It simply exists to get an Invoice object from somewhere. It could be from a database, in memory, a file or wherever.&lt;/p&gt;
&lt;p&gt;We can now go to the uri /invoice/123-ABC and get the output:&lt;/p&gt;
&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;Invoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://schemas.datacontract.org/2004/07/MyFirstOpenRastaProject.Resources"&amp;gt;
  &amp;lt;Date&amp;gt;2009-09-30T00:00:00+01:00&amp;lt;/Date&amp;gt;
  &amp;lt;Reference&amp;gt;123-ABC&amp;lt;/Reference&amp;gt;
&amp;lt;/Invoice&amp;gt;&lt;/pre&gt;
&lt;p&gt;However, that's not the whole story. You can do some pretty neat things with resource templates.&lt;/p&gt;
&lt;p&gt;For example, if the method parameter on the resource handler is a DateTime object you can build up the URI template using the property names in the DateTime object. The template parameters will then be mapped to the properties in the DateTime object.&lt;/p&gt;
&lt;p&gt;First the configuration has to be updated:&lt;/p&gt;
&lt;pre&gt;ResourceSpace.Has.ResourcesOfType&amp;lt;Invoice&amp;gt;()
    .AtUri("/invoice/{reference}")
    .And.AtUri("/invoice/{day}/{month}/{year}")
    .HandledBy&amp;lt;InvoiceHandler&amp;gt;()
    .AsXmlDataContract();&lt;/pre&gt;
&lt;p&gt;There is only one additional line here and that is to add a URI with a template containing the day, month and year. It is still the same type of resource and the code hasn't changed. All that is new is the URI template. If you were to attempt to create a brand new ResourceSpace for the Invoice resource you'd get an error that the resource type was already registered in the system.&lt;/p&gt;
&lt;p&gt;The InvoiceHandler class will need an additional method to handle the new template. The new method looks like this:&lt;/p&gt;
&lt;pre&gt;public Invoice Get(DateTime date)
{
    Invoice result = InvoiceRepository.GetInvoiceByDate(date);
    return result;
}&lt;/pre&gt;
&lt;p&gt;The result for the URI /invoice/29/09/2009 looks like this&lt;/p&gt;
&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;Invoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://schemas.datacontract.org/2004/07/MyFirstOpenRastaProject.Resources"&amp;gt;
  &amp;lt;Date&amp;gt;2009-09-29T00:00:00&amp;lt;/Date&amp;gt;
  &amp;lt;Reference&amp;gt;3e5a4e9e-3b46-4b09-a8f8-45010411501b&amp;lt;/Reference&amp;gt;
&amp;lt;/Invoice&amp;gt;&lt;/pre&gt;
&lt;div style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:dd04ad62-b9d0-44dd-9a9f-eb591ae29464" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/OpenRasta"&gt;OpenRasta&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/ResourceSpace"&gt;ResourceSpace&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/URI"&gt;URI&lt;/a&gt;,&lt;a rel="tag" href="http://technorati.com/tags/resource+template"&gt;resource template&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.colinmackay.net/aggbug/9177.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2009/09/30/My-First-OpenRasta-Project--Part-2-Resource-Templates.aspx</guid>
            <pubDate>Wed, 30 Sep 2009 20:08:46 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/9177.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2009/09/30/My-First-OpenRasta-Project--Part-2-Resource-Templates.aspx#feedback</comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/9177.aspx</wfw:commentRss>
        </item>
        <item>
            <title>My First OpenRasta Project - Part 1</title>
            <link>http://blog.colinmackay.net/archive/2009/09/28/My-First-OpenRasta-Project--Part-1.aspx</link>
            <description>&lt;p&gt;On the &lt;a href="http://trac.caffeine-it.com/openrasta/wiki"&gt;OpenRasta Wiki&lt;/a&gt; there are some &lt;a title="Setting up an OpenRasta project manually" href="http://trac.caffeine-it.com/openrasta/wiki/Doc/Tutorials/FirstSite"&gt;instructions&lt;/a&gt; on getting your project up and running the manual way, should you so wish. One of the new features introduced at the last beta was a Visual Studio 2008 project template, which installs as part of the binary distribution.&lt;/p&gt;
&lt;p&gt;Once installed you can create an OpenRasta project by going to the “Visual C#” Project Types and selecting OpenRasta ASP.NET Application (3.5) from the templates on the right of the dialog.&lt;/p&gt;
&lt;p&gt;&lt;a title="OpenRasta: New Project by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3963679622/"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" border="0" alt="OpenRasta: New Project" width="500" height="354" src="http://farm3.static.flickr.com/2635/3963679622_40c2bf0710.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once the project is created you’ll see that it has set the project up, added the references to the assemblies that it needs and created an initial handler, resource and views.&lt;/p&gt;
&lt;p&gt;&lt;a title="OpenRasta: Solution Explorer by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3962904101/"&gt;&lt;img style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: block; FLOAT: none; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; MARGIN-LEFT: auto; BORDER-LEFT-WIDTH: 0px; MARGIN-RIGHT: auto" border="0" alt="OpenRasta: Solution Explorer" width="282" height="651" src="http://farm4.static.flickr.com/3484/3962904101_baa2fc1b3f_o.png" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Before continuing further a little explanation of what each of these things are is in order.&lt;/p&gt;
&lt;p&gt;A &lt;em&gt;resource&lt;/em&gt; is a source of information. It is referenced via a URI. This blog post is a resource, an image is a resource, an invoice is a resource. However, a resource does not imply any particular kind of representation. In terms of OpenRasta a resource is simply an object&lt;/p&gt;
&lt;p&gt;A &lt;em&gt;handler&lt;/em&gt; is an object that manages the interaction of the resources. In MVC parlance it would be the “C” or controller.&lt;/p&gt;
&lt;p&gt;A &lt;em&gt;view&lt;/em&gt; is a regular ASP.NET page that can be rendered via the WebFormsCodec. It is not compulsory to implement any views at all if you don’t need ASP.NET.&lt;/p&gt;
&lt;p&gt;A &lt;em&gt;codec&lt;/em&gt; is the class responsible for en&lt;strong&gt;cod&lt;/strong&gt;ing and &lt;strong&gt;dec&lt;/strong&gt;oding the representation of a resource. The built in codecs are WebForms, JSON and two types of XML.&lt;/p&gt;
&lt;h2&gt;First Code&lt;/h2&gt;
&lt;p&gt;When you get started you’ll need to configure OpenRasta. It needs to know the details of the resources you want to expose and the handlers that can deal with those resources. To do that OpenRasta looks for a class in your project that implements the IConfigurationSource interface.&lt;/p&gt;
&lt;p&gt;If you have two or more classes that implement this interface then the first one that is found will be used. As the project template already contains a Configuration class already set up and ready to go there is nothing additional to do other than set the configuration.&lt;/p&gt;
&lt;p&gt;In the example I’m going to show, we will be rendering an invoice. So the configuration needs to look like this:&lt;/p&gt;
&lt;pre&gt;public class Configuration : IConfigurationSource
{
    public void Configure()
    {
        using (OpenRastaConfiguration.Manual)
        {
            ResourceSpace.Has.ResourcesOfType&amp;lt;Invoice&amp;gt;()
                .AtUri("/invoice")
                .HandledBy&amp;lt;InvoiceHandler&amp;gt;()
                .AsXmlDataContract();
        }
    }
}&lt;/pre&gt;
&lt;p&gt;The configuration happens through a fluent interface. The ResourceSpace is the root object where you can define the resources in your application, what handles them and how they are represented. In this case this is going to be a fairly simple example. As it is a fluent interface it does seem to be fairly self explanatory.&lt;/p&gt;
&lt;p&gt;The Invoice class is a simple POCO DTO that represents an invoice. POCO means Plain Old CLR Object and DTO is a Data Transfer Object. In this example the Invoice just looks like this:&lt;/p&gt;
&lt;pre&gt;public class Invoice
{
    public string Reference { get; set; }
}&lt;/pre&gt;
&lt;p&gt;The InvoiceHandler class is another POCO that happens to have methods on it that are picked up by the use of conventions. If you have a method named after an HTTP verb (like GET or POST) then OpenRasta will use it to handle that verb. &lt;/p&gt;
&lt;p&gt;In this example we are just going to return a simple Invoice object. I don’t want to complicate the example with other things at the present, so it will, in fact, always return an invoice with the same Reference property value.&lt;/p&gt;
&lt;pre&gt;public class InvoiceHandler
{
    public Invoice Get()
    {
        return new Invoice
        {
            Reference = "123-456/ABC"
        };
    }
}&lt;/pre&gt;
&lt;p&gt;As the configuration specified that the XML Data Contract codec was to be used the invoice is rendered using that codec. The output looks like this:&lt;/p&gt;
&lt;pre&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;Invoice xmlns:i="http://www.w3.org/2001/XMLSchema-instance" &lt;br /&gt;         xmlns="http://schemas.datacontract.org/2004/07/MyFirstOpenRastaProject.Resources"&amp;gt;
  &amp;lt;Reference&amp;gt;123-456/ABC
&amp;lt;/Invoice&amp;gt;&lt;/pre&gt;
&lt;p&gt;Obviously at this stage it isn’t very useful. This is just a quick demonstration showing how quickly something can be set up. In coming parts I’ll be addressing other issues that so that more useful things can be done.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;NOTE: This blog post is based on OpenRasta 2.0 Beta 2 (2.0.2069.364): [&lt;a title="Download OpenRasta" href="http://www.ohloh.net/p/openrasta/download"&gt;Download&lt;/a&gt;]&lt;/p&gt;
&lt;div style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:0b4f63bd-ec98-4dce-bf92-ba4069333b5a" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/OpenRasta"&gt;OpenRasta&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.colinmackay.net/aggbug/9155.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2009/09/28/My-First-OpenRasta-Project--Part-1.aspx</guid>
            <pubDate>Mon, 28 Sep 2009 20:26:46 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/9155.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2009/09/28/My-First-OpenRasta-Project--Part-1.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/9155.aspx</wfw:commentRss>
        </item>
        <item>
            <title>SQL Injection Attacks and Tips on How to Prevent Them</title>
            <link>http://blog.colinmackay.net/archive/2009/09/24/SQL-Injection-Attacks-and-Tips-on-How-to-Prevent-Them.aspx</link>
            <description>&lt;p&gt;I’m giving a talk in Dundee on the topic of SQL Injection Attacks. If you are interested in the subject then the registration link is at the bottom of the page.&lt;/p&gt;  &lt;p align="center"&gt;&lt;strong&gt;Wednesday, 28th October 2009 at 19:00 – 21:00      &lt;br /&gt;Queen Margaret Building, Dundee University&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Talk&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In light of some recent events, such as the man who was convicted of &lt;a href="http://news.bbc.co.uk/1/hi/world/americas/8206305.stm"&gt;stealing 130 million credit card details through a SQL Injection attack&lt;/a&gt;, it is imperative that developers understand what a SQL Injection Attack is, how they are carried out, and most importantly, how to defend your code against attack.&lt;/p&gt;  &lt;p&gt;In this talk I’ll demonstrate a SQL Injection Attack on an application in a controlled environment*. I’ll show you where the vulnerable code lies and what you can do to harden it.&lt;/p&gt;  &lt;p&gt;Although this talk uses C# as the application language and Microsoft SQL Server 2008 as the database engine many of the concepts and prevention mechanisms will apply to any application that accesses a database through SQL.&lt;/p&gt;  &lt;p&gt;* Demonstrating an attack on a real system without the owner’s consent is a breach of the 1990 Misuse of Computers Act, hence the controlled environment.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Venue&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We are meeting in the Queen Mother Building at Dundee University. After the meeting we normally retire to the the bar at Laing's&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Agenda&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;18:45 Doors Open    &lt;br /&gt;19:00 Welcome     &lt;br /&gt;19:10 The Talk (Part 1)     &lt;br /&gt;19:55 Break     &lt;br /&gt;20:05 The Talk (Part 2)     &lt;br /&gt;20:45 Feedback &amp;amp; Prizes     &lt;br /&gt;21:00 Repair to the Pub&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Registration&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Space is limited, we would therefore ask that you &lt;a title="SQL Injection Attacks and Tips on How To Prevent Them" href="http://www.eventbrite.com/event/443957890/colmac"&gt;sign up&lt;/a&gt;.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1042c58b-5acf-45b9-9e40-ab1d6d46edfe" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/sql" rel="tag"&gt;sql&lt;/a&gt;,&lt;a href="http://technorati.com/tags/sql+injection+attack" rel="tag"&gt;sql injection attack&lt;/a&gt;,&lt;a href="http://technorati.com/tags/security" rel="tag"&gt;security&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.colinmackay.net/aggbug/9103.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2009/09/24/SQL-Injection-Attacks-and-Tips-on-How-to-Prevent-Them.aspx</guid>
            <pubDate>Thu, 24 Sep 2009 03:29:25 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/9103.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2009/09/24/SQL-Injection-Attacks-and-Tips-on-How-to-Prevent-Them.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/9103.aspx</wfw:commentRss>
        </item>
        <item>
            <title>If you really must do dynamic SQL&amp;hellip;</title>
            <link>http://blog.colinmackay.net/archive/2009/09/21/If-you-really-must-do-dynamic-SQLhellip.aspx</link>
            <description>&lt;p&gt;I may have mentioned in previous posts and articles about SQL Injection Attacks that dynamic SQL (building SQL commands by concatenating strings together) is a source of failure in the security of a data driven application. It becomes easy to inject malicious text in there to cause the system to return incorrect responses. Generally the solution is to use parameterised queries&lt;/p&gt;  &lt;p&gt;However, there are times where you may have no choice. For example, if you want to dynamically reference tables or columns. You can’t do that as the table name or column name cannot be replaced with a parameter. You then have to use dynamic SQL and inject these into a SQL command.&lt;/p&gt;  &lt;h2&gt;The problem&lt;/h2&gt;  &lt;p&gt;It is possible for SQL Server to do that concatenation for you. For example:&lt;/p&gt;  &lt;pre&gt;CREATE PROCEDURE GetData 
	@Id INT,
	@TableName sysname,
	@ColumnName sysname
AS
BEGIN
	SET NOCOUNT ON;
	
	DECLARE @sql nvarchar(max) = 
		'SELECT ' + @ColumnName + 
		' FROM ' + @TableName + 
		' WHERE Id = '+cast(@Id as nvarchar(20));	
	EXEC(@sql)
END
GO&lt;/pre&gt;

&lt;p&gt;This is a simple stored procedure that gets some data dynamically. However, even although everything is neatly parameterised it is no protection. All that has happened is that the location for vulnerability (i.e. the location of the construction of the SQL) has moved from the application into the database. The application is now parameterising everything, which is good. But there is more to consider than just that.&lt;/p&gt;

&lt;h2&gt;Validating the input&lt;/h2&gt;

&lt;p&gt;The next line of defence should be verifying that the table and column names passed are actually valid. In SQL Server you can query the &lt;strong&gt;INFORMATION_SCHEMA&lt;/strong&gt; views to determine whether the column and tables exist.&lt;/p&gt;

&lt;p&gt;If, for example, there is a table called &lt;strong&gt;MainTable&lt;/strong&gt; in the database you can check it with a query like this:&lt;/p&gt;

&lt;pre&gt;SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'MainTable'&lt;/pre&gt;

&lt;p&gt;And it will return:&lt;/p&gt;

&lt;p&gt;&lt;a title="INFORMATION_SCHEMA.TABLES by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3940740909/"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="INFORMATION_SCHEMA.TABLES" src="http://farm4.static.flickr.com/3510/3940740909_32d8c15927_o.png" width="417" height="64" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a similar view for checking columns. For example:&lt;/p&gt;

&lt;p&gt;&lt;a title="INFORMATION_SCHEMA.COLUMNS by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3940756637/"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="INFORMATION_SCHEMA.COLUMNS" src="http://farm4.static.flickr.com/3504/3940756637_c35304d4f9_o.png" width="510" height="70" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the &lt;strong&gt;INFORMATION_SCHEMA.COLUMNS&lt;/strong&gt; view also contains sufficient detail on the table so that when we implement it we only have to make one check:&lt;/p&gt;

&lt;pre&gt;ALTER PROCEDURE GetData 
	@Id INT,
	@TableName sysname,
	@ColumnName sysname
AS
BEGIN
    SET NOCOUNT ON;
	
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
               WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)
    BEGIN
        DECLARE @sql nvarchar(max) = 
            'SELECT ' + @ColumnName + 
            ' FROM ' + @TableName + 
            ' WHERE Id = '+cast(@Id as nvarchar(20));	
        EXEC(@sql)
    END
END
GO&lt;/pre&gt;

&lt;h2&gt;Formatting the input&lt;/h2&gt;

&lt;p&gt;The above is only part of the solution, it is perfectly possible for a table name to contain characters that mean it needs to be escaped. (e.g. a space character or the table may share a name with a SQL keyword). To escape a table or column name it is enclosed in square brackets, so a table name of &lt;strong&gt;My Table&lt;/strong&gt; becomes &lt;strong&gt;[My Table]&lt;/strong&gt; or a table called &lt;strong&gt;select&lt;/strong&gt; becomes &lt;strong&gt;[select]&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can escape table and column names that wouldn’t ordinarily require escaping also. It makes no difference to them.&lt;/p&gt;

&lt;p&gt;The code now becomes:&lt;/p&gt;

&lt;pre&gt;ALTER PROCEDURE GetData 
	@Id INT,
	@TableName sysname,
	@ColumnName sysname
AS
BEGIN
    SET NOCOUNT ON;
	
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
               WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)
    BEGIN
        DECLARE @sql nvarchar(max) = 
            'SELECT [' + @ColumnName + '] ' + 
            'FROM [' + @TableName + '] ' +
            'WHERE Id = '+cast(@Id as nvarchar(20));	
        EXEC(@sql)
    END
END
GO&lt;/pre&gt;

&lt;p&gt;But that's not quite the full story.&lt;/p&gt;

&lt;h2&gt;Really formatting the input&lt;/h2&gt;

&lt;p&gt;What if you have a table called &lt;strong&gt;Cra]zee Table&lt;/strong&gt;? Okay - Why on earth would you have a table with such a stupid name? It happens, and it is a perfectly legitimate table name in SQL Server. People do weird stuff and you have to deal with it.&lt;/p&gt;

&lt;p&gt;At the moment the current stored procedure will simply fall apart when presented with such input. The call to the stored procedure would look like this: &lt;/p&gt;

&lt;pre&gt;EXEC GetData 1, 'Cra]zee Table', 'MadStuff'&lt;/pre&gt;

&lt;p&gt;And it gets past the validation stage because it is a table in the system. The result is a message: &lt;/p&gt;

&lt;pre&gt;Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Table'.&lt;/pre&gt;
The SQL produced looks like this: 

&lt;pre&gt;SELECT [MadStuff] FROM [Cra]zee Table] WHERE Id = 1&lt;/pre&gt;

&lt;p&gt;By this point is should be obvious why it failed. The SQL Parser interpreted the first closing square bracket as the terminator for the escaped section.&lt;/p&gt;

&lt;p&gt;There are other special characters in SQL that require special consideration and you could write code to process them before adding it to the SQL string. In fact, I’ve seen many people do that. And more often than not they get it wrong.&lt;/p&gt;

&lt;p&gt;The better way to deal with that sort of thing is to use a built in function in SQL Server called &lt;strong&gt;&lt;a title="QUOTENAME function (SQL Server Books On-Line)" href="http://msdn.microsoft.com/en-us/library/ms176114.aspx"&gt;QUOTENAME&lt;/a&gt;&lt;/strong&gt;. This will ensure the column or table name is properly escaped. The stored procedure we are now building now looks like this:&lt;/p&gt;

&lt;pre&gt;ALTER PROCEDURE GetData 
	@Id INT,
	@TableName sysname,
	@ColumnName sysname
AS
BEGIN
    SET NOCOUNT ON;
	
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
               WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)
    BEGIN
        DECLARE @sql nvarchar(max) = 
            'SELECT ' + QUOTENAME(@ColumnName) +
            ' FROM ' + QUOTENAME(@TableName) + 
            ' WHERE Id = '+cast(@Id as nvarchar(20));	
        EXEC(@sql)
    END
END
GO&lt;/pre&gt;

&lt;h2&gt;Things that can be parameterised&lt;/h2&gt;

&lt;p&gt;There is still something that can be done to this. The Id value is being injected in to the SQL string, yet it is something that can quite easily be parameterised.&lt;/p&gt;

&lt;p&gt;The issue at the moment is that the SQL String is being executed by using the &lt;a title="EXECUTE (T-SQL) (SQL Server Books On-Line)" href="http://msdn.microsoft.com/en-us/library/ms188332.aspx"&gt;&lt;strong&gt;EXECUTE&lt;/strong&gt;&lt;/a&gt; command. However, you cannot pass parameters into this sort of executed SQL. You need to use a stored procedure called &lt;strong&gt;&lt;a title="sp_executesql (Stored Procedure, Transact SQL) (SQL Server Books On-Line)" href="http://msdn.microsoft.com/en-us/library/ms188001.aspx"&gt;sp_executesql&lt;/a&gt;&lt;/strong&gt;. This allows parameters to be defined and passed into the dynamically created SQL.&lt;/p&gt;

&lt;p&gt;The stored procedure now looks like this:&lt;/p&gt;

&lt;pre&gt;ALTER PROCEDURE GetData 
	@Id INT,
	@TableName sysname,
	@ColumnName sysname
AS
BEGIN
    SET NOCOUNT ON;
	
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
               WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)
    BEGIN
        DECLARE @sql nvarchar(max) = 
            'SELECT ' + QUOTENAME(@ColumnName) +
            ' FROM ' + QUOTENAME(@TableName) + 
            ' WHERE Id = @Identifier';	
        EXEC sp_executesql @sql, N'@Identifier int',
                           @Identifier = @Id
    END
END
GO&lt;/pre&gt;

&lt;p&gt;This is not quite the end of the story. There are performance improvements that can be made when using sp_executesql. You can find out about these in the SQL Server books-online.&lt;/p&gt;

&lt;h2&gt;And finally...&lt;/h2&gt;

&lt;p&gt;If you must use dynamic SQL in stored procedures do take care to ensure that all the data is validated and cannot harm your database. This is an area in which I tread very carefully if I have no other choice. &lt;/p&gt;

&lt;p&gt;Try and consider every conceivable input, especially inputs outside of the bounds of your application. Remember also, that defending your database is a multi-layered strategy. Even if you have the best firewalls and security procedures elsewhere in your system a determined hacker may find a way though your other defences and be communicating with the database in a way in which you didn’t anticipate. Assume that an attacker has got through your other defences, how do you provide the data services to your application(s) yet protect the database?&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:80b80b8f-e1ea-40c0-bbd9-6e4760f85a79" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/sql" rel="tag"&gt;sql&lt;/a&gt;,&lt;a href="http://technorati.com/tags/sql+server" rel="tag"&gt;sql server&lt;/a&gt;,&lt;a href="http://technorati.com/tags/sql+injection+attack" rel="tag"&gt;sql injection attack&lt;/a&gt;,&lt;a href="http://technorati.com/tags/dynamic+sql" rel="tag"&gt;dynamic sql&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.colinmackay.net/aggbug/9076.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2009/09/21/If-you-really-must-do-dynamic-SQLhellip.aspx</guid>
            <pubDate>Mon, 21 Sep 2009 18:15:01 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/9076.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2009/09/21/If-you-really-must-do-dynamic-SQLhellip.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/9076.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Technology trends through Google Analytics</title>
            <link>http://blog.colinmackay.net/archive/2009/09/20/Technology-trends-through-Google-Analytics.aspx</link>
            <description>&lt;p&gt;I’ve been looking at various Google Analytics stats to see some recent technology trends. This started out as me being simply curious about what technologies people were using to access my blog. But then I got to thinking that it would be interesting to see how the statistics on my (Microsoft/Technology focused) blog fair with regard to other websites.&lt;/p&gt;  &lt;p&gt;The most interesting thing I found is that my blog does not match other other, more mainstream, sites I looked at. I seem to have some early adopters that cause jumps in the trend. Other sites tend to have more stable trends.&lt;/p&gt;  &lt;p&gt;For example, here’s the graph for my blog showing operating system usage:&lt;/p&gt;  &lt;p&gt;&lt;a title="MyBlog-2yr-OS by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3936275739/"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" border="0" alt="MyBlog-2yr-OS" src="http://farm3.static.flickr.com/2495/3936275739_39ba8cb0c3_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In mid-2008 there is a jump of Windows Vista users. I tried to figure out why the number of Windows Vista users should jump like this and the only thing that I can actually think of is that this is about the time of “&lt;a href="http://www.microsoft.com/windows/mojave-experiment/#"&gt;The Mojave Experiment&lt;/a&gt;”. Alternatively, perhaps it is something I did on my blog bringing more traffic to me from people running Windows Vista?&lt;/p&gt;  &lt;p&gt;In July 2008 20% of visitors were using Windows Vista, the following month that number had jumped to 32%. Incidentally on August 3rd I wrote a blog post that has consistently be in my top-5 blog posts each month ever since. That post was about &lt;a href="http://blog.colinmackay.net/archive/2008/08/03/3200.aspx"&gt;installing SQL Server 2005 on Vista&lt;/a&gt;. In fact, that single post currently represents 14% of the monthly traffic to my blog.&lt;/p&gt;  &lt;p&gt;When looking at the browser usage in use the trend doesn’t have such a wild jump in it. Internet explorer is steadily being replaced by FireFox with most other browsers sitting down below 10%. There is, however, a rise in Chrome usage and I expect it to be past the 10% mark by the end of the year.&lt;/p&gt;  &lt;p&gt;&lt;a title="MyBlog-2yr-Browser by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3937055484/"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" border="0" alt="MyBlog-2yr-Browser" src="http://farm3.static.flickr.com/2450/3937055484_4ffd934399_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;What is interesting is the jump when Chrome first came on the market, it jumped in at 3.5% and has been steadily rising since. It is up to 8.1% now so that it is now in 3rd place. &lt;/p&gt;  &lt;h2&gt;Operating Systems&lt;/h2&gt;  &lt;p&gt;Let’s compare that to some more mainstream sites. I’m just going to put the graphs up one after the other then comment on them.&lt;/p&gt;  &lt;p&gt;First the operating system:&lt;/p&gt;  &lt;p&gt;&lt;a title="College-2yr-OS by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3937055726/"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" border="0" alt="College-2yr-OS" src="http://farm3.static.flickr.com/2440/3937055726_ccb69a2eba_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a title="Lingerie-2yr-OS by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3936276021/"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" border="0" alt="Lingerie-2yr-OS" src="http://farm4.static.flickr.com/3448/3936276021_a6632f66b2_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a title="Travel-2yr-OS by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3936275993/"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" border="0" alt="Travel-2yr-OS" src="http://farm4.static.flickr.com/3457/3936275993_f9cc03b02d_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;There are some very interesting variations between these graphs and my blog. None of these have a big jump in any particular operating system, so that suggests that Project Mojave, an earlier speculation to account for a jump in Vista users, didn’t have a big effect.&lt;/p&gt;  &lt;p&gt;What is most interesting is that people looking at Lingerie websites have more Apple Macs (purple line) that people looking at other sites. Mac usage for the lingerie site runs from 10% to 15% over the course of the two years.&lt;/p&gt;  &lt;p&gt;Windows XP usage is coming down, more markedly for the Lingerie site. I’d speculate that since businesses seem to be the ones holding on to Windows XP the longest (still installing it on newly purchased equipment, unlike the home market which will most likely stick to what is on the box already) those viewing the Lingerie website are more likely to do so from home. Certainly, if I was so inclined to visit, I’d do that at home.&lt;/p&gt;  &lt;p&gt;On the early adopter front, most mainstream sites have not seen much of an increase in Windows 7 (turquoise line) usage yet. It isn’t released to the general public until 22nd of October. Early adopters will mostly be running the beta and release candidate. Those with MSDN Subscription will be able to run the full release already. It would seem that many developers (or at least those inclined to visit a software development oriented blog) are already adopting Windows 7 as almost 7% of visitors used that operating system. The mainstream sites are sitting around 0.5% currently.&lt;/p&gt;  &lt;h2&gt;Browsers&lt;/h2&gt;  &lt;p&gt;&lt;a title="College-2yr-Browser by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3937055610/"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" border="0" alt="College-2yr-Browser" src="http://farm3.static.flickr.com/2604/3937055610_0d9542f8de_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a title="Lingerie-2yr-Browser by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3936275843/"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" border="0" alt="Lingerie-2yr-Browser" src="http://farm3.static.flickr.com/2482/3936275843_13dd4ae801_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a title="Travel-2yr-Browser by Colin  Angus Mackay, on Flickr" href="http://www.flickr.com/photos/colinangusmackay/3937055566/"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" border="0" alt="Travel-2yr-Browser" src="http://farm4.static.flickr.com/3515/3937055566_f8ec3b65dd_o.png" width="570" height="570" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It looks like developers buck the trend again. 30%-40% use FireFox to access my blog whereas more mainstream sites get 10%-20% of visitors using FireFox. Also, the lingerie site is also slightly bucking the trend by having around 25% of visitors using FireFox.&lt;/p&gt;  &lt;p&gt;Universally, IE is losing market share. It just seems to be quicker for software developers to be abandoning it, even on this Microsoft oriented software development blog.&lt;/p&gt;  &lt;p&gt;It would seem that FireFox is the browser in line to take the crown, at least in software development circles as it does seem to have a fair way to go elsewhere. However, Chrome has got off to a good start, gaining initial popularity even on more mainstream sites. FireFox may have some real competition. Safari is not doing so badly either, but that trend does seem to follow the Mac OS trend, so perhaps that’s just getting the kudos through being installed as the default browser on Macs.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ecb3c21e-5b94-407a-8209-489abfce188d" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/google+analytics" rel="tag"&gt;google analytics&lt;/a&gt;,&lt;a href="http://technorati.com/tags/windows+7" rel="tag"&gt;windows 7&lt;/a&gt;,&lt;a href="http://technorati.com/tags/windows+vista" rel="tag"&gt;windows vista&lt;/a&gt;,&lt;a href="http://technorati.com/tags/vista" rel="tag"&gt;vista&lt;/a&gt;,&lt;a href="http://technorati.com/tags/windows+xp" rel="tag"&gt;windows xp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/xp" rel="tag"&gt;xp&lt;/a&gt;,&lt;a href="http://technorati.com/tags/IE" rel="tag"&gt;IE&lt;/a&gt;,&lt;a href="http://technorati.com/tags/FireFox" rel="tag"&gt;FireFox&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Chrome" rel="tag"&gt;Chrome&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Safari" rel="tag"&gt;Safari&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.colinmackay.net/aggbug/9063.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Colin Angus Mackay</dc:creator>
            <guid>http://blog.colinmackay.net/archive/2009/09/20/Technology-trends-through-Google-Analytics.aspx</guid>
            <pubDate>Sun, 20 Sep 2009 16:10:17 GMT</pubDate>
            <wfw:comment>http://blog.colinmackay.net/comments/9063.aspx</wfw:comment>
            <comments>http://blog.colinmackay.net/archive/2009/09/20/Technology-trends-through-Google-Analytics.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blog.colinmackay.net/comments/commentRss/9063.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>