David Neale
David Neale

Reputation: 17058

Fluent NHibernate - 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?

Upvotes: 1

Views: 1141

Answers (2)

Jay
Jay

Reputation: 57979

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 => {  
    component.Map(Hours);  
    component.Map(Minutes);  
    component.Map(Seconds);  
    });

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.

Upvotes: 2

Alexander Gro&#223;
Alexander Gro&#223;

Reputation: 10478

I personally haven't worked with AutoMappings yet, but my suggestion would be to look into NHibernate's IUserType to change how a type is being persisted. I believe that's a cleaner way of defining your custom mapping of Time <-> bigint.

Reading the code above, Map(x => x.ToTimeSpan()) will not work as you cannot embed application-to-database transformation code into your mappings. Even if that would be possible, the declaration misses the transformation from the database to the application. A IUserType, on the other hand, can do custom transformations in the NullSafeGet and NullSafeSet methods.

Upvotes: 0

Related Questions