WebApi with Ninject

With WebApi it really sucks that you can’t add its dependencies through IoC like you normally would for any MVC project.  To get it working with WebApi there are two interfaces that you have to implement (IDependencyResolver and IDependencyScope).

The IDependencyScope has two methods that you have to implement “GetService” and “GetServices” both expects a Type and returns an object or a collection of objects.


Then implement the IDependencyResolver which has one method “BeginScope” where you simply return back a new instance of an implementation of IDependencyScope (above class).


Now add a new xxxxConfig class in the App_Start folder with the following code and register it in your Global.asax class



References: Web API Dependency Resolver, IDependencyResolver, IDependencyScope

Entity Framework 5 Code First Development

I have always been a fan of Code-First development style ever since it came out with entity framework 4.0.  What some people don’t understand is that you don’t necessarily have to start with a bunch of C# classes and then generate the database, but you could use the code-first style using the DbContext class and expose any existing database through it.  If the schema of a table changes, lets say you added column “foo nvarchar(500)” all you have to do is add a string property to the corresponding c# class and you are done, no need to mess around with any edmx files (which by the way, are freaking pain to merge sometimes).

With EF5 here are the steps for a simple code first development:

I have three classes that I want EF to generate database tables for.  First create a class that extends DbContext and add three properties utilizing the DbSet class as below.  Now you could create a constructor that takes in a connection string and pass that to the base or just go along with the convention over configuration technique that EF uses and simply name your connection string same as the class – in my case “MyContext”.



Next thing I have to do is enable migration for this project.  Open up your package console and type in the following command.  Ensure that you select the right project (the one that contains your entity classes along with the context class) and also make sure that you have an App.config in that project with the connection string shown above.


After this is done it should generate the following class in a folder called Migrations.  This is where you could add seed data that will be inserted into the database when it gets created.  I have also set AutomaticMigrationsEnabled to true which will ensure that my database is always in sync with my models when I run the update-database command.  This setting should be set to off if you want more control over how migrations are done.


And finally we are ready to generate the database.  Open up Package Manager console (ensure DataAccess project is set as the Startup project along with selecting it as the default project) and type in Update-Database and you are done.  I always use –verbose flag so I can see the actual SQL that’s being used to create the tables.


References: EntityFramework Blog, Entity Framework 5 Performance considerations, Automatic Migrations Walkthrough, Migrations

SqlCacheDependency via polling

Whenever I approach a programming task my model is "first make it work", "then make it work right", "then make it work fast". Now I am not suggesting that during the first and second phase of the process that you completely ignore all performance related issues, such as looping through millions of strings or worse making a database call within a foreach loop, just use common sense and follow best software practices and give each line of code you write – the respect it deserves.

Caching is one of those mechanisms that I like to focus on at the very end, because if you’ve architected your system properly then you should be able to plug it in/out without affecting any of your other layers or components. The .NET framework supports two types of caching:

– OutputCache: the entire rendered html page is cached on the server during the first request, after that all subsequent requests simply gets the cached page for however long you have set the duration for.  There are different attributes that you can set, for instance “vary by param” “location” and bunch of other useful settings that makes it very flexible.

– Application Data Cache: You cache your application data to avoid making expensive database calls. A common example would be for an e-commerce site to cache all products. In this case products is a perfect candidate, since it doesn’t change that often and usually products belongs to some sort of a category hierarchy and you can cache all that related data for quick and easy access.

Putting x in cache and retrieving x from cache is the easy part, but what happens if the actual row in the database that x represents changes. Most of the times you can get away with it by relying on one of your expiration timers because it’s not that big of a deal to have out of sync data on your page for some time. However, on occasions where you do need consistent data on your page all the time, you can rely on one of the cache dependency mechanisms provided to you by the .NET framework. If your cached data depends on a file (rarely) then you can set cache dependency on that file so when any changes to the file happens your cache is updated accordingly. Most of the times however, your cached data depends on data from your database, in that case you can implement SqlCacheDependency. There are two ways of doing this:

1. Polling (older version):

a. Limited to data changes at the table level

b. An asp.net process polls your database at specified interval and invokes the necessary stored procedures (running the aspnet_regsql.exe tool installs these stored procedures).

c. This can be used with SQL Server Express version as I will demonstrate later.

2. Notification:

a. Can apply dependency on a SQL command (queries and stored proc) rather than only at table level

b. SQL Server sends notifications to subscribed application servers whenever the result set for the specified command changes.

c. Not available for SQL Server Express versions

Lets implement SqlCacheDependency using polling.  First open up visual studio command prompt in admin mode and run the following command to install the necessary stored proc on your database.  Here is the reference for all the command flags Aspnet_regsql.


Basically, I give the exe my database name along with proper credentials and it does the rest.  And here are the procs that it installed on my Shop database.


Once you are done with that, run the following command to tell which tables you want the SqlCacheDependency to monitor.  In my case there is just one “Products” and it creates a new table in your database that keeps a record of it.



This is all you need at the database end, so now lets jump in visual studio and wire it all up. 

Add the following section in your web.config, ensure that the connectionStringName attribute matches your actual connection string name defined in the <connectionStrings> section.  I have set mine to poll every 15 minutes for this demo.


Now when you insert data in your cache, specify that you want to use SqlCacheDependency by instantiating a new instance of it and passing it as one of the parameters for the Cache.Insert method.  In an actual application you probably should enable AbsoluteExpiration as well as NoSlidingExpiration by setting it to the appropriate values.


So, after these 4 steps we have a fully configured Caching mechanism (at least for the Products table) that uses SqlCacheDependency via polling to determine whether any products data has changed, and if so invalidates it and evicts it from the cache on our behalf – not too bad.

References: ASP.NET Caching Overview, Sql Cache Depedency, Improving Performance with Output Caching, Caching Architecture Guide, Caching