【问题标题】:Class design and NHibernate mapping question类设计和 NHibernate 映射问题
【发布时间】:2009-07-20 01:30:19
【问题描述】:

我有一个地址类,我想将它用于其他对象的地址信息。下面我粘贴 2 个使用 Address 类的示例类 Company 和 Person

public class Company
{
    public virtual string Name { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Fax { get; set; }

    public virtual Address Address { get; set; }
}

public class Person
{
    public virtual string Name { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Fax { get; set; }

    public virtual Address Address { get; set; }
}

public class Address
{
    public virtual string Address1 { get; set; }
    public virtual string CityName { get; set; }
    public virtual string StateName { get; set; }
    public virtual string CountryName { get; set; }

    public virtual AddressOf AddressOf { get; set; }
    public virtual object Owner { get; set; }
}

public enum AddressOf : byte
{
        Company,
    Person
}

我的计划是有一个地址表,我将保存公司客户的地址等。 每个地址都有其唯一的 ID,并将通过另一个 owner_id 和 owner_type 列与地址的所有者相关联。

我通常的做法是在所有需要地址信息的表上都有地址列(然后我可以为 nhibernate 进行组件映射)但现在我认为这可能没有必要,我可以在一个上收集所有地址公用表..

现在我面临的挑战是如何将这些映射到 nhibernate。我想到了一对一的映射,但不知道如何映射 owner_id 列,该列应该是从所有者/公司、个人等外部生成的。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
    <class name="Company" table="Companiess">
        <id name="ID" column="ID" type="Int32" unsaved-value="0">
            <generator class="native" />
        </id>

        <property name="Name" />
        <property name="Phone" />
        <property name="Fax" />

        <one-to-one name="Address" class="Address"  />

    </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="Addresses" table="Addresses   ">
        <id name="ID" column="ID" type="Int32" unsaved-value="0">
            <generator class="native" />
        </id>


        <property name="Address1" />
        <property name="CityName" />
        <property name="StateName" />
        <property name="CountryName" />

        <one-to-one "here is where im having trouble, I need to map owners id to owner_id column of Addresses table" ></one-to-one>


    </class>
</hibernate-mapping>

如果您发现架构问题,也请告诉我,因为我以前没有以这种方式使用过单独的地址表,而且我对此还不是很满意。 谢谢!

【问题讨论】:

    标签: c# nhibernate nhibernate-mapping


    【解决方案1】:

    您需要 Company 和 Person 类来实现一个通用接口。在地址中添加一个字段,该字段将跟踪该行是属于公司还是个人。然后在地址映射中使用&lt;any&gt; 标记。有关该主题,请参阅 Ayende's post 'NHibernate Mapping - <any/>'

    【讨论】:

    • 这种方法让我考虑将地址字段保留在同一个表上而不是单独的表上,因为看起来这会使事情复杂化太多..(如果没有更好的解决方案,我会将其标记为答案这个问题)
    【解决方案2】:

    我不确定您为什么需要从地址到所有者的双向链接;那真的有必要吗?地址真的必须知道它归谁所有吗?

    您可能想要考虑删除该关系并提出一个通用的“共享”关系类,如下所示:

    public class Relation
    {
      public virtual string Name { get; set; }
      public virtual string Phone { get; set; 
      public virtual string Fax { get; set; }
      public virtual Address Address { get; set; }
    }
    

    还有……

    public class Person : Relation
    {
    }
    public class Company : Relation
    {
    }
    

    还有映射文件:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
        <class name="Relation" table="Relations">
            <id name="ID" column="ID" type="Int32" unsaved-value="0">
                <generator class="native" />
            </id>
    
            <property name="Name" />
            <property name="Phone" />
            <property name="Fax" />
            <Component name="Address" class="Address" >
            ... address fields...
            </Component>
    
            <joined subclass="Person..." />
    
            <joined subclass="Company..." />
    
    
        </class>
    </hibernate-mapping>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多