0

If I have an object with a DateTime property is it possible to map the Date and the Time to seperate properties in the same object? I.e An object with a StartDateTime, StartDate and StartTime where StartDateTime exists as a column in the database and StartDate and StartTime simply reference the different parts of the StartDateTime column.

Thanks in advance for any help you can give.

1 Answers1

1

I would say, that trick here would be:

  1. map one property to that column. It could be hidden for other parts of your Model, even protected (e.g. see here)
  2. create two virtual (I mean un-mapped properties) public properties accessing and setting that mapped value.

For example, very similar scenario I use for mapping a version. So while this would be the mapping:

<version name="Timestamp" generated="always" unsaved-value="null" type="BinaryBlob">
  <column name="RowVersion" not-null="false" sql-type="timestamp"/>
</version>

It is working with byte[] which is hard to send to UI - Client. So this this would be the Domain Entity representation.

protected virtual byte[] Timestamp { get; set; }

public virtual string Version
{
    get { return Timestamp.IsEmpty() ? null : Convert.ToBase64String(Timestamp); }
    set { Timestamp = value.IsEmpty() ? null : Convert.FromBase64String(value); }
}

Having that in place, we can send the version to client as a string... and get it back...

NOTE: I would map the DB column to public property. It makes it easier to use that property in QueryOver queries... at least getter could be public

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335