views:

219

answers:

2

I'm currently building a new app based on a legacy database. In the app, there are many "list" tables that have the following format:

table listLanguages
{
    PrimaryId bigint;
    Item varchar(30);
}

...with some sample entries like this:

PrimaryId Item
1         English
2         French
3         Spanish
...

They were used to populate dropdown lists in web forms that did things like edit client details. When they were saved in a client record, the full string value was used, and not the ID. I have to keep this behaviour.

My question is: what is the best way to map these in my domain model using NHibernate?

I cannot change the structure of the database, and there are many list tables (I'd say about 40).

+1  A: 

I normally use code generation to create C# Enums from the relevant database tables:

public enum Language
{
    NoneSpecified = 0,
    English       = 1,
    French        = 2,
    Spanish       = 3,
    ... 
}

Have a look at T4 template code generation, built into Visual Studio 2005 onwards.

Mitch Wheat
Unfortunately that's not an option as the users are able to add their own items to list via a web interface.
cdmckay
+1  A: 

Here's what i did in a project: I created a base class for these tables and created a child class for each of tables inherited from this base class then I had separate mapping files for each of tables.

Although I think there can be some more complicated solutions like using an IInterceptor to intercept load behavior or adding some mapping files in run time using Configuration class can be some options.

Beatles1692
Yeah I thought about that but that's a shitload of mappings... although I guess the mappings would be very simple.
cdmckay
You are right but that's the way NHibernate works, you should provide mappings for your types :) .
Beatles1692