2012-03-13 120 views
0

这个问题要么是由于我的项目设计不佳,我缺乏关于如何使用nhibernate的知识,或者两者兼而有之(只是说明,我为我的糟糕的项目设计指责nhibernate)。那就是:Nhibernate映射/设计问题

在我的域模型,我有以下的(我剪了很多多余的让事情变得简单的):

的Event对象:

public class EventBO 
{ 

    public int ID { get; set; } 

    public String Name 
    { 
     get; 
     set; 
    } 

    private List<InviteeBO> _invitees = null; 

    public List<InviteeBO> Invitees 
    { 
     get { return _invitees; } 
     set { _invitees = value; } 
    } 
} 

而一个被邀请对象:

public class InviteeBO 
{ 

    public int ID { get; set; } 

    public String Name 
    { 
     get; 
     set; 
    } 

    private List<InviteeBO> _invitees = null; 

    public List<InviteeBO> Invitees 
    { 
     get { return _invitees; } 
     set { _invitees = value; } 
    } 

    private EventBO theEvent; 

    public EventBO TheEvent 
    { 
     get { return theEvent; } 
     set { theEvent = value; } 
    } 

    private InviteeBO invitedByUser = null; 

    public InviteeBO InvitedByUser 
    { 
     get { return invitedByUser; } 
     set { invitedByUser = value; } 
    } 
} 

创建事件时,创建事件的人自动添加到Event.Invitees列表中。然后,同一个人可以添加无限量的被邀请者,然后将其添加到刚刚添加到事件的受邀者对象中的受邀者列表。原因是因为我需要不仅追踪谁加入了赛事,而且还追踪谁加入了谁。

我的问题是这样的:当我保存一个事件时,事件的被邀请者列表中的每个被邀请者(在被邀请者数据库表中),EventID被添加到数据库的相应列中。此外,如果该Event.Invitees列表中的Invitee邀请好友(并因此将邀请者添加到其自己的Invitee.Invitees列表中),则保存事件将正确地写入邀请对象,邀请者邀请他邀请的所有被邀请者也在Invitee数据库表中)。问题:EventID未被添加到Event.Invitees列表中的所有被邀请者的被邀请者。

上帝......我正在做一个可怕的工作来解释这个,我很抱歉。我可以在这里提供无限的细节,但我不想超载这篇文章。请告诉我你是否需要其他任何东西来帮助我解决这个问题。

无论如何,这里的事件和被邀请者映射文件的相关部分:

Event.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="DAL" 
        namespace="DAL.DAO.Entities"> 
    <class name="EventDO" table="Events"> 
    <id name="ID"> 
     <column name="ID"/> 
     <generator class="native" /> 
    </id> 
    <property name="Name"> 
     <column name="Name"/> 
    </property> 
    <property name="Description"> 
     <column name="Description"/> 
    </property>  
    <bag name="Invitees" table="Invitees" inverse="false" cascade="all" lazy="false" > 
     <key column="EventID"/> 
     <one-to-many class="InviteeDO" /> 
    </bag> 

    </class> 
</hibernate-mapping> 

Invitee.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DAL" namespace="DAL.DAO.Entities" > 
    <class name="InviteeDO" table="Invitees"> 
    <id name="ID"> 
     <generator class="identity"/> 
    </id> 
     <property name="FullName"/> 
     <many-to-one name="TheEvent" cascade="none" column="EventID" lazy="false" /> 
     <bag name="Contacts" table="Contacts" inverse="false" cascade="all" lazy="false" > 
     <key column="ContactOfUserID"/> 
     <one-to-many class="ContactDO" /> 
     </bag> 
     <bag name="Invitees" table="Invitees" inverse="false" cascade="save-update" lazy="false" > 
     <key column="InvitedByID" /> 
     <one-to-many class="InviteeDO"/> 
     </bag> 
     <property name="IsOrganizer"/> 
     <property name="IsCreator"/> 


    </class> 
</hibernate-mapping> 

更新 @Roberto埃尔南德斯 - 首先,感谢您的回应。我真的很感激它。

其次,您只需使用我的DAL和DOs进行测试,然后运行。所以很显然,将我的DOs转换为BOs的功能出了问题。

还有一些我做错了,你指出。我已将子受邀者添加到父邀请对象,并将父邀请对象添加到事件邀请对象。然后我推测,保存事件将执行所有必要的持续性,包括对于被邀请的子女。很明显,我错了,我还需要将parentInvitee添加到该子项,并将该子项添加到Event.Invitees。谢谢!

+0

很高兴听到一切正常! – 2012-03-13 18:15:08

回答

1

我不认为上面列出的代码有什么特别的错误,我的意思是有一些可能的改进,但我认为你主要假设NHibernate会做更多,目前正在为你做。我使用你的代码和映射构建了一个应用程序,并且只要我明确设置了上面提到的属性,一切都按预期工作。以下是摘录。

// This is my SessionFactory (Not Relevant!) 
using (var session = NhSessionFactory.GetSession(generatedatabase: false)) 
{ 
using (var tran = session.BeginTransaction()) 
{ 
    var theEvent = new EventBO(); 
    theEvent.Name = "EventName"; 

    var parentInvitee = new InviteeBO(); 
    parentInvitee.Name = "Parent"; 
    theEvent.Invitees.Add(parentInvitee); 

    for (int index = 0; index <= 5; index++) 
    { 
     var childInvitee = new InviteeBO(); 
     childInvitee.Name = string.Format("Child {0}", index); 
     childInvitee.InvitedByUser = parentInvitee; // Adding invited by user. 

     parentInvitee.Invitees.Add(childInvitee); 
     theEvent.Invitees.Add(childInvitee); // Adding child invitees to Event. 
    } 

    session.SaveOrUpdate(theEvent); 
    tran.Commit(); 
} 
} 

虽然上述工程的代码,我会建议重构到您的BO对象这一切。例如,将AddInvitee方法添加到处理子对象中属性设置的Event类。

警告:你似乎有你是不是向我们展示的是处理从BO映射到我回答我可以在最好的办法不知道你有什么在这层映射层。