views:

35

answers:

3

Suppose I have a table:

 ID(pk) | HOME_EMAIL | WORK_EMAIL | OTHER_EMAIL
-------------------------------------------------

and the .NET classes

class A {
    int id;
    List<MyEmail> emails;
}

class MyEmail {
    string email;
}

I suppose there's no way to map those (multiple) columns into a single collection in NHibernate, or is there? :)

It's come to a point that we'd rather not tinker with the database schema anymore so we can't do much with the database, if that helps.

+2  A: 

I would suggest working with Interfaces so you could do something like this

 interface IUser
{
    int Id {get; set;}
    IEnumerable<string> Emails {get;}
}

class MyUser : IUser
{
    public int Id {get; set;}
    public IEnumerable<string> Emails
    {
        get
        {
            return new [] { SomeEmail, SomeOtherEmail };
        }
    }

    public string SomeEmail { get; set; }
    public string SomeOtherEmail { get; set; }
}

Your application can expect an IUser and not care where we got the list of emails. You would map MyUser in NH, while the application does not (and should not) care about the actual implementation.

Christopherous 5000
+1 for the encapsulation heads up. this one looks more like our previous implementation PLUS this new one thrown in together.I'll see what I can do with this.
Richard Neil Ilagan
+1  A: 

If it doesn't have to be a collection, but could be a custom type instead, say EmailAddresses which contains three properties:

public class EmailAddresses
{
    public virtual string Home { get; set; }
    public virtual string Work { get; set; }
    public virtual string Other { get; set; }
}

You could use a component to map the three columns into the three properties of this object as a single property on the parent:

public class MyUser
{
    ...
    public virtual EmailAddresses { get; set; }
}

You can map these in NHibernate using components or if you're using Fluent NHibernate with the ComponentMap<T> classmap (automapper can't do components).

Michael Shimmins
yeah, this was the first implementation actually, right before some tweaking, which left us with the example above. :) i'm a bit partial to this structure, but if there's a way to do it without wringing another architecture change, I think our developers would be moving towards that. :)
Richard Neil Ilagan
A: 

There is a feature that's very close to what you want, <dynamic-component>

The documentation at http://nhforge.org/doc/nh/en/index.html#components-dynamic should get you started.

Diego Mijelshon
yup, Dictionaries would make for a more logical mapping of a database schema (at least over a skeletal List) just for the fact that they're key'ed. I'll look into this as well, thanks!
Richard Neil Ilagan