byouness
byouness

Reputation: 1782

Fluent NHibernate mapping: QuantLib.Date and Mysql's Date

I am using the QuantLib's Date class in a C# project, and this causes some problems when trying to map my Price class to my price table in a mySql database.

The price table has three columns: [id], [date] and [value] and the code looks like this :

//ENTITY
    public class Price{
        public virtual long id { get; protected set; }
        public virtual QuantLib.Date date { get; set; }
        public virtual double value{ get; set; }
    }

//MAPPING
    public PriceMap() {
        Table("price");
        Id(x => x.id, "id");
        Map(x => x.date, "date") // to be completed... ;
        Map(x => x.value, "value");
    }

Is there a way to specify to NHibernate how to map this class to MySQL's Date type, using Fluent NHibernate? Sort of casting it before saving in or retrienving from the db, without having to change the entity's structure.

Upvotes: 0

Views: 221

Answers (1)

MichaC
MichaC

Reputation: 13380

You have two options

  1. Create a custom type for nhibernate and specify it in the mapping as .CustomType. Therefore you'll have to implement a small interface which does the conversion to and from QuantLib.Date

  2. You simply change the type of the date property to System.DateTime and add another property which read/writes to the date property but uses your QuantLib.Date, pseudocode:

    public QuantLib.Date QDate {
       get{
         // read from this.date and create a new QuantLib.Date
       }
       set{
         // write to this.date
       }
    }
    

Upvotes: 1

Related Questions