Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,949 | Comments: 44,548

filter by tags archive

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

now you are teacher too??

dude

connor peterson

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

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

Connor,

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

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

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

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

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

Very interesting stuff

Alk.

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

[Oren]

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

Thank you for confirming it.

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();

            }

        }

    }


}

}

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
  2. Special Offer (2):
    27 May 2015 - 29% discount for all our products
  3. RavenDB Sharding (3):
    22 May 2015 - Adding a new shard to an existing cluster, splitting the shard
  4. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  5. Interview question (2):
    30 Mar 2015 - fix the index
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats