NHibernate Forge
The official new home for the NHibernate for .NET community

Prefent NHibernate from saving default relations

Wiki Page Hierarchy

Pages

Page Details

First published by:
dnperfors
on 07-01-2009
Last revision by:
dnperfors
on 03-30-2011
This page has not yet been rated
Article
Comments (3)
History (3)

Prefent NHibernate from saving default relations

Let's take a look at the following code:

public class TaxCalculator

{

    public static readonly TaxCalculator NoTax = new TaxCalculator("No Tax", 0.0);

    public TaxCalculator()

    {

    }

 

    public TaxCalculator(String name, Double percentage)

    {

        this.Name = name;

        this.Percentage = percentage;

    }

 

    public Decimal CalculateTax(Decimal value)

    {

        return value * pecentage;

    }

    public Guid Name { get; set; }

    public String Name { get; set; }

    public Decimal Percentage { get; set; }

}

public class Invoice
{

 public Invoice()

  : base(TaxCalculator.NoTax)

{
}


 public Invoice(TaxCalculator calculator)
 {
     this.calculator = calculator;
 }

 public TaxCalculator Calculator { get; set; }
 public string Description { get; set; }
 public decimal Total { get; set; }

 public decimal Tax
 {
     get { return Calculator.CalculateTax(Total); }
 }
}

In this example I want to be able to save a reference to the TaxCalculator, using the following mapping:

<class name="Invoice">
 <id type="guid">
     <generator class="guid"/>
 </id>
 <property name="Description"/>
 <property name="Total"/>
 <reference name="Calculator" />
</class>

<class name="TaxCalculator">
 <id name="Id" type="guid">
     <generator class="guid"/>
 </id>
 <property name="Name"/>
 <property name="Percentage"/>
</class>

 

I assume that TaxCalculators are created before I save the invoice (that is how we do it at the moment). The problem we are getting into is that we can't save a reference to TaxCalculator.NoTax. This is because the NoTax object will be created over and over again and so it will not have any Id. 

The solution could be to set the Calculator property to null, but then we have to check for a null reference. But luckily it is also possible to let NHibernate set this property on null when persisting the object and set it to NoTax when loading the invoice. This is done by using Events:

public class FlushEvent : DefaultFlushEntityEventListener

{

    public override void OnFlushEntity(FlushEntityEvent @event)

    {

        if (@event.Entity is Invoice && ((Invoice)@event.Entity).Calculator == TaxCalculator.NoTax)

            ((Invoice)@event.Entity).Calculator = null;

        base.OnFlushEntity(@event);

    }

}

public class LoadedEvent : DefaultPostLoadEventListener, IRepositoryEvent

{

    public override void OnPostLoad(PostLoadEvent @event)

    {

        base.OnPostLoad(@event);

        if (@event.Entity is Invoice && ((Invoice)@event.Entity).Calculator == null)

            ((Invoice)@event.Entity).Calculator = TaxCalculator.NoTax;

    }

}

 

Recent Comments

By: store Posted on 01-06-2012 3:17

Louis Vuitton Outlet offers not only the fashion

louis vuitton uk www.louisvuitton-ukoutlet.org.uk

, but also many other LV goods. Welcome to visit the LV online store.If you want to catch up to the latest vogue, having

louis vuitton www.louisvuitton-ukoutlet.org.uk

Handbags of the latest styles can absolutely satisfy you.Just visit LV outlet webpage and select the most suitable products for yourself.

By: store Posted on 01-06-2012 3:17

In the

coach outlet online www.coachoutletonlinelove.com

shop, a collection of high quality and hottest Coach Shoulder Bags is offered to you. They are all at the best prices you will ever find.With the safe door to door shipping, the

coach outlet www.coachoutletonlinelove.com

will send the products to your hands, which are of top quality and at competitive factory prices.

By: xiha37 Posted on 01-12-2011 23:40

some times we rather want to abstract the data manipulation interface from a specific infrastructure implementation .<a href="http://www.jihoy.com">free advertising</a> |<a href="www.jihoy.com/.../a>|<a href="http://www.simplyrest.com">sleep number bed</a>

View All
Powered by Community Server (Commercial Edition), by Telligent Systems