Taswar Bhatti
The synonyms of software simplicity
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

powershell

Currently I am doing some performance testing on our application and part of it is seeing how well do we handle “insert” in our application. (Are there any problems etc, how many can it handle, etc)
I am using JMeter to test the performance and it is quite a flexible product that I will talk about later in one of the blog post on using JMeter to perf test ASP .NET application.

So back to PowerShell. Basically I needed a good source of data to (e.g real live data) that people type, so what came to my mind was, why don’t I just use some RSS data out there and try it out, it gives good and real information rather than generic data like “Hello World” that I may generate.

So I took upon to use the CBC News for Ottawa as an RSS feed, local news good data and it changes everyday and every 15 minutes or so.
Please note don’t hit the server continuously to try the script out, I don’t want CBC to be made at me 🙂

So here is a simple script.

So this will output data on the screen with
e.g This news is awesome|Reports have reported that this news is awesome|http://www.someurl.com

The reason I choose “|” rather than csv(comma separated) is because there could be data in there that could have comma in them.

Also in order to output it to a file one has type into PowerShell prompt
$ CBCRssPopulate.ps1 | out-file “cbcnews.txt” -encoding ascii

Please note the ascii setting at the end since by default everything in PowerShell is unicode and you might get into issues later on for parsing data.

Recommended Tool for PowerShell editing is PowerGUI http://www.powergui.org/index.jspa

Have fun 🙂

base64

How to do safe base64 url encoding in ASP .NET, I came into a problem of url encoding in IIS 7 where it does not like “+”, “/” in the Url. Basically a Url that looks like of like this

http://fake.abc/TIRlcEq0umjO6uJqtqvnkUGntUzv19rK+8mcvPK5qL1bwEZtEUqTlc3iF/TomuXU746Il5IF2iN9SeYuYDqt6SQzfdrv+Ltug2KZteKlYawc=

I know you may say what is this??? Its actually some kind of encrypted data that is in base64, but since IIS 7 does not like “+” or “/” even if its url encoded.

So I made a Utility class string extension method that just simply replaces the characters with “-” and “_”

Here is the code so maybe someday it would help you out also.

And always there should be a test for it.

There are other ways of fixing this and one can read them at
http://blogs.iis.net/thomad/archive/2007/12/17/iis7-rejecting-urls-containing.aspx
http://bentaylor.org/dasBlog-Patch-Add-Choice-Of-Dash-For-Post-Title-URL-Spaces.aspx

SQL2005

So was just in the process of installing SQL2005 on one of our VM to test some stuff. Guess what install went great with Client Tools selected, but unfortunately SQL Management Studio doesn’t get installed??? Why ohhh why?
I cannot find SQL Management Studio after install of SQL2005

So anyways, the solution is quite simple don’t use the installer again, simply go into the CD Drive:  Tools\Setup Directory

and launch SqlRun_Tools.msi and remember to click on client tools and check the tree if everything is selected and then click on install. Then viola you will have SQL Server Management Studio.

Working Effectively with Legacy Code

In this Learn Series I am going to write about the Sprout Method which is in the book “Working Effectively with Legacy Code by Michael Feathers“, all I have to say is what a great book if you don’t have it, you should pick it up asap.

Sprout Method is a technique that one can use when one has to write a new feature into a system. The code will be formulated as new code, and you want to put the new code into a new method and call it where the new functionality needs to be.

The reason for Sprout method is that sometimes you cannot write unit test for the code that you want to change but you can use Test Driven Development to write unit test for the new code, so that at least the new code would be tested.

Let look at some code to see what that means.

The code looks simple and now a change request has come in, the clients is requesting that they want to be notified of unique files names that would change. Not every file since there could be other files with same name but in different directory, and what is important is only one of them should be notified. Which make sense, since you don’t want to receive multiple emails on the same name files.

Your first instinct would be to call the method and pass in unique files, but you cannot do that since the call is being used somewhere that is out of your control or is ghetto code calling your code, code that no one wants to touch or change due to XYZ reason, so you need to think back and code into the method. Let see how we can change it.

We have added a LINQ query to find out the distinct elements but how do we know that our LINQ was correct or is doing the right thing. And we are making this method do two things, filtering and then appending.

So what we do is move that call to another method and write test against it. I have left out the test code just to make things shorter.

So here it is the Sprout Method and the advantages of it is you got your new code under test, the disadvantages is that you are not working on getting the entire code in test due to hard to break dependency or other reason that you may have and you might just not get it under test at all.

I would suggest the Sprout Method if you are starting TDD and would like to write little parts to get it under test and go from there at least you would get your feet wet.

All finally the methods as listed out in “Working Effectively with Legacy Code” for Sprout Method are:

  • Identify where you need to make your code change.
  • If the change can be formulated as a single sequence of statements in one place in a method, write down a call for a new method that will do the work involved and then comment it out. (I like to do this before I even write the method so that I can get a sense of what the method call will look like in context.)
  • Determine what local variables you need from the source method, and make them arguments to the call.
  • Determine whether the sprouted method will need to return values to source method. If so, change the call so that its return value is assigned to a variable.
  • Develop the sprout method using test-driven development.
  • Remove the comment in the source method to enable the call.
design patterns

Null Pattern

Last time I blogged about “Learn the Null Pattern“, but I forgot to mention one thing about the Null Pattern. That is you have to be mindful of it.

For example

The code looks okay but what if the GetEmployee method actually returns a NullEmployee? Well that would still be okay, since when the Pay method is called it would just do nothing.

Now what if we did this though

Now we get into trouble since we counted even the Null Employee.
Again be mindful of Null Object Pattern when you use it, since it may cause you headache in the future of finding that nasty bug, but nevertheless the Null Object Pattern is still a very useful pattern under your belt.

UA-4524639-2