简体   繁体   English

类设计和NHibernate映射问题

[英]Class design and NHibernate mapping question

I have an Address class that I want to use for other objects address info. 我有一个Address类,我想将其用于其他对象的地址信息。 Below I paste 2 example classes Company and Person that use Address class 下面我粘贴使用Address类的2个示例类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
}

My plan is to have an addresses table that I will keep addresses of companies customers etc. Each address will have its unique id and will associate to the owner of the address via another owner_id and owner_type columns. 我的计划是有一个地址表,该表将保留公司客户的地址等。每个地址将具有其唯一的ID,并将通过另一个owner_id和owner_type列与该地址的所有者相关联。

The way I normally did was I would have address columns on all tables that needed address information(then I could do component mapping for nhibernate) but now I'm thinking that may not be necessary and I can collect all addresses on one common table.. 我通常的做法是,在所有需要地址信息的表上都有地址列(然后我可以为nhibernate进行组件映射),但是现在我想可能没有必要,我可以在一个公用表上收集所有地址。 。

Now my challenge is how I map these to nhibernate. 现在我面临的挑战是如何将它们映射到nhibernate。 I thought of one-to-one mapping but could not figure how I will map owner_id column which should be foreignly generated from the owner / company, person etc. 我想到了一对一的映射,但无法确定如何映射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>

If you are seeing an architectural problem please also let me know as I have not used a separate address table this way before and I'm not yet entirely comfortable about it. 如果您遇到体系结构问题,也请让我知道,因为我以前没有以这种方式使用过单独的地址表,而且我对此还不太满意。 Thanks! 谢谢!

You need the Company and Person classes to implement a common interface. 您需要Company和Person类来实现公共接口。 Add a field in Address that will track if the row belongs to a Company or a Person. 在地址中添加一个字段,该字段将跟踪该行是公司还是个人。 Then use the <any> tag in the Address mapping. 然后在地址映射中使用<any>标记。 See Ayende's post 'NHibernate Mapping - <any/>' on the subject. 有关该主题,请参见Ayende的文章“ NHibernate映射-<any />”

I'm not sure why you want the bi-directional link from Address to the owner; 我不确定为什么要从地址到所有者的双向链接; would that really be necessary? 真的有必要吗? Does an address really have to know who it is owned by? 地址真的必须知道谁归谁所有吗?

You might want to consider dropping that relation and come up with a generic 'shared' relation class, like this: 您可能要考虑删除该关系,并提出一个通用的“共享”关系类,如下所示:

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

and... 和...

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

And the mapping file: 和映射文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM