Second IUserType of the day 8211 Store a list of string in database with NHIbernate
I have an object that has 4 properties of IList<String> type. You can natively map these properties in a separate table with this simple mapping
|
|
Ok, this is a simple mapping that store all the keys into another table with two columns, one is the foreign key to the main object, the other is a string column to store the data. But a similar mapping does not satisfy me, because I need to create 4 more table to store string coming from my 4 IList<String> properties…too bad.
The solution is…again….IUserType. The solution is store the strings in a single column, combining the strings in one with a separator that does not happens to be part of any string, as an example, if the collection contains (“Key1”, “Key2”) I want to be stored as “Key1#Key2”, here is the get part of the usertype
|
|
If the data in database is null, or is an empty string I return an empty List<String>, but if the data is not empty I split the string with the separator, and then add each single string in the list. The set part is similar
|
|
If the value is null I set null value, if not I simply cast the original value as an IEnumerable<String>, that is sufficient for me to enumerate all the string, store them in a StringBuilder, and finally remove the trailing separator and store the string in database. Here is a Test
|
|
This piece of code produces this SQL
|
|
As you can see the Uri is translated into a string and the two keywords are combined into one, in this way I can store all the 4 properties in the same table, having a clearer structure of the database.
alk.
Tags: NHibernate IUserType