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

design patterns

Learn the Observer Pattern:

I am starting to write a series of blog post which would be named “Learn {tagline}…..”
In these series I would post things about design pattern, programming methodologies, skills etc etc

For today, I will start with my favorite Design Pattern. The Observer Pattern.

I would first state out what the GoF book, states as the intent of this pattern.

Intent

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Applicability of this pattern

  • When a change to one object requires changing others, and you don’t know how many objects need to be changed
  • When an object should be able to notify other objects without making assumptions about who these objects are. (Loosely coupled objects)
  • When an abstraction has two aspects, one dependent on the other.

UML Structure

observerpattern

Sample Code

I am going to write a sample application using WPF to show how to use the pattern. One may say its an overkill but it does show loose coupling.

First our pattern, our class diagram would look like this

classdiagram1

Our Interface ISubject and our Concrete Subject implementation

 

Next the interface for IObserver

 

Now that we got the pattern in place, let start with creating two UserControl in WPF, we will create an English and Turkish Label Control that implements the IObserver interface, as shown in the class diagram below
classdiagram12
English and Turkish Label Control implementation

 

Simple nothing fancy about these 2 controls other than the Turkish one, which has English -> Turkish hard coded translation.

Now lets see how the WPF app looks like and how it hooks it all up.

 

Application Screen shot

observerapp

Conclusion
Our main WPF app hooks the EnglishControl and TurkishControl with the Concrete Subject by using poor man’s Property Injection in the Default Constructor of the App. When a button is clicked on the App it sets the Subject State to a random English word from the list. This in turns calls the Notify on the Subject and it will then notify all its IObservers.

Additionally I would like to mention that we have used the pull method. Where we request to get the state from the subject, we could also use the push method where the subject will sends details information about the change to the observer. The push method makes the observer less reusable, because Subject class makes assumptions about the Observer classes that might not always be true. I suggest one to use the pull method whenever possible.

Once you understand the Observer Pattern concept, it is very easy to implement your own Events, the basic idea of Events is the Observer Pattern. In my next post I will show you how to use Events and Delegates to implement Observer Pattern in C#

You can also download the entire source code.

Hope you enjoy the first series of the Learn topics, will be posting more in the future.

mysql

Here is another post moved over from old blog about mysql installation on FreeBSD.

Installing the mysql ports

 

I will be using mysql50-server from the ports section of FreeBSD 7.0

The above command will build and store the database in /var/db/mysql

One will also need to add to rc.conf file to start mysql

The scripts of where the database to start is stored at /usr/local/etc/rc.d/mysql-server

But before we start it, we will create a my.cnf file and store it in /etc/my.cnf

By default the install provides a list of my.cnf files stored in /usr/local/share/mysql

I will be using the my-large.cnf since I know I will be using large data tables, but choose one that you find fits your needs.

Now lets start the database (assuming you are logged in as root)
Now that the database has started, lets set the root password for mysql

To setup root password for first time, use mysqladmin command at shell prompt as follows:

However if you want to change (or update) a root password, then you need to use following command:

 

Backup of Mysql Database

 

For backup of the database I will be using mysqlblasy.pl

On can download mysqlblasy.pl from http://pol.spurious.biz/projects/scripting/mysqlblasy.php#download

I will be using mysqlblasy verison 0.73

In order for mysqlblasy to run one needs to have the configuration file ready for it

Here is a sample configuration file that I placed in /etc/mysqlblasy.conf

This will allow root user to use mysqldump with password provided above and it will rotate the logs 7 days, compress it and store it at /database/mysql-backup

Lets try to run the command now (I got an error since I have not got some of the Perl modules in)

So lets fix it and install the Archive/Zip perl module

Now when we run

We should have a mysqldump stored in /database/mysql-backup

Lets now add it to the crontab at the end of the file.

This will backup mysql everyday at 11:00 pm

UA-4524639-2