Dependency resolver in .Net

Ninject is very cool and lightweight DI container.
But you can not always inject objects through constructor – sometimes you must have parameter-less constructor, like in MvcApplication object.
In that case you can use DependencyResolver from System.Web.Mvc namespace from System.Web assembly

_userDataService = 
DependencyResolver.Current.GetService<IUserDataService>();

Unfortunately MSDN documentation does not have much, only link to this blog post: http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html

Razor view: conditionally include debug versions of JS libraries

In Razor compiler directives do not work, cause Razor Views are not compiled.
So this would not work:

@{ #if DEBUG}

@{#else}

@{#endif}

But you can trick that thing into submission, thanks to this StackOverflow topic

A better, more generic solution is to use an extension method, so all views have access to it:

public static bool IsReleaseBuild()
{
#if DEBUG
    return false;
#else
    return true;
#endif
}

You can then use it like follows in any view (razor syntax):

@if(IsReleaseBuild())

Entity Framework, Update-Database command gives “Object reference not set” error

If Entity Framework (version below 5.1) on your attempt to Update-Database gives you error of “Object reference not set”, change the default start-up project to MVC project, not anything else. And try the same thing again.
This reported to be fixed in the latest 5.x version of EF, but it is a little annoyance just now.

p.s. This can be eased with parameter: -StartupProject “helloMVC” when running Update-Database command

Visual Studio Code Snippets

Visual Studio Code snippets are handy way of increasing your speed of development.

User keyboard shortcuts
CTRL+K + X for code snippet
CTRL+K + S for Surround With snippet

Here are most of the described: http://msdn.microsoft.com/en-us/library/z41h7fat%28v=vs.100%29.aspx

Or you can download this Word document and print them out – the same information, only in printer-friendly view.

EF 4.3 Database Migration Commands Cheatsheet

Code-Based Migrations

Original: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

Enable-Migrations
Add-Migration – scaffold next migration based on changes made to the model.
Update-Database – update database model to the latest migration model
Update-Database -Verbose – update database model and output SQL generated
Update-Database -TargetMigration: “someMigration” – update database up (or down) to a “revision”
Update-Database -Script -SourceMigration:$InitialDatabase -TargetMigration:”AddPostAbstract” Generate SQL script of the migrations
SQL(“update sometable set somefield=’hi'”) – can insert custom SQL into migration class.

Automatic Migrations

Original here: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx

Enable-Migrations -EnableAutomaticMigrations -Force – turn on automatic migrations

Basically does the same as code-based migrations, but before doing Database-update it automatically calls Add-Migration and gives it a name.

Visual Studo 2010 Useful Shortcuts

  • **CTR + ALT + L **- move to Solution Explorer
  • Type (for example “try” or “for”) and hit Tab twice for code snippet auto-completion.
  • Ctrl + – and the opposite Ctrl + Shift + – Move cursor back (or forwards) to the last place it was. No more scrolling back or PgUp/Dwn to find out where you were.
  • Ctrl + I – Incremental Search
  • Shift+Alt+Enter window in focus in full screen mode. Hit it again, and you have all the panels back. ****
  • Ctrl + Alt + I – jump to Immediate Window
  • Tools -> Options -> Environment -> Keyboard. Type “Navigation” to get the shortcut to combo-box on top of the window.
  • Ctrl + Shift + , or Ctrl + Shift + . to zoom-in or zoom-out
  • Ctrl + M + M – collapse/expand current region
  • Ctrl + M + O – collapse  all regions
  • Ctrl + M + L – expand  all regions
  • CTRL+K + X – code snippet
  • CTRL+K + S – Surround With code snippet More shortcuts are here:

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13189 Or here: http://www.itscodingtime.com/post/Visual-Studio-2010-Keyboard-Mouse-Shortcuts.aspx