Wednesday, November 17, 2010

Nice Urls - Slugs

By default ASP.NET MVC comes with urls like the following: /products/details/256. I find this already some kind of improvement over the normal web forms urls with aspx filename in the end but having numbers in the url is not really fashionable and SEO compatible. So while refactoring my app I thought that I will also make some fashionable and beautiful urls using so called slug. A slug is url compatible (no special characters, no spaces) string that is generated usually from the title of the item. So with slugs you can have url like /products/details/back-pack. It is not at all so hard to generate slugs. So you need an extra colums in the database table and on creation you generate the slug than you just need a GetBySlug instead of GetById and it is all working. If you are like me and have started out with ids and then change for slugs and want to keep also the "old" urls working the best is to have a GetByIdorSlug that tries to parse the third part of the url, and if it is an int it gets by id otherwise by slug, and that way old and new urls will work. :) Ok enough talking. I get to the point. Here is the code how to generate slug:
 public static string RemoveDiacritics(string stIn)
        {
            string stFormD = stIn.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int ich = 0; ich < stFormD.Length; ich++)
            {
                UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
                if (uc != UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(stFormD[ich]);
                }
            }

            return (sb.ToString().Normalize(NormalizationForm.FormC));
        }

        public static string CreateSlug(string stIn) { 

            string slug = stIn.ToLower();
            slug = RemoveDiacritics(slug);

             // Replace unwanted characters with space
             slug = Regex.Replace(slug, @"[^a-z0-9\s-]", " ");
             // Replace multple white spaces with single space
             slug = Regex.Replace(slug, @"\s+", " ").Trim();
             // Replace white space with -
             slug = slug.Replace(" ", "-");
 
             return slug;
        }

Things I wish I knew about how to create an ASP.NET MVC app....

I have a website I am doing as a hobby, and it is an ASP.NET MVC web application. I have started to write the code in March and I have launched the site in July so it is not very small not very large either, it is a normal size web app. :) I am full of plans though what new features to implement and even since July I have done already three new versions so the app is getting bigger and bigger. It has now reached a point that I have started to feel that the application is not well written enough to be maintainable in the long run so I have decided to refactor the whole backend. Here is a list of things I wish I knew when I started the app:
  • In many sample databases and apps (at least in the ones I have seen before starting to do my app) the naming convention for the db table id is TableNameID (ProductID, OrderID...). This naming convention is a piece of crap. It is much better to call all ids just simply Id, and then you can make an interface IHasId and have nice methods that handle objects that implement IHasId. And I tell you to rename table ids is really painful though it is not impossible....
  • LINQ to SQL was basically replaced by Entity Framework, and EF is developed by Microsoft while LINQ to SQL is just in maintenance mode... So if you can switch. I have switched....
  • Dependency Injection is nice and it is nice to use an IoC container. If you want to understand what is this whole stuff I suggest this great presentation from Brand Wilson. I am using now Ninject.
  • A Service layer is great to have. When I have started to do the app I have thought that only large apps need service layer and anyway I have a simple web app, no business logic. But it turns out there is always some business logic and service layer is good also in not so large apps...
  • There is a great starter ASP.NET MVC app made by Rob Conery on codeplex. It is nice to download it and study it a bit and lean form it. The generic CRUD repository that is usable for all enities is especially great. Before I had All, Single, Add and stuff like that for all my entities separately, and this starter app taught me how to have just one generic. :) I love if I can delete code from my app and make my app cleaner with less lines of code. :)
  • Never trust yourself or EF or LINQ to SQL and always check what gets actually executed on the database. I think that these abstractions and writing queries in C# and using navigation properties is great but you can find yourself very very easily doing very ineffective things on the database level (like select N+1, really ineffective queries with tons of joins....). Always monitor what is going on in the database and entity framework profiler is a great tool for that.

Saturday, November 13, 2010

GIT branching and merging

Further useful git things about branching and merging: To list your existing branches:


$git branch


To check out visually what your branches are doing:


$gitk


To create a branch:


$git branch newbranchname


To change to a certain branch:


$git checkout branchname


To merge the changes made in the branch "branchname" into the current branch:


$git merge branchname


After merging the branch is not deleted automatically. To delete it:


$git branch -d branchname


To undo a merge:


$ git reset --hard HEAD


Merge branch obsolete into the current branch, using ours merge strategy:


$ git merge -s ours obsolete


Take out of source control some directories:

$ git rm -r --cached Somefolder/bin