Wednesday, November 28, 2007

Friday, November 23, 2007

Darwin's Radio, by Greg Bear

I remember reading Greg Bear books a while ago and thinking they were really cool. The coolness, of course, came from his hard sci-fi style, mixing good ideas with believable science. Now, after about a decade I suppose, I've decided to read another of his books and here is my review.
Darwin's Radio cover
Darwin's Radio starts a little like Village of the Damned, but without the extraterrestrial origin, continues a bit like The Stand, without the religious mambo-jumbo, and ends in waiting for Darwin's Children, the next book in the series. The plot is about a major and sudden evolution of the species, without the X-Men powers.

My personal feel was that the writing is less than I wanted. The book seems shallow and uninteresting after reading Peter F. Hamilton. While the science is interesting, it also makes huge leaps of faith and I wouldn't put it behind Bear to end the story with a good versus evil battle. Was I too young to appreciate good writing, starved as I was of anything interesting in my life? Or is this book not so hot?

I will certainly read the next volumes, but I am a bit disappointed. I feel like I am watching a play after seeing a real good movie.

Thursday, November 22, 2007

Nullable types and their boxing in O/R mapping

Nullable types are new to .NET 2.0 and at first glance they seem great. The idea that you can now wrap any type into a generic one that also allows for null values seems valuable enough, especially for database interactions and the dreaded SQL null.

However, having an object that can be both instantiated and null can cause a lot of issues. First of all, boxing! You can define a Nullable<int>, set it to null, then access one of its properties (HasValue). So suddenly a piece of code like obj=null; obj.Property=...; makes sense. But if you want to send it as a parameter for a method, one that receives an object and then does stuff with it, the object must represent the null value, which means it is no longer an instance of anything. Therefore you can't get the type of the variable that was passed to the method!

Quick code snippet:
int? i=null;
DbWrapper.Save("id",i);

With Save defined as:
public void Save(string name,object value)

Now, I want to know what kind of nullable type was sent to the method. I can't see that if the parameter signature is object, so I am creating another signature for the method:
public void Save(string name,Nullable value)

At this time I get an error: static types cannot be used as parameters. And of course I can't, because Nullable is a generic static type that needs the type of the wrapped value. So the type is actually Nullable<int>. My only solution now is to create a signature for every value type: integers, floating point values, booleans, chars and IntPtrs. String and Object are value types, but they accept null, so Nullable<string> is not used on them, but if you count all the other value types , there are 14 of them!

There is another option that I just thought of: a custom object that implicitly converts from a nullable. Then the method would use this object type as a parameter.
I tested it and it works. Here is the code for the object:
Click to show


Update:
There are issues regarding this object, mainly refering to constants. If you pass a constant value like the number 6 a method that expects either Object or NullableWrapper, will choose NullableWrapper. More than that, it will choose a NullableWrapper<byte> since the value is under 256. Adding signatures for int, double, etc causes Ambiguos reference errors. So my only solution so far is to consider the UnderlyingType of even Nullable<byte> as Int32. It is obviously a hack, but I haven't found a good programming solution to it, yet. If you have an idea, please let me know! Thanks.

Links:
Nullable Types (C# Programming Guide)
Check If Type is a Nullable Type - Convert Nullable Type to Underlying Type - C# 2.0 Generics

Friday, November 16, 2007

FxCop integration in Visual Studio 2005 Standard or Professional

FxCop is a free Microsoft utility that analyses compiled .Net code and makes suggestion based on rules, may them be design, security, performance of company policy rules.

The first problem is that you can only use it on compiled code. That means executables and DLLs. But what about ASP.Net 2.0? It doesn't build a DLL anymore, like 1.1 did. How can one use it? I have built an application (one that you will have to message me to send to you) that takes the project name as a parameter and then creates an FxCop project file with the DLLs from the site bin folder as reference DLLs and the DLLs from the Asp.Net temporary folder of that project as analysis targets. That means that you can also use it for ASP.Net now.

The second problem is that FxCop is now part of Team System, the overpriced and overhyped version of Visual Studio, and any attempts to use it with the Standard or Professional versions are cumbersome and undocumented. Siderite to the rescue! Here is a quick way to integrate FxCop as an external tool to Visual Studio and use it for either Console and Windows Forms applications or Asp.NET sites.

Step 1. Download FxCop. The latest version is 1.35, but there is also a 1.36 beta available that knows about lambda expressions and stuff like that.
Step 2. Get from me the FxCopAspNet application (completely free and with source), or build your own. Here is a possibly working link for it: at MediaFire.
Step 3. Open Visual Studio, go to Tools/External Tools and add two FxCop entries:
- FxCop [use C:\Program Files\Microsoft FxCop 1.36\fxcop.exe as Command, "$(TargetPath)" as Arguments and C:\Program Files\Microsoft FxCop 1.36 as Initial Directory]
- FxCopAspNet [use C:\Program Files\FxCopAspNet\FxCopAspNet.exe as Command, "$(ProjectFileName)" as arguments and C:\Program Files\FxCopAspNet as Initial Directory]
Step 4. Just open your web site or application in Visual Studio, compile it, then click on the FxCop item that applies.

Now, this is not meant to be a tutorial on FxCop, here are some links that might enlighten people:
Download FxCop 1.36 Beta
Download FxCop 1.35
Open Source FxCop Integration Add-in for Visual Studio 2005
How to copy the necessary files from Team System to Visual Studio 2005 Professional to make integration work
Use FxCop from your own code
A small tutorial
FxCopUnit, FxCop testing unit project
Video on how to create your own FxCop rules

There are a myriad rules included in the default installation of FxCop, some of them are just annoying, like some naming rules or some telling you you shouldn't raise Exceptions just objects inherited from Exception, but some are pretty good. A lot more can be found on the Internet and now, with the integration in VS Team System, I expect a lot more to pop-up.

Sunday, November 11, 2007

TV Series I have been watching

The third in this category of blog entries, this describes the few TV shows I have been watching lately. The previous entry can be found here.

House MD is a doctors show. A bit more interesting than E.R. and Grey's Anatomy because they actually focused on interesting characters and interesting medicine. However, after three seasons, the quirky intelligent doctor is just annoying, the supporting cast makes no sense and the medicine is becoming a joke. It's never lupus!

Prison Break is the new Lost. It had an interesting premise and I admit I watched it with pleasure for about a season and a half. Then it all went haywire. I predicted it, too... When people recommended the show I said "How the hell can you keep a prison break for 3 seasons?"; and the answer is that you can't. Not without bringing in mysterious government secret agencies with hidden and malevolent agendas. As acting goes, I like Dominic Purcell, interpreting the older brother, but I can't seem to see him in any decent show. You may remember him from John Doe, another clicheatic TV dead end.

Also in the list:
Numb3rs, even if the math has gone sour already and the show has basically turned into another pure police show. (good versus bad silliness)
Stargate Atlantis, which will probably end with this season without any other decent sci fi to replace it.
Battlestar Galactica, which was great, but moving towards boredom.
Regenesis probably won't get another season after season 3. It was never a great show, but it was decent, being Canadian and all :), with a bunch of virus hunters. It could have been a lot more scientific.
Grey's Anatomy is still on the market for my wife, but barely. A doctor show, but now it is very little about medicine and a lot about the human side of the characters; and who the hell wants a movie about people, completely overlooking the cool stuff happening near them?
Private Practice is an off shoot of Grey's Anatomy, but we never even started watching it.
Ugly Betty still sucks enormously and the wife still watches it.
Eureka. The second season waits on my harddrive, cobwebs on each file. Maybe if I am really bored...
Desperate Housewives takes the desperation way too far, probably reflecting the feelings of the screenwriters when watching the audience numbers fall.
A new season of Doctor Who will start in December.

Night's Dawn trilogy - Peter F. Hamilton

He did it again! Or would it be better for me to say he has done it afore, as I am kind of moving backwards in the Peter F. Hamilton writings? Night's Dawn is a huge book, about 9Mb of pure text, divided into three parts purely out of paper formatting reasons, I am sure. So far, this is the best thing he has written, at least in my opinion.

Maybe the guy is the kind of writer who writes his best work first, then tries unsuccessfully to follow up. Not that any of his followups could be called a failure, it's just that Night's Dawn is really cool! I mean who can seriously deal with possession, necromancy, devil worship, witch hunting, vampires, werewolves, ghosts and demons, all in a future world in which humanity has conquered space an tries to attack the situation with science and rationality? Seriously! If this guy would have written the Bible, there would be no Muslims! (Ron Hubbard, eat your heart out!)

There isn't much else I can say. I certainly cannot summarize a book that spreads over about a dozen inhabited planets, all with their own history, socio-economic situations described and own characters to add plot (real plot) to the story. Right now I am terrified. I need to find the Greg Mandel trilogy, which is the last of the Hamilton big stories, and there are only two outcomes: a) I hate it, which would have wasted my time and trust in humanity; b) I love it, and then I go into withdrawal waiting for the last two volumes of the Void trilogy and whatever else he brilliantly writes in the future.

Bottom line: if you like Sci-Fi, you need to read this. Hamilton and the ReSharper guys are the only people I ever felt the need to send money to in order to apologize for my shameless Internet piracy.

Links:
Peter Hamilton's official site
Wikipedia's entry on the Night's Dawn trilogy
Peter F. Hamilton's entry in Wikipedia.

Friday, November 09, 2007

Lateral parking solution for women everywhere

There are a lot of people in the world having problem with lateral parking. It is not only difficult to manage when you have nothing to go on but the little tiny images in the car mirrors, but stressful as well, as normally the procedure is done on a crowded street with drivers behind you urging you to do it faster.

Not a problem anymore! The Chinese have found the perfect way of parking in under 5 seconds! Here is a video tutorial on how to do it:



Here is an alternative solution.

Sunday, November 04, 2007

Fruit of the mall - part 2

Two months ago I wrote a post about the exotic fruits I found in a hypermarket in Sibiu, now it's time for additions, taken from the Bucharest Metro.

My own photo of the fruits
So here are the fruits I bought today:
the Maracuya
. It has the same feel as a fruit from my first post, the Kiwano. The taste, though, is very sour, like a lemon, and a little spicy. I could eat it, but I think people don't really eat it raw. The outer skin is hard, inedible and thick.
the Kaki fruit. It is very tasty, although it has a peary texture that I didn't quite like. It is a sweet banana tasting fruit.
the Cactus fruit has a sweet coating around the hard inedible seeds inside with a texture of baby food and taste like a not aromatic cantaloupe, similar to the Pepino. The seeds, though, make it less than pleasant to eat.
the Pepino mellon has a similar texture to the Kaki, but it has the taste of Cantaloupe, yet not so aromatic.
Now, the Papaya is an interesting tasting fruit. I am afraid my best approximation is still the cantaloupe, but the papaya also has its aromatic properties and the texture. Its aroma, though, is slightly different, more like banana. It has a big core of inedible seeds and the skin is also inedible. That makes the useful part of the fruit rather small.

In conclusion, one must definitely try the papaya and the kaki. The maracuya is the weirdest taste among all, not entirely pleasant, although I can try to eat it with sugar or something. The net suggests mixing the seedy content with water and sugar after letting the skin wrinkle.

Fried Calamari - simple and damn tasty!

I went to one of the Metro hypermarkets in Bucharest and I was pleasantly surprised by the variety of stuff one can find there, as compared with other hypermarkets that seem to be supplied from the same sources. Also check my next entry, I will write about exotic fruits there.

Calamari in a plate. My own picture.In this post I will talk about calamari! There is an entire store dedicated to fish in Metro, filled with a lot of nice looking and/or packaged treasures of the sea. My picks were swordfish stakes and one big calamari. Well, not that big... it's no architeuthis, but it will do.

I went home, made a longitudinal incision, threw away the awkward looking organ inside (which I suspect had a digestive function) and the eyes and beak, then threw it in boiling butter after putting a bit of spice over it. I removed it after 2-3 minutes and ate it. Yes, it's that simple! The taste is not strong, but really special and it was totally worth the buy.

Warning, as read from the googling on calamari: Calamari is either to be cooked in 2-3 minutes or in more than 30. Everything in between turns it to rubber. So, if you are like my wife and you want to spit it after you taste it, you might want to try the long cooking calamari recipes out there. :)

Friday, November 02, 2007

Getting the index or rank of rows in SQL Server 2005

A while ago I wrote a little post about changing a paging GridView in order to show certain number of pages and page index, but extract from the database only the data you needed. I was looking there for something in SQL Server like the MySql LIMIT, the T-Sql equivalent of TOP, but which accepts two parameters instead of only one.

I just found out that there is a way of doing this in Sql Server 2005 by employing functions that return the position of a row, depending on a certain ordering and partition, called Ranking Functions. From the little googling I did, it seems that Microsoft borrowed this technique from Oracle, but one never knows, maybe it was the other way around.

I will write a short example on ranking functions, then link to the appropiate articles. Given this SQL query:
select *,
Row_Number() OVER (ORDER BY name) as [Index],
Rank() OVER (ORDER BY name) as Rank,
Dense_Rank() OVER (ORDER BY name) as DenseRank,
NTile(3) OVER (ORDER BY name) as NTile,
Row_Number() OVER (PARTITION BY nr ORDER BY name) as IndexNr,
Rank() OVER (PARTITION BY nr ORDER BY name) as RankNr,
Dense_Rank() OVER (PARTITION BY nr ORDER BY name) as DenseRankNr,
NTile(3) OVER (PARTITION BY nr ORDER BY name) as NTileNr
from test
ORDER BY ID

you get the following result:
ID Name Nr Nr2 Index Rank DRank Ntile IndexNr RankNr DRankNr NtileNr
1 Mark 1 7 8 7 4 2 2 2 2 2
2 Mike 1 4 11 11 6 3 3 3 3 3
3 John 2 8 5 5 3 2 1 1 1 1
4 Charles 3 2 1 1 1 1 1 1 1 1
5 Ellen 3 6 4 3 2 1 2 2 2 2
6 Mary 4 1 9 9 5 3 2 2 2 2
7 Mark 4 17 7 7 4 2 1 1 1 1
8 Mike 2 41 12 11 6 3 2 2 2 2
9 John 6 83 6 5 3 2 1 1 1 1
10 Charles 1 72 2 1 1 1 1 1 1 1
11 Ellen 0 68 3 3 2 1 1 1 1 1
12 Mary 3 21 10 9 5 3 3 3 3 3


As you can see, Row_Number returns the row index, Rank returns the rank, Dense_Rank returns consecutive rank (no gaps between rank numbers) while NTile puts each row in a category using a given number of total categories. Partition by makes the operations work for each distinct value of a certain column, in this case nr. If the partition would have been on nr2, all the ranking values would have equaled 1, since there are only distinct values on the nr2 column. The Over clause can be used on more than just ranking functions; it also works on Aggregate functions. Yummy!

Links:
Ranking Functions (Transact-SQL)
OVER Clause (Transact-SQL)
Aggregate Functions (Transact-SQL)
This article also shows a similar method in Sql Server 2000 of which I knew nothing until today: Row_Number() function in SQL Server 2005
Returning Ranked Results with Microsoft SQL Server 2005

Thursday, November 01, 2007

Enabling GZip compression in IIS 5 (Win2000 and not XP) for ASP.Net files

Maybe it works for other IIS versions as well, but I certainly was looking for a way of turning it on on our Windows 2000 development/test computer. So this is the long story:
HOW TO: Enable ASPX Compression in IIS

and this is the short one:
Step 1: backup your site metabase
Go to the Internet Information Services (IIS) tab and right click on it, go to All Tasks, choose Backup/Restore Configuration and save it.

Step 2: make the change to the metabase
Create a .bat file that has the following content:
net stop iisadmin
cd C:\InetPub\adminscripts
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "aspx"
CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "dll" "exe" "aspx"
net start w3svc


Make sure to restart the SMTP service or any others that were stopped by the bat. I don't know how to start it from the command line and I pretty much don't care. The batch file will notify you of possible services it will shut down, but will restart in the end only the Web service.

The performance is immediately visible and it also works with Ajax.

Update:
This article was originally talking about Windows XP. Thanks to McHilarant (see comment below) I realized that, even if the changes in the metabase are possible on any IIS5 (Windows XP and Windows 2000), the actual compression will not be possible on XP. I remembered then that the actual modification that I did that time was not on my dev machine, but on our office server, therefore I updated the post accordingly.

Another Update:
Here is a link about a script to enable IIS 6 gzip compression: Script to Enable HTTP Compression (Gzip/Deflate) in IIS 6.

Speed up Javascript

I was looking for ways of speeding up my web pages, considering that one utility page I built reached 500Kb and from that, only 170kb where actually content, the rest was Javascript. Bad Javascript, I accept, but it works, ya know?

So, after thinking and thinking I thought of a novel approach to this: how about googling for it? and I found this very nice and comprehensive article: Speed Up Your Javascript Load Time.

There isn't much else I can say about it.