views:

275

answers:

2

In my database I have field containg a comma delimited list of emails. How would I map that to a IList<string> in my model ?

A: 

The table in question is not even First Normal Form, which is bad.

The only way you can possibly do that is something along these lines:

class Foo
{  
 private List<string> emails = new List<string>();  

 public string _Emails 
 { 
  get { return string.Join(",", emails.ToArray()); }
  set { emails = new List<string>(value.Split(',')); }
 }  

 public IList<string> Emails
 { 
  get { return emails; }    
 }
}

and map _Emails property.

Edit

One more solution is implemention your own IUserType or IUserCollection. Thus your model will be much prettier.

Anton Gogolev
Well, sometimes you are not responsible for designing the database.Your solution will work with some tweaking (the client must be able to add addresses to the Emails field as well), but I was hoping for some kind of user defined mapping. Im not very keen on polluting my model :)
BjartN
+2  A: 

You should implement an IUserCollection, which would map your CSV column to an actual list of emails, then serialize it back on save.

James Gregory

related questions