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
now you are teacher too??
dude
What/where is the "With" in With.Transaction?
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.
Connor,
That is the class name for the Transaction method, just a convention that I use for this type of stuff.
@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 )
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
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? :)
No, that is simply missing the rest of it.
Transaction<T>(Func<T> exec)
{
T result = null;
Transaction(delegate(IDbCommand command)
{
});
return result;
}
Very interesting stuff
Alk.
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.
[Oren]
That's what I figured after I'd read two of your earlier scathing posts.
Thank you for confirming it.
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
{
}
Comment preview