1

I need some ideas how to implement a URL Shortener Service with WCF along with sql server as database.

I will handle my url shortening logic with WCF. this project will handle creating short aliases for the urls and write them on the database with the actual url so that the project which will redirect the ulrs could use them.

where I need the logic ideas is on how to create short random letters. I will only allow numerical digits and letters in american alphabetic. Also;

  1. it should make the first char a numerical digit and the second char a letter. rest could be anything. how can I implement that feature so that framework could create random aliases for me.
  2. firstly, it will create 4 chars long words. but what if there are no 4 chars long unused words left? how can I implement that feature? if there are no unique 4 chars long words left, it should create 5 chars long words but it should make the first char a numerical digit again and the second char a letter. rest could be anything on this, too.

I know that I will be using System.Random class inside the mscorlib.dll but honestly, do not know much about it. More detailedly, I do not have any idea how to create a random unique word with numerical digit and letters in american alphabetic.

tugberk
  • 57,477
  • 67
  • 243
  • 335
  • You almost certainly **do not** want any randomness involved. :) Also, duplicate? http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener – Sapph Apr 03 '11 at 08:21
  • @Sapph no, I do. didn't u see this sentence : **rest could be anything on this** this says I am allowing randoms :) – tugberk Apr 03 '11 at 08:26
  • @Sapph thanks for the link BTW but that is not entirely what I wanted. – tugberk Apr 03 '11 at 08:26
  • I was more suggesting that going for deterministic functions (as in the provided Q) are almost certainly a better solution than generating strings of random characters. What if you have a collision? Roll the dice again? – Sapph Apr 03 '11 at 08:29
  • @Sapph that's why I asked this question. to not have a collision. but some voice inside me is saying that I will end-up somehow using System.Random class. am I wrong here? – tugberk Apr 03 '11 at 08:33

2 Answers2

4

You do not need a Random function. Randomness only gives you a chance for collisions.

Simply use an incrementing, numerical, key and encode it. Your database already provides a way to create them.

A simple encoding would be Hex (base 16) but you can get shorter and fancier with a base 32 (or higher) encoding. I'm not sure if the requirement for 'first char should be numerical' is useful but it's easy to accomplish.

And while a reversible encoding seems logical, it's also quite feasible to store the generated encoding as a column (Key) in the database and use that for lookup. That allows more fancy encoding, even adding a (random) digit in front.

H H
  • 263,252
  • 30
  • 330
  • 514
  • does that solution give me to have strings chars inside the words? – tugberk Apr 03 '11 at 11:06
  • @tugberk "string chars inside words" doesn't mean anything to me but when writing such an encoding you break it into series of number 0..31 and you can simply use your own array of 32 symbols to encode. – H H Apr 03 '11 at 11:09
  • could you give me an example of how I can implement that or point me a source which I can dig in find it out? – tugberk Apr 03 '11 at 11:28
  • @tugberk, have a look here: http://stackoverflow.com/questions/729268/encoding-a-number-c-implementation-of-z-base-32-or-something-else/729285#729285 – H H Apr 03 '11 at 11:57
1

The URL that Sapph posted has some good background reading. With regards to your WCF/SQL combination. Obviously you need to put your data somewhere, so SQL Server is as good as anything. As for WCF, most of the URL shrinking services use a nice simple hackable URL structure. This means that you could potentially call it via JavaScript and get a JSON result as well as rendering HTML.

Given that scenario you could use ASP.NET MVC or a WCF hosted service. I'd probably go with MVC simply because it'd be easier and you'll probably need some kind of UI anyway.

Mitch Denny
  • 2,095
  • 13
  • 19
  • that's what I was thinking. I will do the logic with wcf and host that service inside an asp.net mvc app. so, I will also provide json, xml result as well as directly redirecting the url to an actual url. but my problem here is how can I implement random chars that feature. – tugberk Apr 03 '11 at 10:35