Taswar Bhatti
The synonyms of software simplicity
serviceLocator

There are certainly many ways to stub out dependency, using constructor injection, etc etc. Here is one way to quickly stub out ServiceLocator rather than implementing ServiceLocatorStub.

Lets say you have code that uses a helper and the dependency is not quite the responsibility of that object but its the sub object that uses it. As an example the IAccessHelper uses some security server to check some values. But the AccountController doesnt use it so why inject security service in AccountController? In that case you will call a service locator to get you the AccessHelper object.

Now if you wish to unit test this you will need to do quite a few things to stub it out. The quick way to do this is using property in C#

So that now when you write your unit test you dont have to care about service locator you can just stub out your IAccessHelper by just passing in a mock or stub to it.

This helps one to quickly break out the dependency of an object, that would otherwise take too many lines of code to test out a simple idea.

json

I found this weird error when I was testing a no trivial test case in my app and was not able to find out what was wrong with it.
It appears like a Json error in my asp mvc app, although the stack really doesn’t help 🙂


Basically what is happening is Json does not like to have circular reference ie. A -> B -> A
The easiest fix is to use [ScriptIgnore] in your ViewModel to tell it not to serialize object that may cause such a circulate issue.

Add the System.web.extensions reference to your viewmodel and make the elements that may cuase issue to scriptIgnore

example

By doing so now your json would look more like { Id: 3, FirstName: ‘Taswar’ }.
Also there is JSON.NET that provides [JsonIgnore] attribute if you are using json .net

jquery

So here is an interesting jquery plugin called jqprint it allows one to specify any element and sent it to print.

Assuming you have an element with id printButton and a content area called divToPrint

This will pop up the print functionality in your browser and send it with the default media=”print” css or else it picks up your default css. Kind of neat.

One thing to be mindful of in Firefox is the overflow attribute in your css stylesheet if you have overflow: hidden you will only get one page to print only.

Example:

Remove the overflow and it would print all the pages of a div.

jquery

So for some reason IE just loves to cache things for you when you call an ajax method, thus my JQuery get ajax call not working in IE.

Something like

This will work every time in firefox but IE will just call the cache version, thus whatever you have changed in the post version would not matter.

To fix this one can use datetime to append to the query string in each call or just use jquery ajaxSetup like

or use the $.ajax call and pass in cache: false.

jquery

So was having issues as in why does my checkbox doesn’t work in IE when I do

And found out that jquery does not bind live “change” events, I was told that it works in 1.4.2 but for some reason I still was not able to get it working.

The solution that I found was to use the livequery or bind method thus the code would become

or if you are using Ajax then

That should do the trick, remember to add jquery.livequery.js if you are using livequery in your file.

ASP.NET-MVC

Here are some changes that affected my asp mvc update process, specifically ASP .NET MVC2 Changes: GetControllerInstance & DataAnnotationsModelBinder.
By the way, there is a tool that does it for you and also a manual way .

But once you have updated there are also 2 more changes that you may need if you are using IoC or DataAnnotations.

First you will need to change the method in your Ioc to look for controller

into, notice the RequestContext in the parameter

The other change is in DataAnnotationsModelBinder if you have one, rather than inherit from Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder inherit from DefaultModelBinder

Hope this helps 🙂

sparkviewengine

So you want to use Spark View Engine: Highlight Table Alt Row.
Here is how one highlights an alt row with spark, quite clean I tell you.
Assuming you have a css that like below.

And in your template you will call it like this.

Note: How I am using personIndex. It basically goes with the item, thus if you have var order in Orders, you would then say orderIndex

linq

So sometimes you will get these strange errors or exceptions when using Linq Sequence contains no matching element

For example if you use

This will throw InvalidOperationException with Sequence contains no matching element.

But if you use

Which will return you a null if its not found then you can check if the result is null and continue on with your work 🙂

Hope this helps 🙂

asp.net

There are tones of blog post out there on how to redirect http to https. As in when a user request for http://myserver.com/Login.aspx
they will be redirected to https://myserver.com/Login.aspx

One can do it with IIS but I find that way a bit ugly since you have to create another site and set the 403 to redirect to another location. Not as elegant as apache mod rewrite.

A more elegant approach I found when using asp .net is by using an HttpModule in your code, so that the entire site is redirected. There are other solutions to redirect individual pages but for this post we will focus on redirecting all traffic to SSL.

To begin with we will code up an RedirectToSSL.cs page and place it into our App_Code folder.

Then all we have to do is add this line in our web.config file in order to enable it.

Replace the solutionName with your project name and voila you now have the entire site in SSL and you don’t have to worry about talking to an admin for IIS configuration or anything like that.

ASP.NET-MVC

So I was going through some code of codebettercanvas and found it interesting of Karl Seguin using Html Helper extension for his form tags, which has its elegance.

e.g

Basically it calls an extension method called LinkTo with a generic controller and a lamda for calling its action, the extension method looks like

This would work great if you are using IIS 7 or some kind or rewrite in your URL but if you are using IIS 6, where you might need some special extension in your routes for IIS 6 to surf ASP MVC e.g {controller}.mvc/{action}/{id} pattern. (Steve Sanderson does a post on deploying it on IIS 6) Then you are out of luck to use the html helper extension method form tag. Since the application path does not know about the .mvc extension you added to the route.

But not to worry one can use the UrlHelper class to find the path. Here is an example of it.

Now all you have to do is use this in your form e.g

Please note I am using sparkviewengine for my views in my asp mvc

UA-4524639-2