Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,128 | Comments: 45,549

filter by tags archive

NHibernate – Get Thou Out Of My Database – 2nd Edition

time to read 3 min | 543 words

Following up on my previous post, the customer has complained about table names like [tbl_-1434067361], apparently they felt that this was misusing their naming policy. I told them that while I understood that, it did meet their naming policy. I got a new naming policy that stated that numbers are not allowed in column or table name, (and showing forethought) that table names must be composed of valid English words.

I, of course, decided that if this is what they wanted, they will get just that. And created this:


The words.txt file was taken from this URL: http://www.puzzlers.org/pub/wordlists/pocket.txt

The result is exactly per their specification:

create table [tbl_colonel] (
  [col_verbiage] INT IDENTITY NOT NULL,
   [col_unsparing] NVARCHAR(255) null,
   [col_indomitable] NVARCHAR(255) null,
   [col_bulldog] BIT null,
   [col_thank] DATETIME null,
   primary key ([col_verbiage])
create table [tbl_stump] (
  [col_upheaval] INT IDENTITY NOT NULL,
   [col_promissory] NVARCHAR(255) null,
   [col_predecessor] NVARCHAR(255) null,
   [col_chafer] NVARCHAR(255) null,
   [col_unyoke] INT null,
   [col_vise] NVARCHAR(255) null,
   PostId INT null,
   primary key ([col_upheaval])

create table [tbl_reprieve] (
  [col_wherewith] INT IDENTITY NOT NULL,
   [col_wolf] VARBINARY(8000) null,
   [col_legendary] NVARCHAR(255) null,
   [col_ago] NVARCHAR(255) null,
   [col_carabineer] DATETIME null,
   [col_referee] NVARCHAR(255) null,
   primary key ([col_wherewith])

The fun part about this approach is that you still get great level of security, while maintaining the naming convention. Even more than that, you get queries like:

SELECT col_verbiage, col_indomitable, col_bulldog, col_wolf, col_legendary, col_referee
FROM tbl_colonel JOIN tbl_repreieve ON tbl_repreieve.tbl_referee = tbl_colonel.col.unsparing
WHERE col_ago = 'Fun@house.at'

It is obvious that we are getting users & blogs with specific email, right?

This is actually much harder than just numeric values, because this is going to really mess with your mind.

Nitpicker corner: Yes, this is a humorous post, don’t take it seriously, and please don’t really do it to unsuspecting customers / DBAs. Unleash it on silly integration teams only.



I once suggested to my DBA team that all table names should be First Names of People, like 'Steve' and 'Lisa' because then you can write SQL like "Insert into <marginally".

The ones that laughed were the good ones.


Ayende, this great technique should make it into next edition of 'Teach Yourself Application Architecture in a Weekend'

matt hinze

3rd requirement, the names of tables must be the names of entities in the system:

foreach (var clazz in configuration.ClassMappings)

clazz.Table.Name = getRandomFromExistingTableNames();


No no no, this just won't work. You are bound to have collisions here. You should use this function to avoid collisions:

public string GetName(string prefix, uint code) {

StringBuilder result = new StringBuilder(prefix);

while (code > words.Length) {


    result.Append(words[code % words.Length);

    code /= words.Length;



result.Append(words[code % words.Length]);


Tobin Harris

Hahahaha. That's awesome.


Security by obscurity is ever a good solution. And in this case it would only make your work harder when the shit hit the fence. SQL Performance analyze would become a real bitch.

Preventing people to run queries is a solution. That's where our nhibernate repository comes in. It's basically a facade around the ISession interface, but it prevents submitting a raw query. Only nhibernate objects or an ICriteria/IQuery instance is allowed.

Code should be as self describing as possible.


Sorry, make that 'never a good solution.'

Dan F

Hahahahaha, nice!

Ian Nelson

Who knew NH could double-up as a poetry generator?!

Harry S.


Maybe you should check out the Nitpicker Corner at the end of the post...

Comment preview

Comments have been closed on this topic.


  1. The worker pattern - 3 days from now

There are posts all the way to May 30, 2016


  1. The design of RavenDB 4.0 (14):
    26 May 2016 - The client side
  2. RavenDB 3.5 whirl wind tour (14):
    25 May 2016 - Got anything to declare, ya smuggler?
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats