nHibernate根据子属性更改更新Parent对象的属性

时间:2014-02-28 19:17:46

标签: c# nhibernate properties parent

我有一个MVC3 C#.Net网络应用程序。我们使用nHibernate作为我们的ORM。我需要在子属性更改时更新父属性。即,我的Proposal对象具有TotalCost属性。 Proposal有子对象,Task,有成本 如果在我们的应用程序中更新了任务的成本,则需要更新提案的总成本 目前我们正在计算TotalCost。但是,随着提案中任务数量的增加,性能确实会降低 有没有办法在nHibernate中,可能通过映射文件来完成这个?还是其他任何想法? (以下是所涉及的类和nHibernate文件的关键部分)

提案C#类:

namespace Domain 
{
    public class Proposal : DomainBase
    {
        public Proposal() { }

        [DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
        public virtual double TotalCost
        {
            get
            {
                double totalCost = Tasks.Where(a => a != null).
                    Aggregate<BOE, double>(0, (current, a) => current + a.Cost);

                return totalCost;

            }
        }   
     }

}

建议nHibernate映射文件:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Domain"
                   namespace="Domain"
                   default-lazy="true">
  <class name="Proposal" table="dbo.Proposal" >
    <id name="Id" column="Id" type="Guid"
        unsaved-value="00000000-0000-0000-0000-000000000000" >
      <generator class="guid.comb" />
    </id>

    <property name="RowVersion" column="RowVersion" not-null="true" />
    <property name="TotalCost" column="TotalCost" not-null="false" />

    <set name="Tasks"
        cascade="all-delete-orphan"
        inverse="true"
        order-by="WbsNumber asc">
      <key column="Proposal_Id" />
      <one-to-many class="BOE"/>
    </set>

  </class>
</hibernate-mapping>

任务C#类:

namespace Domain
{
    public class BOE : DomainBase
    {
        [DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
        public virtual double Cost { get; set; }

    }
}

任务nHibernate映射文件:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Domain"
                   namespace="Domain"
                   default-lazy="true">
  <class name="Task" table="dbo.Task" >
    <id name="Id" column="Id" type="Guid"
        unsaved-value="00000000-0000-0000-0000-000000000000" >
      <generator class="guid.comb" />
    </id>
    <property name="RowVersion" column="RowVersion" not-null="true" />
    <property name="CostCost" column="Cost" not-null="false" />

    <many-to-one name="Proposal" class="Proposal" column="Proposal_Id" not-null="true"/>


  </class>

</hibernate-mapping>

0 个答案:

没有答案