NHibernate fluid - mapping an entity as a different type


I have a class which I would like to map as a component onto any table which contains it:

public class Time
    public int Hours { get; set; }
    public int Minutes { get; set; }
    public int Seconds { get; set; }

I would like to store this class as a bigint in the database - the same as how TimeSpan is stored but my class has completely different behaviour so I decided to create my own.

I'm using FLH's automapper and have this class set as a component (other classes have Time as a property). I've got as far as creating an override but am not sure how to go about mapping it:

I gave it a try this way:

public class TimeMappingOverride : IAutoMappingOverride<Time>
    public void Override(AutoMapping<Time> mapping)
        mapping.Map(x => x.ToTimeSpan());
        mapping.IgnoreProperty(x => x.Hours);
        mapping.IgnoreProperty(x => x.Minutes);
        mapping.IgnoreProperty(x => x.Seconds);

But got this error:

Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MethodCallExpression'.

How should I go about this?

Details of components can be found here: http://wiki.fluentnhibernate.org/Fluent_mapping#Components

But first of all, you can't map a method.

Assuming you change ToTimeSpan() to a property AsTimeSpan, there are two ways to do it, only the harder of which will work for you because you are using automapping:

  1. Create a ComponentMap<Time> -- once done, your existing mapping will just work. This is not compatible with automapping.
  2. Declare the component mapping inline:
mapping.Component(x => x.AsTimeSpan, component => {

You'll have to do this every time, though.

Of course, this doesn't address "I would like to store this class as bigint…"

Are you saying you want to persist it as seconds only? If so, scratch everything at the top and again you have two options:

  1. Implement NHibernate IUserType (ugh)
  2. Create a private property or field that stores the value as seconds only, and wire only this up to NHibernate. The getters and setters of the pubic properties will have to convert to/from seconds.