Skip to content

Set IUserType by convention #1735

@EamonHetherton

Description

@EamonHetherton

I am using a custom type in my code to store small base 10 decimal values without the memory overhead of System.Decimal (4 bytes vs 16 bytes).

public struct SmallDecimal : IEquatable<SmallDecimal>, IEquatable<decimal>, IComparable, IComparable<SmallDecimal>
    {...}

I also have a custom IUserType that maps this type.

I am using ConventionModelMapper to set up a number of conventions including one that inspects the property type and sets the appropriate IUserType

mapper.BeforeMapProperty += (mi, propertyPath, map) =>
{
    var propertyType = propertyPath.LocalMember.GetPropertyOrFieldType();
    if (propertyType.Equals(typeof(SmallDecimal)) || propertyType.Equals(typeof(SmallDecimal?)))
    {
        map.Type(typeof(SmallDecimalUserType), null);
    }
};

This works correctly as long as I explicitly map every property that is SmallDecimal, but what I want is to be able to set up some kind of convention such that any property of type SmallDecimal is automatically mapped as a property with a type SmallDecimalUserType without the explicit mapping. (I have a lot of properties across a lot of classes that are currently automatically mapped as decimal here that I want to change to SmallDecimal)

At the moment, any property I do not explicitly map that has a type of SmallDecimal ends up mapped as a component.

I've never used fluent NHibernate but it appears that I could achieve the above with their UserTypeConvention.

I'd rather stick with the native mapping-by-code Is there a parallel in mapping by code?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions