Ayende @ Rahien

It's a girl

Reflections on the Naked CLR

I am teaching beginners right now, and we started out from:

using (SqlConnection connection = new SqlConnection(Settings.Default.Database))

{

 connection.Open();

 using (SqlTransaction tx = connection.BeginTransaction())

 {

     using (SqlCommand command = connection.CreateCommand())

     {

         command.CommandText = "SELECT COUNT(*) FROM Books;";

         result = (int)command.ExecuteScalar();

     }

     tx.Commit();

 }

}

But they said that they already know this stuff and I am boring, so we moved to this:

return With.Transaction<int>(delegate(SqlCommand command)

  {

      command.CommandType = System.Data.CommandType.Text;

      command.CommandText = "SELECT COUNT(*) FROM Books;";

      return (int)command.ExecuteScalar();

  });

Where the implementation is:

 

    public static void Transaction(Proc exec)

    {

        using (SqlConnection connection = new SqlConnection(Settings.Default.Database))

        {

            connection.Open();

            SqlTransaction tx = connection.BeginTransaction();

            try

            {

                using (SqlCommand command = connection.CreateCommand())

                {

                    command.Transaction = tx;

                    exec(command);

                }

                tx.Commit();

            }

            catch

            {

                tx.Rollback();

                throw;

            }

            finally

            {

                tx.Dispose();

            }

        }

    }

}

They didn't think it was boring any longer :-)

That made for some interesting diagrams, just to show the flow of the code. The piece of code above covers a lot of topics, next step is to introduce Unit of Work.

They are beginners, so they need to understand the basics well before they can do anything with frameworks, but I just find it so frustrating to work on the naked CLR. It is like a construction worker that can arrive on a building site with an anvil and a hammer, and then need to build all the tools before they can start working.

Comments

Eber Irigoyen
05/26/2007 11:43 PM by
Eber Irigoyen

now you are teacher too??

dude

connor peterson
05/27/2007 01:01 AM by
connor peterson

What/where is the "With" in With.Transaction?

Bil Simser
05/27/2007 01:13 AM by
Bil Simser

Naked CLR is boring, but once the light bulb turns on and they "get it" you'll see a huge difference. It's the same when I'm mentoring guys on refactoring. I make them (and myself) go through the pain and suffering of doing refactorings manually so that they know why they're doing them. Then I show them ReSharper. Then I introduce them to the keyboard and live templates. Then their heads explode. It's all a very natural progression.

Ayende Rahien
05/27/2007 06:25 AM by
Ayende Rahien

Connor,

That is the class name for the Transaction method, just a convention that I use for this type of stuff.

Ken Egozi
05/27/2007 08:45 AM by
Ken Egozi

@Bil:

I guess I still have a head explosion ahead of me, since I'm not using ReSharper yet (it slowed my machine too much, and I use rather a strogng enough machine, 3.0HT+2GB), and my laptop is a poor 1.7+756MB so no ReSharper will fit in currently :(

@Ayende:

Is this "teaching" a part of mentoring new members, or is it a paid activity (thus, you have clients who pay you to mentor their employees? I guess those are clients worth working for )

Chris Khoo
05/27/2007 01:44 PM by
Chris Khoo

Hi man

Even though you're not a native English speaker, I find your blog to be one of the most enjoyable & interesting to keep up with, even if you post like 5 times a day :-).

I hope a major publisher approaches you to write a book on enterprise architecture one day - that is if you want to.

Chris

Gunnlaugur Briem
05/28/2007 03:06 AM by
Gunnlaugur Briem

Slight bogosity in that Transaction method. It's missing the type parameter and it's void, the exec return value is discarded. Posting a little too fast? :)

Ayende Rahien
05/28/2007 04:35 AM by
Ayende Rahien

No, that is simply missing the rest of it.

Transaction(Func exec)

{

T result = null;

Transaction(delegate(IDbCommand command)

{

   result = exec(command);

});

return result;

}

Gian Maria
05/28/2007 10:07 AM by
Gian Maria

Very interesting stuff

Alk.

Ayende Rahien
05/28/2007 07:28 PM by
Ayende Rahien

Foobar,

That is a term invented by yours truly to refer to the CLR without any additional frameworks, basically, just the CLR Runtime Redistributable, and building up from there.

foobar
05/28/2007 09:08 PM by
foobar

[Oren]

That's what I figured after I'd read two of your earlier scathing posts.

Thank you for confirming it.

Ayende Rahien
05/29/2007 11:31 AM by
Ayende Rahien

Beginner, here is is, in its naked glory:

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using BookStore.Properties;

namespace BookStore.Util

{

public static class With

{

    public delegate T Func<T>(SqlCommand command);

    public delegate void Proc(SqlCommand command);


    public static T Transaction<T>(Func<T> exec)

    {

        T result = default(T);

        Transaction(delegate(SqlCommand command)

        {

            result = exec(command);

        });

        return result;

    }


    public static void Transaction(Proc exec)

    {

        using (SqlConnection connection = new SqlConnection(Settings.Default.Database))

        {

            connection.Open();

            SqlTransaction tx = connection.BeginTransaction();

            try

            {

                using (SqlCommand command = connection.CreateCommand())

                {

                    command.Transaction = tx;

                    exec(command);

                }

                tx.Commit();

            }

            catch

            {

                tx.Rollback();

                throw;

            }

            finally

            {

                tx.Dispose();

            }

        }

    }


}

}

Comments have been closed on this topic.