August 28, 2012

A generic way to read data from an SqlDataReader

Through the years I have been struggling to find a really good way to read data from a datareader. If you know me, you also know that I prefer good old reliable methods that puts me in control to object relational mapping..

Consider the following code (didn't compile it so forgive me for eventual bugs):



    private static List<Book> ReadData(string connectionString)
    {
        List<Book> books = new List<Books>();
        string queryString =
            "SELECT Author, Title, Year FROM Books";

        using (SqlConnection connection =
                   new SqlConnection(connectionString))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Book book = new Book();
                book.Author = reader("Book");
                book.Title= reader("Title");
                book.Year= reader("Year");
                books.Add(book);
            }

            reader.Close();
            return books;
        }
    }
}

This code should work pretty well, the problem comes when you start using nullables. If year were int? instead of int this code this would throw an exception as soon as a null value appears in the reader.

Null in Sql Server and in c# are two different things.
From the database points of view a field has three values:

  1. A value. For example 1993.
  2. A null value. We left the year empty because we didn't know the year.
  3. Empty, the book does not exist.
Now you might argue that if the row was empty a try would anyway give you an error and the pragmatical side of me totally agrees, but still, the issue stems from the fact that null in C# is the absence of a reference to an object, while null in a database is an uninitialized or empty value. It exists and we have to deal with it.

I like solving this with extension methods (actually I am generally pretty hooked on extension methods).

The standard approach is to use something like this:

public static int? ToNullableInt(this int value)
{
    return value.IsNull ? (int?) null : value.Value;
}

And then call the reader like:

book.Year= reader("Year").ToNullableInt();

It is a reasonable solution. But you would have to write those extension methods for every nullable data type.

A single function version that handles all data types plus basic conversion but also is slightly slower looks something like this:

           private static T Get<T>(this SqlDataReader reader, string index, T defaultValue = default(T))
          {
                    var t = reader[index];
                    if (t == DBNull.Value)
                                return defaultValue;
                   Type type = typeof(T);
                   if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                   {
                               var subType = Nullable.GetUnderlyingType(type);
                               return (T)Convert.ChangeType(t, subType);
                   }
                   return (T)System.Convert.ChangeType(t, typeof(T));
           }

This would be called like this:

book.Year= reader.Get<int?>("Year");

When microseconds are not the issue, I prefer that solution, but I am always looking for something even better. Ideas? :)



August 1, 2012

My populator is beginning to take shape

I have been knee deep into my populate project the last days and I truly believes that meta programming is somewhat like LSD. It seriously fucks up your mind.

When you take a step away from your usual garbage in - garbage out project and enter the realm of reflection and generics stuff can be, well, abstract.

I hopefully will finish the project before ending up in an asylum.

In the meanwhile I am happy to see that there will be a Build conference this year. Read all about it here.

I went to the conference last year and loved it, will definitely take a talk with my boss about this one. So far there is no more info than that the registration will start at 8, the 8th day in month 8. Why are developers so beautifully nerdy? :)

Now back into the code again! See you!

July 18, 2012

Just to prove my theory...

In my last entry I made a Shakespeare generator.

I completed it with a Strindberg generator:


Som det!
Fiskar utanför ett ord.
Att följa mig från vita skjortbröstet och vila; fyllde i sin levnads mål, ingen annan sak till fånga och jag är bittert ibland med god dag?
Varpå ett tyst och lämnade Arvid Falk tittar jag säger oss, men nu svarade Sellén.
Ty hans brustna moln av utlänningen, tyst att han!
Låt mig!
Och vad har lossnat här, är gudfruktig!
Varför han så finns välgörenhet också!
Hennes nåd skulle komma hit utan ser jag tala, i Byggmästarbanken.
När di kommer visst inte känner sig böjd att Sellén, här har du opp.
Nu har du fått slag.
Men han uttalade alltid; den tog upp.
Stod Ygberg såg huru skulle fosterlandets namn; befann sig på vår begravningsbön.
Konglig sekter o den ena lakanet blivit snillen, att han sin överrock med en stol skjutas på en gumma, Karlen super ju värden höjde första revbenet.
Nåå!
Denne sköt hela sin motståndares argumenter.

And a Goethe generator:


Benutz es hinkte, höchster Herr ich schnell quillt, dem ich weiß, so wird nicht, folge ziehn die Gift?
Als sonst kamt ihr anständig seh' ich, der Geist verwirrt genug, Echo!
Zwar ist sie zu schauen aus millionen kehlen.
Da steigen meine Widersacher drängen, ich bei den erdball abgeründet, dem seeisch heitern feste Unzugängliche Mauer spaltet sich ganz ausgewachsen schauen, die Füße tragen; aber hüte dich auf!
Fackeln, die Kraft, Hiebei darfst du nichts an das Proszenium.
Nun fort, uns im dienen, wir?
Du scheinest mir doch gar selten aber flammt's empor und solch einen!
Begnügen sollt' ich allen Seiten hundert Hügeln unterbrochner Fläche fließt Peneios: auf!
Und Land, sie sich ergetzten, wir sehn!
Der flammend sich die thronen hehr, Umstellend ruhigen Friedensweiher.
Durchlauchtigster, angebaut.
Die Neigung, gemein!
Den Mittelpunkt.
Der schönsten einer längst gewohnt der Ernte zu dem heiligen liebeshort.
Ein Schnippchen schlagen deine macht erst sah sie genießen wollten, ein Marmorblock als er sich selbst außer mir den klügsten Mann, schlachterzogne junge Brut?
Ich nicht benommen; Fuß nimmt jeder, Chiron könntest du's doch gewandter sein, entflammte stroh.
Sind gewohnt, hier ja mein, wie das Papiergespenst der Treppe, so sehr es am besten frommt; über der einzig vom meinem Geiste.

Was considering a Bukowski generator, but I foresaw the results...

Lightning storms in Sweden when I write this.

July 8, 2012

The Shakespeare Generator


A fool thinks himself to be wise, but a wise man knows himself to be a fool.

I didn't write that. Shakespeare did. Wish I had though.

For us with an ounce less talent but with a will to cut corners I decided to make a Shakespeare generator.

So what is the Shakespeare algorithm?

I don't know, but I can show you my take on the subject.

The easiest way would of course to just randomize the words. In the example above you could get stuff like:
"A think a to himself."
Or
"be knows a to a"

Definitely words from the Shakespeare vocabulary, but still crappy. You will not find any memorable quotes there unless you randomize a lot. A lot like in this.

If I were a linguist I would try the other end of the spectrum. I would analyze each word and implement the grammatic rules on when and how to bend them.

For me that sounds too tiresome. I want quick results.

So I constructed a graph out of Macbeth.

The basic fact was that Macbeth is made out of sentences and the sentences are made out of words.
All sentences has a start and an end, made out of an ending sign such as "?", ".", "!". To make it easier for me I decided that "," ";" and ":" also ends a sentence. After all it is word groups rather than pure sentences that I am seeking.

So the graph starts with a start node and that node is connected to all words that starts a sentence, namely "a" and "but".


And then I just continued adding nodes for each word to the following word. Eg. "A" -> "Fool", "Fool" to "Thinks" etc. 

When a word came to an ending sign, I added a link to a node of that sign, such as "Wise" -> "," and "Fool" -> "."

When a word came up that already had a node, I didn't add a new node, but just a new edge, so the "Wise" node is connected both from "Be" and from "A". 

When the same word pair came up a second time, such as the double connection between "A" and "Fool", I added a one to the weight of the edge, ending up with a weighted graph.

I did this for the complete Macbeth which means that a word such as "A" is connected to 119 other words with different weights depending on how often those words shows up.

When my graph is ready I can randomize a trip through it from start node to an end node. Randomized based on the weight of the edges. Wise huh? This pseudorandomization also sees to that words usually have the correct ending and that all words fit with the word before it.

So some generated sentences:


Mingle with you make us.
Come to the subject of our chimneys were a ring the gate make such welcome.
Whey-face?
Every man.
Madam, 
and blind-worm's sting, 
I' the honest men our offices and 'tis the instruments, 
at peace?


"Come to the subjects of our chimneys" Poetry!

Next step will be to make a graph consisting of all Shakespeares works.

Am I wise or not? (or just a simple fool)

PS. The graph was made using this tool. Recommended!

July 5, 2012

Generate word like sentences

Randomizing data is fun so I decided to make a program to automatically fill objects with data.
i started out with totally random data, but I realized that 'lhghhjfdr' is not that fun to put as value in a string so I have been producing generators.

Generators that can do the Lorem ipsum, generate random names, superhero names, street names, titles, cities... and generating random stuff started to be fun again.

One thing I wanted to do was to generate word like words, strings that have no meaning but are easy to read and pronounce.  Pseudo random text.
I found this and it gave me a basic algorithm, I spiced it up with this to give each letter a statistical weight.

The random word is made of a random number of parts that are concatenated. The parts are either v, cv or cvc where c stands for a consonant and v stands for a vocal. which part type I choose is of course random.

The probability for each letter is according to the letter frequency in the english language:


private void LoadLetters()
{
Vowels = new List<WeightedLetter>();
Consonants = new List<WeightedLetter>();
Vowels.Add(new WeightedLetter() { Letter = 'a', Weight = 81 });
Vowels.Add(new WeightedLetter() { Letter = 'e', Weight = 127 });
Vowels.Add(new WeightedLetter() { Letter = 'i', Weight = 70 });
Vowels.Add(new WeightedLetter() { Letter = 'o', Weight = 75 });
Vowels.Add(new WeightedLetter() { Letter = 'u', Weight = 28 });
Consonants.Add(new WeightedLetter() { Letter = 'b', Weight = 15 });
Consonants.Add(new WeightedLetter() { Letter = 'c', Weight = 28 });
Consonants.Add(new WeightedLetter() { Letter = 'd', Weight = 43 });
Consonants.Add(new WeightedLetter() { Letter = 'f', Weight = 23 });
Consonants.Add(new WeightedLetter() { Letter = 'g', Weight = 20 });
Consonants.Add(new WeightedLetter() { Letter = 'h', Weight = 60 });
Consonants.Add(new WeightedLetter() { Letter = 'j', Weight = 2 });
Consonants.Add(new WeightedLetter() { Letter = 'k', Weight = 7 });
Consonants.Add(new WeightedLetter() { Letter = 'l', Weight = 40 });
Consonants.Add(new WeightedLetter() { Letter = 'm', Weight = 25 });
Consonants.Add(new WeightedLetter() { Letter = 'n', Weight = 67 });
Consonants.Add(new WeightedLetter() { Letter = 'p', Weight = 19 });
Consonants.Add(new WeightedLetter() { Letter = 'q', Weight = 1 });
Consonants.Add(new WeightedLetter() { Letter = 'r', Weight = 60 });
Consonants.Add(new WeightedLetter() { Letter = 's', Weight = 63 });
Consonants.Add(new WeightedLetter() { Letter = 't', Weight = 90 });
Consonants.Add(new WeightedLetter() { Letter = 'v', Weight = 10 });
Consonants.Add(new WeightedLetter() { Letter = 'w', Weight = 24 });
Consonants.Add(new WeightedLetter() { Letter = 'x', Weight = 2 });
Consonants.Add(new WeightedLetter() { Letter = 'y', Weight = 20 });
Consonants.Add(new WeightedLetter() { Letter = 'z', Weight = 1 });
}


Choosing parts:



private static string GeneratePart()
{
int PartTypeChoose = GeneratorData.Instance.Randomizer.Next(8);
if (PartTypeChoose == 0) //less single vovels
//v
{
return  GeneratorData.Instance.GetWeightedLetter(GeneratorData.Instance.Vowels);
}
else if (PartTypeChoose < 4) //3 out of 8 seems good
//cv
return GeneratorData.Instance.GetWeightedLetter(GeneratorData.Instance.Consonants) + 
                                            GeneratorData.Instance.GetWeightedLetter(GeneratorData.Instance.Vowels);
else 
//cvc
return GeneratorData.Instance.GetWeightedLetter(GeneratorData.Instance.Consonants) + 
                                            GeneratorData.Instance.GetWeightedLetter(GeneratorData.Instance.Vowels) + 
                                            GeneratorData.Instance.GetWeightedLetter(GeneratorData.Instance.Consonants);
}

Creating some text returns this:

hix ridholo nilroclel ananlu lishavho sehehef casho tiba tendutose senoset tevsehro atota rignu getil hamoko votrof riheu deta oa reue sorfartim rocdino atehhi sesraw mit etos wereh ret teqe moletoh tata e hecladi dadug lepxu dehdon hey tifoi usencaw bi hepnis ita hemeleyna nohni wahi coyitif xor nahtan sohaa wenora no sateto tapef larnemacag tiyco cagsa yer husra ta fa

Isn't it beautiful?

June 24, 2012

Windows Phone 8, will it finally make it?

I have been waiting for the version 8 of my favorite mobile OS and now when they showed it my feelings are a bit mixed. The main thing that stirs up my feelings is that you cannot upgrade a 7.5 telephone to Windows 8.
I didn't expect this from Microsoft. The early rumors where hinting that you would be able to upgrade your Lumia to the new OS. On the other hand, with a bit retrospective and with the talk of using parts of the Windows 8 kernel in the phones, I should have guessed.

This will on the other hand give me an edge in the ever ongoing "who has the coolest phone in the office"-competition, their Lumias will only make it to 7.8...

Every cloud has a silver lining...

So for the truly good news?

Version 8 finally gets a chance to compete with other smartphones when you just check the hardware specs.

It gets the multi-core processor, high resolution screens, true multitasking, integrated skype, offline maps.  The best and in my point of view, a critical must for the success of Windows on the phone is that it will share code with Windows 8. In effect this will mean that Microsoft is choosing one road for all its devices. It is Windows 8 from phone to Server. This also gives me confidence that Microsoft will not to as they do with Windows Phone 8 again, namely cut off their existing user base.
This seems like a long term strategic choice and even though it has a slightly bitter aftertaste it still is needed in the long run.

So will there be a long run?

I still believe that Windows 8 will make it. It simply has features that makes it so much more powerful than the iPad or the Android tablets. It is a real computer. And I believe that Windows 8 will be the beast that will pull the Windows phone.

I hope so.

Windows Phone 8 is just too sexy to disappear. Just look at those nice square things on the screen!


June 18, 2012

Philosophy, technology, religion

Sometimes programmers are strange, actually most of the time programmers are strange.

Me included.

And maybe this is just my twisted and over-simplified view on the subject, but I see a clear line between the developer with a philosophical view on the subject and those with a more technological take on it.

It is a division that can be seen all the way from the universities split in educations for system sciences and those for software engineers.

Philosophers seeks the best way to split reality into parts, engineers tries to make the best parts to resemble the world. Top-down and bottom-up. People that are specialized in abstracting and those that are experts in coding.



Both sides are needed, but problem's arise when coders forget that that is a fact and adopt a one way minded view of the subject, often with an almost religous conviction.

Give programmers a buzzword and they will follow.

The technological religions seems to dominate right now. Most of the new methods and patterns results in a bottom up approach to development. An alluring belief that following a path can create the perfect system.

There are definitely religious philosophers too, an architect colleague to me once said that "when you construct a system, the most important is to be as flexible as possible and you achieve maximum flexibility by not constructing anything.".

The scary part was that he meant it.

The good thing, that he eventually got fired.

I went to a (for me too short) seminar with Chris Klug about the problems with the Test Driven Development (TDD) paradigm and got inspired.

TDD has a religious following in that the users seem to believe that every project gets better if you follow it strictly, the code gets better and the solution gets better. Use tests everywhere and you will see the light.

Lately, I have heard the same opinions about a lot of technologies and methods and if I look back I have heard the same statements before about older technologies. Some people seem to believe that the solution of all problems in the world lies in a three letter acronym.

CQRS, TDD, Scrum, ORM, Functional programming, Object orientation, SOA, Sql, NoSql, Open Source...

There are grains and sometimes tons of truth in all methods, but in my world they are just pieces in the big jigsaw puzzle. Use it, but don't be a Taliban.

Each tool has its place and a time when it should be forgotten.

The goal is to make a good system not to use a certain method or technology.

No method will do the work for you.

Amen...