Tuesday, March 19, 2013

HttpSessionState vs HttpSessionStateBase

ASP.NET MVC controllers have a Session variable of type HttpSessionStateBase. This is different from the Session variable available in Webform (in class Page) which is of type HttpSessionState.
The reason for introducing this new type of HttpSession is to improve testability since its easier to mock out HttpSessionStateBase.

However if you have an extension method for HttpSessionState that will be incompatible with an extension method for HttpSessionStateBase (and vice versa). You can work around this by making writing the extension method for MVC (i.e. HttpSessionStateBase) and then using HttpSessionStateWrapper for converting HttpSessionState to HttpSessionStateBase.

HttpSessionStateBase sessionBase = new HttpSessionStateWrapper(Page.Session);

Here is a sample example:

Sunday, March 17, 2013

Real private static class members in Typescript

The default implementation of private in typescript classes is a compiler enforced constraint only. Which means that if it is used from Javascript your members are still available.

If you really really want to prevent this then the following is one solution for private static members:

That is surround your class with a module and declare your private static variables in that module.

Thursday, March 14, 2013

Quick smooth fade in transition with Jquery

This is what I use when I need to test a quick smooth fade in transition with Jquery

// hide and show this element                          
$selector.css('opacity', 0).animate({ 'opacity': 1 }, 400);

The reason why I don’t use .hide and .fadeIn is that it causes the layout to jump which opacity avoids.

Resharper Tip : Pause temporarily for large files

Resharper can slow down older versions of visual studio (2005/2008) on large files for some reason. So when working on large files it helps to pause Resharper temporarily. 

The keyboard shortcut for pausing Resharper is ctrl+shift+alt+8
Instead of being stuck at: 
The icon changes to: 

Then when you are done editing the file you can unpause with the same shortcut (ctrl+shift+alt+8) 
Here is how I remember this shortcut: Its take an infinite amount of time so holy trinity + 8. 
where Holy trinity = ctrl+shift+alt and 8 = infinity on its side. 

Monday, March 11, 2013

Html.DropDownListFor ASP.net MVC usage

Basically this is how I like to use DropDownListFor and SelectList in MVC

However  I have a simple class containing the items and the selection in one place:

Where I have these two pretty simple classes.
One class for each item:

And one class for the complete encapsulation:
This also allows me to keep my select list models independent of system.web 

Thursday, March 7, 2013

Visual Studio Tip : Surround With

Something I use sparingly but find invaluable for my arsenal is the ability to quickly surround html elements with other elements. Especially useful when iterating over web design.

Select the section of code you want to surround with an HTML element:

Press the keyboard sequence Ctrl+(k,s)
I like to think of this as Control Code , Surround
You will get a popup like so:

 This popup works similar to how IntelliSense works i.e. type ht (to select html) and press tab to go into html submenu:

Similarly, Press di followed by tab to select and surround with div:

 One further mention , backspace key works as it normal in a text field when inside the surround with dialog.

Wednesday, March 6, 2013

Typescript function signatures

Typescript provides two distinct methods of providing function signatures and these vary with either being in module , class or interface.

Here is a sample on playground  for your convenience.

Tuesday, March 5, 2013

Generate POCO classes from your Database

There is a great post about how to do this manually by Jon Galloway that you should checkout : http://weblogs.asp.net/jgalloway/archive/2011/02/24/generating-ef-code-first-model-classes-from-an-existing-database.aspx


However I just discovered a great tool to do this much more smoothly. Available as Entity Framework Power Tools which is a visual studio extension:



Just right click on your project and select “Reverse Engineer Code First”

Monday, March 4, 2013

CodeMaid : You should hire one for free

A perfectly innocent line of code

But in the eyes of Style Cop it is evil. And justifiably so. This is a simple example but perfect spacing, removing and sorting usings and other small simple tasks start to accumulate. These are all still important things you should do for others and your own future convenience.

However these are things you would rather have your code editor do so you can focus on solving the actual problem you are trying to solve. Enter CodeMaid (http://www.codemaid.net/) .

It does these and more for you on file save. So your code looks the way code should look everytime.
If you use stylecop (which you should) you need a code maid.