Microsoft Azure launches Search as a service

Having only recently been switched on to how powerful and surprisingly cost effective Microsoft’s Azure services can be, I was delighted to log into Twitter this evening to be greeted with a tweet highlighting the launch of Azure’s new Search service.

Having had experience with how difficult effective search can be, it’s great to see that Azure have tackled this and turned what’s usually a complex problem into an easy service solution. And best of all? It’s free for smaller sites.

At the moment Azure’s search as a service is in preview stage, though I’ll be interested to see if they introduce any more pricing tiers considering the jump between them as highlighted below:

Free STANDARD
Storage 50 MB 25 GB per unit
Queries per Second* N/A 15 per unit
Documents Hosted** 10,000 Documents
3 Index Limit
15 Million per unit
50 Index Limit
Scale Out Limits N/A Up to 36 units
Price per Unit per Hour Free £0.107 / hour
(~£80 / month)

In the mean time though I’m most definitely going to give Azure’s search as a service a try for a few projects I’m currently working on and will no doubt report back my experiences here.

You can read more about Azure’s Search as a service preview here with information on the existing pricing structure available here.

 

ASP.NET Naming Conventions – how to avoid calling everything a service

Naming things can be tricky. I’ve found that when I’m creating a new class the first class name that pops into my head is usually one that doesn’t describe the function and purpose of the class I’m creating adequately enough. It usually takes a few moments of stopping and thinking to myself “What can I call this class that will describe its role clearly to other developers and/or consumers of this API, class or interface – something succinct and no longer than 4 words long” (4 words or less is a general rule of thumb I’ve picked up throughout my years programming that seems to hit the sweet spot).

Over time I would start to notice patterns occurring. For instance when developing web based applications following a typical n-tier application structure I’ve noticed it becomes very easy to label everything at the service/business logic level a ‘service’. With this in mind I started to do a little googling for naming ideas and found this little gem of a question on every developer’s favourite site that highlights a whole range of different naming ideas and conventions that might help describe that class you’re creating perfectly.

These are:

  • Coordinator
  • Builder
  • Writer
  • Reader
  • Handler
  • Container
  • Protocol
  • Target
  • Converter
  • Controller
  • View
  • Factory
  • Entity
  • Bucket
  • Attribute
  • Type
  • Helper
  • Collection
  • Info
  • Provider
  • Processor
  • Element
  • Manager
  • Node
  • Option
  • Factory
  • Context
  • Designer
  • Editor

Sessions in ASP.NET MVC using Dependency Injection

Storing and reading data from a session in ASP.NET MVC

A common approach I see whilst browsing tutorials or StackOverflow questions relating to sessions in ASP.NET MVC is the following:

And then retrieving session data like this:

What’s the problem with this?

Whilst this solution works, there’s improvements that can be made. Take a look at the code for a moment and try and think of what problems doing the above could lead to as the project started to mature.

First of all, what happens if we want to modify the what information gets stored in the session? Or even worse, we want to switch from session based storage to database storage? Ultimately we’d have to spend a great deal of time going through any controller that references the session and make changes.

Ultimately, what we’re doing by accessing the Session object directly from within the controller is breaking the D (dependency inversion) of the SOLID principles – a set of five principles of object-orientated programming and design aimed at encouraging the creation of extensible, flexible and testable software; something all developers should strive to do.

What is the Dependency Inversion principle?

Once of the most memorable definitions of dependency inversion that I’ve heard and which has stuck with me is:

“Program to an interface, not an implementation”

In the context of the code sample above, by directly programming against the Session object (an implementation) we’re creating a dependency within our controller. We can avoid doing this by using an interface type to abstract and encapsulate the session’s implementation details – allowing us to programming against some kind of simplified interface. Doing this leads to a loosely coupled controller that’s not directly depending on the existence of the Session object, but instead depends a type that implements our IUserInformation type.

This powerful thing about this is the controller does not need to be concerned about the implementation details, meaning we could easily switch the implementation to read the data from a database instead of a session object – and we wouldn’t have to touch anything in the controller.

This also makes the controller far more testable as we can easily mock the session’s abstraction.

Ok, I’m following so far. So how do we ‘invert’ our controller’s dependencies?

There are multiple types of dependency injection but the most common pattern for this type of challenge is constructor injection using an Inversion of Control Container.

In this example I’m going to use StructureMap as my IoC container of choice as it’s one I’m most comfortable with and prefer using in my day-to-day life.

The first step is to include the Nuget package for both StructureMap and StructureMap.Web. Now we’ve got the StructureMap packages we need to hook Structuremap into ASP.NET’ MVC’s dependency resolver by creating a new class (In this instance I called it StructureMapDependencyResolver.cs) and implementing the IDependencyResolver from the System.Web.Mvc package like so:

Once this is done simply add our DependencyResolver to the Application_Start method within Global.asax.cs, like so:

Now that StructureMap is hooked up to our ASP.NET MVC application, all that’s left to do is define our dependencies by creating our WebsiteRegistry.cs class that extends StructureMap.Configuration.DSL.Registry.cs, like so:

Once we’ve added our Registry we need to define how our instance of IUserInformation is constructed. In this case we want to deserialize our session object and cast it to an instance of IUserInformation.

We can now refactor our ExampleLoginController.cs file to move the dependencies outside of the controller and program against the IUserInformation interface.

Golden Nuget Package #1: RouteDebugger by Phil Haack

Whilst working on a side project written in ASP.NET MVC (version 5), I ran into some routing issues that involved routes just flat out not working. To make matters worse I was using Areas which add a little more complexity to routing (not much, but just enough to spice things) by splitting routes up into separate areas.

After spending about an hour trying to figure out why my route wasn’t working I decided to pull out the Google-fu to look debugging ASP.NET MVC routes, which is when I came across a golden nuget package written by Phil Haack called RouteDebugger.

Setup was simple, and after downloading the RouteDebugger Nuget package via Visual Studio’s awesome Nuget Package Manager tool and enabling it within the Web.config configuration file by adding the following it was up and running.

Now when you fire up your application you’re greeted with a useful debugging pane located at the bottom of your page (see below) showing you exactly what route your current page URL matches and which ones they don’t. In my instance this proved extremely valuable as I was able to very quickly identify that my route was being matched to a far more vague route before it was being hit.

RouteDebugger

Creating reusable HTML components in ASP.NET MVC using Razor

Whilst working on a side project I started to notice I was using a lot of the same HTML to create floating boxes. Whilst these boxes looked the same, the content of them changed quite dramatically; for instance, some of the content boxes contained forms, others contained straight text, like so:

As my side project progressed and grew, I found myself making more and more modifications to these HTML components so I started to look how I can encapsulate the component for greater flexibility and extensibility as my project progressed.

The solution I ended up with was creating a HTML extension modelled off of the way the the Html.BeginForm() extension works, by writing directly to the view’s context and then returning an instance of IDisposable, with the call to disposing of the context writing the closing HTML statements of my component to the view context – essentially wrapping the contents passed into the HTML extension in my HTML component.

Below is an example of what the code looks like:

Using this new HTML extension I’m now able to reuse my HTML component and fill the panel’s content in with whatever I please, like so:

Generics and Lambda Expressions in Javascript? With TypeScript you can!

Whilst on my lunch break I took some time out to watch the TypeScript presentation at Build 2014 presented by C# and TypeScript’s father Anders Hejlsberg. It’s a fantastic presentation by Anders that really highlights how powerful TypeScript is, and the benefits it can Javascript development.

If you’re short on time then I’d recommend fast forwarding to the 20 minute mark where Anders runs through using Generics and Lambda Expressions (instead of waiting for ECMAScript 6) in TypeScript.

Upgrading to StructureMap 3

As explained in previous blog posts, I’m a huge fan of StructureMap and use it as my IoC container of choice when writing .NET applications. With the release of StructureMap 3, I was happy to see there have been some rather large updates to it. However, when I tried to update an application from version 2 to StructureMap 3 I quickly ran into some configuration issues as a result of the API changes, and with StructureMap 3 being as new as it is there’s little documentation at the moment. Eventually I managed to figure out where missing classes have been moved to so I thought I’d document it here to help anyone who experienced similar head scratching questions.

Q1: HttpContext related scoping has moved

Updating my .NET MVC application that uses HttpContext I quickly became aware that HybridHttpOrThreadLocalScoped had vanished, but I noticed this has now been moved into a separate StructureMap.Web Nuget package that can be found here. It can also be found within Visual Studio’s package manager by simply searching for “StructureMap.Web”. Added this package to my solution quickly resolved the HttpContext related issues I ran into.

Jeremy Miller did mention this on his blog, however I seemed to have skimmed over little detail as I rushed to update like an excited kid at Christmas.

Q2: SetAllProperties(), where are you?

SetAllProperties has now been moved into a class called Policies and can be accessed like so: this.Policies.SetAllProperties();

Side project: Space shooter written in Javascript using Typescript

Having heard lots about Typescript and how awesome it is, I decided to spend a few lunch breaks at work and a few evenings at home looking into what the fuss was all about. I initially started writing simple CSS based stuff that simply drew elements to the DOM, but quickly moved onto playing around with HTML Canvas. Eventually I ended up writing a Javascript game with it called Space Shooter.

spaceshooter

All of the source code is up on Github for those interested.

Fixing the “your project file uses a different version of TypeScript compiler” error in Visual Studio 2013

Last night, after downloading and installing the Visual Studio 2013 Update 2 Release Candidate update which included TypeScript 1.0, I loaded up a TypeScript project that I’d been playing around with and started to code. Soon after starting I clicked save, expecting Visual Studio to compile the Javascript but quickly encountered the following build failure error:

“Your project file uses a different version of the TypeScript compiler and tools than is currently installed on this machine. No compiler was found at C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.9\tsc.exe. You may be able to fix this problem by changing the <TypeScriptToolsVersion> element in your project file. <ProjectName>”

Scratching my head I wondered where I needed to go to reference the new Type Script 1.0 compiler that had installed along with Visual Studio 2013 Update 2 Release Candidate. After having a quick look around the project options such as the TypeScript Build panel within project properties I was still stumped.

It turns out that you specify the TypeScript version number within the project’s .csproj file itself. Simple right click the project that requires TypeScript compilation, click “Unload Project” and then right click the unloaded project again and click “Edit  <ProjectName>.csprok” to view the project’s project configuration. From here you should see a TypeScript version property that you need to update to your desired version (in my case 1.0).

Once you’ve updated this, simple reload the project and you’re good to go!

The Architecture of StackOverflow.com

Unless you’re a developer who’s been living under a rock for the past 5 years you’d no doubt have heard of Stack Overflow, and no doubt you already be aware of what a great resource it is as a developer.

Since it’s launch in August 2008 Stack Overflow spread like wildfire throughout the development community, quickly establishing itself as the place to go to find programming and development related issues answered. No longer did we need to trawl through the noise of forum posts looking for solutions to questions or fixes for bugs, all we needed to do was type our question into Google with a suffix of ” + stackoverflow” to find exactly what we were looking for.

Whilst skeptical of claims that Stack Overflow has saved companies billions of dollars in developer productivity, once thing for sure is that whether or not the total figure is billions, Stack Overflow has certainly saved a lot of companies a lot of money in the 5 years it’s been alive.

Currently sitting above sites such as NetFlix, BBC and Flickr in the Alexa Top 100 the most visited websites (position 58 at time of writing), Stack Overflow receives roughly 1.7 million visits a day. With such high traffic numbers and a huge fan of system architecture I was really interested to know a bit more about what was going on under the hood of Stack Overflow. We all know that it’s powered by Microsoft’s fantastic .NET MVC framework, but what about the structure, the database access, the servers and all of the other juicy bits that get us developers salivating?! Thankfully StackOverflow software engineer Marco Cecconi, gave a talk at the Developer Conference 2013 on this very subject.

At just over 46 minutes long I highly recommend you check out the talk if like me, you love to know about systems and software architecture.