Wednesday, April 25, 2012

Leading zeros in SQL


So at work today, I ran across a SQL stored procedure called 'fixLeadingZeros' that has the below implementation.

It's actually much longer than this since it does it for several columns.... and some columns have longer values. In other words, if the field is supposed to have 8 digits, it has 8 statements.  I couldn't help but to feel a bit disgusted from it and had to share it.
UPDATE  [SomeTable]
      SET SomeColumn = '0' + [SomeColumn]
WHERE 
len (SomeColumn) =4
 
UPDATE  [SomeTable]
      SET SomeColumn = '00' + SomeColumn
WHERE 
len (SomeColumn) =3
 
UPDATE  [SomeTable]
      SET SomeColumn = '000' + SomeColumn
WHERE 
len (SomeColumn) = 2
 
UPDATE  [SomeTable]
      SET SomeColumn = '0000' + SomeColumn
WHERE 
len (SomeColumn) =1
 
UPDATE  [Some]
      SET SomeColumn = '00000'
WHERE 
SomeColumn ='' OR SomeColumn is null

This could have been done a LOT easier.  If your goal is to update existing columns so that they have leading zeros, all you'd need to do is this (assuming the column is a varchar type already and we wanted a 5 digit value):


UPDATE  [SomeTable]
SET SomeColumn = RIGHT('00000' + SomeColumn, 5)

What this does is specifies a string that has 5 zeros and the value of the column, ensuring that we have at least a string with 5 zeros.  Then we just want a subset of those values, from the right. In this case, we only want 5 characters from the right.

Let's say the value of SomeColumn is '123'.   We are then combining '00000' with '123' to give us '00000123'.

We then only take the 5 characters on the right, giving us '00123'.

Voila! Leading zeros.



Friday, March 23, 2012

Properly disposing COM objects in your .NET code

Here at work, we are using a third party component where they provide a COM interface.

We're using .NET code so we are using COM interop to use the COM object.

Remember back in the VB6 days when you would set your objects to null when you're done with it? Well, so did someone else here, because that's what I saw.

I'm going to show you an example using the Microsoft Excel Type Library.

Here's some sample code that I wrote that will open up an Excel file and get the text value from a cell in the first worksheet.


    using System;
 
    using Microsoft.Office.Interop.Excel;
 
    internal class Program
    {
        #region Methods
 
        private static void Main(string[] args)
        {
            var app = new Application();
            var books = app.Workbooks;
            var book = books.Open(@"C:\Temp\doc.xlsx");
            var sheets = book.Sheets;
            var sheet = sheets.Item[1] as Worksheet;
            string cellText = string.Empty;
 
            if (sheet != null)
            {
                var cells = sheet.Cells;
                var cell = cells.Item["2""B"as Range;
                if (cell != null)
                {
                    cellText = cell.Text.ToString();
                    cells = null;
                    cell = null;
                }
 
                sheet = null;
                sheets = null;
            }
 
            app.Quit();
 
            books = null;
            book = null;
            app = null;
            Console.WriteLine(cellText);
            Console.ReadLine();
        }
 
        #endregion
    }

See what I did there, I set all of the object references to null, which is how it was done back in the VB6 days, but we're using the .NET CLR. Things are different.


So what the program does is essentially fires up an instance of excel.exe, and then does what I asked it to do which in this case is to just get text from a cell.


The problem is that after my program finishes... excel.exe is still running even though I called "app.Quit()". That's because it can't quit while I still have references to the objects. Simply setting the references to null didn't do the trick. The managed runtime of the .NET CLR doesn't know how to garbage collect unmanaged COM Objects, so even though these references are null, they won't be properly disposed.


So how do you properly dispose of these references? By using Marshal.ReleaseComObject or Marshal.FinalReleaseComObject. Click on the links to read more about each method.


Here's the new version of the program:


    using System;
    using System.Runtime.InteropServices;
 
    using Microsoft.Office.Interop.Excel;
 
    internal class Program
    {
        #region Methods
 
        private static void Main(string[] args)
        {
            var app = new Application();
            var books = app.Workbooks;
            var book = books.Open(@"C:\Temp\doc.xlsx");
            var sheets = book.Sheets;
            var sheet = sheets.Item[1] as Worksheet;
            string cellText = string.Empty;
 
            if (sheet != null)
            {
                var cells = sheet.Cells;
                var cell = cells.Item["2""B"as Range;
                if (cell != null)
                {
                    cellText = cell.Text.ToString();
                    Marshal.FinalReleaseComObject(cells);
                    Marshal.FinalReleaseComObject(cell);
                }
 
                Marshal.FinalReleaseComObject(sheet);
                Marshal.FinalReleaseComObject(sheets);
            }
 
            app.Quit();
 
            Marshal.FinalReleaseComObject(books);
            Marshal.FinalReleaseComObject(book);
            Marshal.FinalReleaseComObject(app);
            Console.WriteLine(cellText);
            Console.ReadLine();
        }
 
        #endregion
    }

Give it a try yourself. Run the app and keep an eye on task manager. When the app gets to the last line, Console.ReadLine(), you'll notice that excel.exe is still running in the first version of the code. Now run the second version and wait for the program to get to the last line, you'll notice that excel.exe is not runnning. 

Useful links:
Mixing deterministic and non-deterministic cleanup


Tuesday, March 20, 2012

Debugging .NET Windows Service apps more easily

Here at work, I keep seeing a couple of my fellow developers struggle to debug their Windows Service apps that they're writing.  What they have to do is remove the service, install the new version of the service, start the service, then attach the debugger.

If there's an error when the service first starts up, the workaround is to introduce a Thread.Sleep before it actually does anything, giving you enough time to attach the debugger.

By default, you can't directly run the service from within Visual Studio. If you try, you get the following message:



If you look at the code that actually starts the service, you'll see that it's not that different from a console app.

By default, it looks something like this:

    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { new Service1() };
            ServiceBase.Run(ServicesToRun);
        }
    }
And the Service1 class looks like this:

    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            this.InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
            Console.WriteLine("Hello, world!");
        }
 
        protected override void OnStop()
        {
        }
    }
The code that runs when the service Starts happens in OnStart() .  There are other events that you can override, such as OnPowerEvent, OnSessionChange, OnShutdown, but for the most part, you'd primarily be concerned about OnStart.

So how can we get a Windows Service to run like a console app?  Pretty simply actually. I'm just going to post the solution here and walk you through it:

    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main(string[] args)
        {
            var service = new Service1();
            if (Environment.UserInteractive)
            {
                service.Start(args);
                Thread.Sleep(Timeout.Infinite);
            }
            else
            {
                var servicesToRun = new ServiceBase[] { service };
                ServiceBase.Run(servicesToRun);
            }
        }
    }


First, you should change the signature of Main() to include an array of arguments.
  private static void Main(string[] args)
Next, we'll want to create a new instance of Service1 and keep that in a variable.
var service = new Service1();
Now comes the magic part.  We want to be able to check whether the app is being ran as an application, or as a service.  How can we determine that?  Simple:
if (Environment.UserInteractive)
The above code checks if the application is being ran by the user, or as a service.  If it's being ran by the user, then we want to treat it like a console application, otherwise, run it as a service.  This gives us the flexibility of being able to run it in two different modes.

Next, you'll see me calling a method on Service1 called "Start" instead of "OnStart".  The problem is that OnStart is protected, which means we can only access that method from derived classes, and since Program is not deriving Service1, we can't access that method.  The simplest way to be able to call the "OnStart" method would be to just add a public method to Service1 that will do it for us:

        public void Start(string[] args)
        {
            this.OnStart(args);
        }

And now you can run and debug your service from Visual Studio without having to go through the trouble of installing it.  Once you feel that you have it working, you can then install it by creating a Installer Project, or manually installing it using the "installutil".





Monday, February 13, 2012

Stepping out of my comfort zone, trying different languages and web frameworks.

For the longest time, I've been developing on the Microsoft technology stack, with ASP.net MVC using C# for web application development, WCF for web services, MS SQL server for the database, and of course, Windows and IIS for the OS and web server.

But I then started asking myself... is this the best way to develop? What are some of the other languages and web frameworks like?  Easier? Harder?

I knew I didn't want to really mess around with PHP, just because I don't like how the code feels like spaghetti code of classic ASP.  Another reason for not going PHP is due to performance benchmarks.

Here's a neat site that shows how programming languages compare in performance:
http://shootout.alioth.debian.org/u64q/which-programming-languages-are-fastest.php

I decided to try out the Python language, using the Django web framework thinking that it would perform fairly quickly due to the fact that it creates "compiled" files, but found that simple web pages rendered slowly.  This of course could be due to the Django framework slowing it down too.  However, I did really like programming in it and found that the MVC model closely matched that of Microsoft ASP.net MVC making it fairly simple to transition.

I then started to read a lot of hype over the Lift framework, and it helped to have a big name like foursquare using it too. It uses the Scala programming language which I then started to practice with a few tutorials.  The source I used primarily for learning was this easy to follow eBook called "Programming Scala" written by Dean Wampler and Alex Payne.  Lift uses an interesting approach where you decorate your view with CSS classes and your code targets those classes.  The concepts really seemed nice on paper, but trying to actually use it is really hard with their poor documentation.  Many of the documentation pages still say "TODO" or "FIXME".

Getting back to scala... scala is a mix of functional and object oriented programming. By default, it uses the Java VM to run so the performance should be comparable to Java. The nice thing about it is that you can run scala on the .NET runtime.  It also seems to be getting a lot of hype so I thought it'd be worthwhile to invest some time to learn.  Not being thrilled with the lackluster documentation that lift has, I found another framework that runs on scala, which is the Play framework. Play originally runs on Java, but you can download the scala module and create a scala based project.

Play's current support (version 1.2.4) for scala isn't quite up to par with the Java support however, version 2.0 will be built for scala.  Play so far has been my favorite.  It does have a very similar feel to ASP.net MVC, and the convention over configuration model made it really easy.  I looked at Struts and Spring MVC a while back and was turned off by how much configuration it required and the poor template engine.  Play MVC realizes that their is already a language built for the web which is HTML/JavaScript and doesn't try to hide that.  They do have a few helper methods for creating form elements and script references, but they don't create a bunch of non-standard tags for your markup unlike other frameworks.  Play's library make it easy to create comet actions, or web sockets for your web apps too.

Not to mention, the performance isn't too bad.  When combined with an alternative template engine such as "fastergt" or "japid", the performance is even better.

After doing some research, I think Play is going to be what I'm going to play around with (no pun intended) to expand my horizons and get a feel for a different product.  I am pleased with it so far.  Even though Scala is the big thing right now, the current version of Play doesn't have the same support level as it does for Java, so I've decided to use Java.  It's also easier to program in Java coming from C#.

As far as IDEs go, I've been playing around with a few different ones, primarily, eclipse, Netbeans and IntelliJ.  I've already used eclipse before for Java/Android development so I'm familiar with it already.  Netbeans was ok, but just didn't feel like it had all the features.  Finally, I was hesitant to try IntelliJ because it's not a free product, but I do like JetBrains (the maker of IntelliJ) because they make a great product for the .NET world called ReSharper. I gave it a try and I must say I really like it.  It has great intellisense (not sure if that's the term they use, but that's what it's called in Visual Studio), and it already has support for the Play framework. The support mostly consists of having auto completion for the template tags and having a built in Play console. I'm currently using the trial version, but hoping I will be able to get a free copy with their open-source license, or I'll probably just end up buying it at their discounted academic price.

I'll keep you updated on my findings.  If you have any other ideas or suggestions for other languages and frameworks to consider, I'd love to hear it.

Wednesday, January 18, 2012

Wikipedia goes dark in protest of SOPA, how to get around this

I admire Wikipedia for raising awareness to this issue, and if you found this blog through searching, then you already have been made aware.

If there is something you absolutely must see on Wikipedia during the blackout period, here's how you can get around it.

Drag the link below to your bookmarks bar.

Show wikipedia content

Load up the Wikipedia page you want to see. You'll probably be able to see it for a brief second until it's covered up by the SOPA information.

Once you have the page up, click on the link that's on your book mark bar.

Essentially, it just removes the SOPA overlay and then shows ALL hidden elements... even elements that are normally supposed to be hidden.  I didn't really feel like spending too much time getting it to work elegantly for only needing this for a day, but it certainly does the trick.

Monday, January 16, 2012

Learning new web frameworks

So in case you didn't already know, I'm a web developer and have been doing it for over 10 years now and still really love it.  It's always changing and there's always new things to learn and new challenges and the web is relevant to me and almost everybody else.

The majority of my career has been around Microsoft based technologies such as ASP and ASP.net. Currently, I've adopted Microsoft's ASP.net MVC framework and really love it.  It makes sense and is easy to use.  The razor template engine is really great too.

However, I wanted to step out of my comfort zone and learn some new frameworks, see what they do differently or better, and allow me to experience new languages.

I first started off dabbling in Python and using the Django framework.  The language is nice and I was pleased with the framework, but wasn't really impressed with the performance. Also, not a lot of major sites have adopted it so it doesn't seem to be gaining a lot of popularity.

Then I tried out Spring MVC which uses Java.  From what I read, I thought I was going to like it, especially with Spring's built in DI framework and various other components, but it took too long just to configure a project and get it going.  There was way too much configuration, and I prefer convention over configuration. Also, the template engine I didn't care for much and would have had to rely on a third party template engine like Apache Tiles to effectively create templates. Importing tags and the tag styles remind me of ASP.net web forms, which I liked initially due to the ease of use and reduced coding, but then you lose flexibility and control of the rendering.

Then I looked into the Scala language which is a nice blend of OOP and functional programming.  It can be compiled against the JVM or .NET CLR, which is nice since you can still use all the Java libraries out there.  C# has some functional paradigms such as lambda expressions and the ability to treat functions as variables using the Action and Func types.

Two frameworks that I've looked into for Scala are the Lift and Play frameworks. Play also supports Java, but for the Scala version, uses a Scala based template engine that out performs the Groovy template engine that Play uses by default for the Java version.  Lift looks promising and is currently used by FourSquare, however, the documentation is lacking and incomplete, with many pages still saying "TODO".  Both frameworks also allow you to save changes and go without having to recompile and re-deploye/restart your app.

I decided to invest my time learning Play since it has a style that I'm familiar with and has better documentation at the moment.

What is your favorite framework and language to use for web development?