NHibernate:将多个列映射到一个集合中 [英] NHibernate: map multiple columns into a single collection

查看:93
本文介绍了NHibernate:将多个列映射到一个集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一张桌子:

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

和.NET类

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

class MyEmail {
    string email;
}

我想没有办法将那些(多个)列映射到NHibernate中的单个集合中,或者存在吗? :)

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.

推荐答案

我建议您使用Interfaces,以便您可以执行类似的操作

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; }
}

您的应用程序可以期望有一个IUser,而不必关心我们在哪里获得了电子邮件列表.您将在NH中映射MyUser,而该应用程序则不会(也不应该)在乎实际的实现.

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.

这篇关于NHibernate:将多个列映射到一个集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆