Taswar Bhatti
The synonyms of software simplicity

So haven’t posted for a while, was in home city Hong Kong for holidays and busy with work releasing software, gonna be posting a few more things coming up this month so be tuned in 🙂

Javascript

So was going through some legacy code to fix some security issues. One of them was there were links that were passing the data on url request. e.g NewFile.aspx?uid=1234

Rather than storing data in a session sometimes developers use shortcuts to do this, could be due to the pressure or time limit we have in shipping a product.

Aside from that lets see how we can fix this issue, what we want to accomplish is to post some data without calling server code and we can achieve that by some tricks in javascript.

Lets say you have a link that will say <a onclick=”javascript:NewFile()”>New File</a>
(Note I know this is not good again its legacy code)

Now we want to make a post request to the window and pass in the data.
Here is how we do it, I created a blank html page first and used this javascript.

By doing so we can pass in the data to the NewFile.aspx page with a post request now, also note if you are using Request.QueryString[‘uid’] in the NewFile.aspx page you will need to change it to Request[‘uid’]

Hope this helps 🙂

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 🙂

UA-4524639-2