Twitter Bootstrap & .net MVC 4

Twitter Bootstrap is a really cool lightweight library to enable modern looking responsive web development. It’s pretty easy to incorporate with .net MVC 4, but there are two items you need to do when using MVC 4 with Twitter Bootstrap:

  1. Update jquery to the version that Bootstrap needs (currently 1.7.1) you’ll get JS errors if you don’t. MVC 4 is currently at 1.6.x.
  2. Edit your Global.asax file to enable BundleTable.Bundles.EnableDefaultBundles(); and disable BundleTable.Bundles.RegisterTemplateBundles(); or your JS bundle won’t include the bootstrap.js (if you include it in your project)
    more info 

MS-SQL | how to create a fast GUID index

Need to use a GUID for a row identifier (identity column)?

For any DBA reading, this is probably old hat, but MS-SQL server 2005 introduced a new command for creating GUIDs= NEWSEQUENTIALID(). This allows a fast index based on a GUID key that performs close to the speed of an integer index. Just use the command NEWSEQUENTIALID as the default in the identity column of your table and viola, you have a very fast GUID index. 

note: If privacy is a concern, do not use this function. It is possible to guess the value of the next generated GUID and, therefore, access data associated with that GUID (if the GUID is exposed).

msdn docs

The lazy way to multi-thread loops in .net (4.0)

Here’s the easy way to turn foreach loops into multithreaded execution. Using the Task Parallel Library. TPL manages the threads and semaphores and all that stuff that stuff so hacks like me can make it work well. Note the use of a lambda inside the loop. This is actually executed in another thread so careful that you don’t reference variables out of scope. Here’s sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MultiThreadingSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var sampleList = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

           Parallel.ForEach(sampleList, item =>{
               Console.WriteLine(“Thread start” + item.ToString());
               Thread.Sleep(1000);
               Console.WriteLine(“Thread end” + item.ToString());
            });
           Console.WriteLine(“done with all threads…”);
           Console.ReadLine();
        }
    }
}

Want to control the number of threads? Use this:

   var parallelOptions = new ParallelOptions();
  parallelOptions.MaxDegreeOfParallelism = 5;//or whatever you want Parallel.ForEach(sampleList, parallelOptions  item =>          

The nuts and bolts of optimizing .net MVC 3 routes

From the Stack Overflow guys (who know their shit)

Here are four things to look at:

  • The Regex cache - only holds 15 so if you have more routes then that it’s an issue (if you use regex matching on your routes)
  • Stop using string based route constraints (build them in code — see article)
  • Put most used routes on top
  • Disable file checks for static content

(Source: samsaffron.com)

Elasticsearch as nosql

If you are looking for a fast nosql database don’t overlook Elasticsearch. Props to Andrew Doran who recommended it. Like SOLR it is based on Lucene but it’s what SOLR should have evolved into, but didn’t. It has JSON-REST and Memcache interfaces and unlike SOLR it stores all of your documents in the original form (this is a big deal - trust me). Built-in support for shards and load balancing with auto-discovery (yes even on Ec2). It can work as a key-value store or full search capability.  There is a Windows service installer for those of you like me working in .net and Windows. I also recommend the .net client nest it’s not complete yet, but it’s the best of the pack (for now). 

P3P Privacy files and IE iframes

So IE blocks cookies in iframes that don’t a have a policy file set. How do you fix this?

  1. create a policy with the IBM editor 
    (I hate this domain, but it’s the only valid source for the editor)
    http://www.softpedia.com/get/Security/Security-Related/P3P-Policy-Editor.shtml
  2. Add a header to your pages or though a .htaccess file (or custom http headers in IIS) 
  3. the header? key = “P3P value= “policyref=”/p3p/p3p.xml”, CP=”ALL DSP COR NID TAIa OUR NOR STA”“ 

Note: The P3P a legally binding document so you might want to read up on what you are getting into.