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

Learning

sylar
So there is this character (antagonist) in this TV series “Heroes”, and his name is Sylar

People might also know him from the new Star Trek movie, where he plays the young Spock. Anyways, back to the Sylar character, in the show he plays a serial killer who kills people with ability (e.g flying, shooting fire out of hands, invisible, shape shifter etc etc) and takes on their ability/power after killing them.

That sounds awful as a character but some may say that does sound pretty cool, if you could take like Beethoven ability to play the piano or Jimi Hendrix guitar playing ability. So what does this have to do with software development you may ask?

The ability that I speak of could very well be from one of your fellow developer. S/he may have some ability/knowledge in software programming that you may not have learned yet. If you wish to take the ability try and set up some time with them so that you can learn from them. Try pair programming with them or have them show you some stuff, the next time you are near their cubicle or office. There is a lot you can learn from others, you can watch them use their IDE or Resharper short cut keys, or just their design principles they may use in their application development. Trust me most developer’s don’t mind showing off to their coworker its part of their job to act smart 🙂

I think the quote from a TED talk by John Wooden a basketball coach sums up my entry pretty well:

“Never try to be better than someone else, always learn from others, and never cease trying to be the best, since that is under your control”

design patterns

Today, I thought I would mention the Null Object design pattern, its an object that provides intelligent do nothing behavior, hiding the details from its collaborators.

So what does this mean??? Before I tell you what it means, lets see an example of how often you might be seeing code that is checking for null.

Let say we have a Person object, that is implementing an abstract or interface IPerson and there is a repository somewhere that finds the person for us.

What happens when we don’t find the person, since the Find method above returns null, we end up with NullReferenceException thrown, because we are calling person.Firstname in the PrintPerson method.

So in order to fix the code we write if statements around it like so

And we keep writing code with the if statement over and over, every time we want to find we need to check the if(person != null). That is just too painful in my opinion.

So how do we fix it. We use the NullObject, and here is how it looks like in UML.

Null Object

Null Object

Basically we create another class that implements the interface or abstract class and all its methods and properties implementations does nothing, an example below

As you can see the NullPerson has no implementation code in the DoSomething method and it returns 0 for CalculateSalary.
Back in the repository code we will have to return NullPerson rather than null when we can’t find the person.

From here onwards you can eliminate most of your person != null code. Since whatever method or property you call on it, it would never fail and never return you a NullReferenceException.

You could do something like

Our code would not be littered, and we could start writing code that is more elegant that does not contain those nasty if (x == null) statements all over the place. Do remember we might have to implement more NullObjects for other classes that we will be using rather than mishmash of null and NullObject, which would make it ugly.

This pattern helps us realize that we sometimes don’t really want to know if the Get or Find method returns null or not we just want to do something like

One may also say that there might be cases where you want to an error message, then we can use this way to solve the issue

The NullObject is a very simple idea and once you start using it, you would truly get hooked on it and see the power of something so simple. We then start not caring about those if statements and an added benefit is that our code would be much cleaner and easier to read for others.

design patterns

As promised here is the version of observer pattern with Event & Delegate. Personally I am not a big fan of this solution, although it does remove quite a bit of code and show how to use a push model with the observer pattern. Next time around I will show a more elegant solution by using Event Aggregator.

But for now lets see how I implemented Observer Pattern with Event/Delegate.

First I tackled the Subject of our class, the thing you will see different is there is an instance object called TextChanged and is an EventHandler, the second change is it Notify method. I also removed the ISubject interface just because there isn’t really a need for it in this example and to make things simple.

 

From the above code you will see that there is an object called TextLangChangedEventArgs, that is basically our data object that would be pushed to the observer. Here is how it looks like, the important part is that it extends/inherits from EventArgs

 

Finally back to our WPFApp, we again have 2 labels the Turkish and English one, but they both don’t know anything about the Subject which is a good thing, so that it is loosely coupled. In order for the subject to hook into them they provide a signature where the event will call it.

 

Turkish UserControl does the translation of English to Turkish

 

Last but not least here is the WPFApp

 

I have not posted the XAML code but one can download the source and solution.

For next time I would like to show a more elegant solution called Event Aggregator which is a pretty cool pattern introduced by Martin Flower that allows one to write extensible code.

UA-4524639-2