Fluent NHibernate - How to do Additional Processing when Converting from SQL to C# type? -


i'm using fluent nhibernate data access layer, , need every time value in sql mapped datetime type:

var newdatetime = datetime.specifykind(olddatetime, datetimekind.local); 

in code above, newdatetime represents value should returned sql c# conversions, , olddatetime represents default converters nhibernate automatically convert to.

besides issue fluent nhibernate documentation bleak, i've tried searching around internet conventions let me this, iusertype heavy (and haven't been able find comprehensive explanation of how implement methods derived iusertype), , ipropertyconvention seems offer ways modify how c# being converted sql (not other way around, need in scenario).

can please point me in right direction? and/or provide quality links read on conventions? none of wiki pages explain in detail please refrain linking those. thank you.

nhibernate (not fluent) supports setting, distinguishing how handle datetime stored in db (regardless of db support offset e.g. datetimeoffset (transact-sql)). see 5.2.2. basic value types

get db:

so, can explicitly define, how treat value returned table column this:

map(x => x.expirydate).customtype<utcdatetimetype>(); // utc map(x => x.maturitydate).customtype<localdatetimetype>(); // local 

so, once retrieved db, datetime properties automatically provided correct kind setting:

assert.istrue(entity.expirydate.kind == datetimekind.utc); assert.istrue(entity.maturitydate.kind == datetimekind.local); 

set

let me provide extract date/time support in nhibernate:

notice nhibernate did not perform conversions or throw exception when saving/loading datetime value wrong datetimekind. (it argued nhibernate should throw exception when asked save local datetime , property mapped utcdatetime.) developer ensure proper kind of datetime in appropriate field/property.

other words, entity datetime, coming client (during binding, deserializing etc) must correctly set in custom == our code.

an example, in web api, during conversion of datetime json. while json utc, db set lcoal. can inject converter , sure, that:

class datetimeconverter : isodatetimeconverter {     public datetimeconverter()     {         datetimestyles = datetimestyles.adjusttouniversal;     }      public override object readjson(jsonreader reader, type objecttype            , object existingvalue, jsonserializer serializer)     {         var result = base.readjson(reader, objecttype, existingvalue, serializer);         var datetime = result datetime?;         if (datetime.is() && datetime.value.kind == datetimekind.utc)         {             return datetime.value.tolocaltime();         }         return result;     } 

and can sure that:

  1. get - our mapping .customtype<localdatetimetype>() correctly inform application data coming db in local zone
  2. set - converter, correctly set datetime values local zone

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -