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.

Print | posted on Sunday, May 27, 2007 1:37 AM

Feedback


Gravatar

# re: Reflections on the Naked CLR 5/27/2007 2:43 AM Eber Irigoyen

now you are teacher too??

dude


Gravatar

# re: Reflections on the Naked CLR 5/27/2007 4:01 AM connor peterson

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


Gravatar

# re: Reflections on the Naked CLR 5/27/2007 4:13 AM 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.


Gravatar

# re: Reflections on the Naked CLR 5/27/2007 9:25 AM Ayende Rahien

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


Gravatar

# re: Reflections on the Naked CLR 5/27/2007 11:45 AM 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 )


Gravatar

# re: Reflections on the Naked CLR 5/27/2007 4:44 PM 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


Gravatar

# re: Reflections on the Naked CLR 5/28/2007 6:06 AM 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? :)


Gravatar

# re: Reflections on the Naked CLR 5/28/2007 7:35 AM Ayende Rahien

No, that is simply missing the rest of it.
Transaction<T>(Func<T> exec)
{
T result = null;
Transaction(delegate(IDbCommand command)
{
result = exec(command);
});
return result;
}


Gravatar

# re: Reflections on the Naked CLR 5/28/2007 1:07 PM Gian Maria

Very interesting stuff

Alk.


Gravatar

# re: Reflections on the Naked CLR 5/28/2007 10:28 PM 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.


Gravatar

# re: Reflections on the Naked CLR 5/29/2007 12:08 AM foobar

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

Thank you for confirming it.


Gravatar

# re: Reflections on the Naked CLR 5/29/2007 2:31 PM 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.