类设计和NHibernate映射问题

时间:2022-09-15 16:43:00

I have an Address class that I want to use for other objects address info. Below I paste 2 example classes Company and Person that use Address class

我有一个Address类,我想用于其他对象的地址信息。下面我粘贴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
}

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

现在我的挑战是如何将这些映射到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>

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!

如果你看到一个架构问题,请告诉我,因为我之前没有使用过单独的地址表,我还不太满意。谢谢!

2 个解决方案

#1


You need the Company and Person classes to implement a common interface. 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. See Ayende's post 'NHibernate Mapping - <any/>' on the subject.

您需要Company和Person类来实现通用接口。在地址中添加一个字段,该字段将跟踪该行是属于公司还是人员。然后使用地址映射中的 标记。请参阅Ayende的帖子'NHibernate Mapping - '。

#2


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>

#1


You need the Company and Person classes to implement a common interface. 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. See Ayende's post 'NHibernate Mapping - <any/>' on the subject.

您需要Company和Person类来实现通用接口。在地址中添加一个字段,该字段将跟踪该行是属于公司还是人员。然后使用地址映射中的 标记。请参阅Ayende的帖子'NHibernate Mapping - '。

#2


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>