Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:41:27+00:00 2026-05-29T09:41:27+00:00

I have a custom user type that is used to map from a decimal

  • 0

I have a custom user type that is used to map from a decimal value out of the database to a property on an entity using Fluent NHibernate. The object works correctly as does the mapping to the object but I do not know how to dynamically change the precision and scale of the decimal sql data type so that I can use different precision and scale in different class maps.

Here is an example of my custom type;

public struct MyCustomType
{
    private readonly decimal value;

    public MyCustomType(decimal value)
    {
        this.value = value;
    }

    public static implicit operator MyCustomType(decimal value)
    {
        return new MyCustomType(value);
    }

    public static implicit operator decimal(MyCustomType value)
    {
        return value.value;
    }

    public string ToString(string format, IFormatProvider provider)
    {
        return value.ToString(format, provider);
    }
}

And here is how I am doing the user type.

public class MyCustomUserType : IUserType
{
    ...

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        MyCustomType customType = (decimal)rs[names[0]];

        return customType;
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        var parameter = (IDataParameter)cmd.Parameters[index];
        parameter.Value = (decimal)(MyCustomType)value;
    }

    ...

    public SqlType[] SqlTypes
    {
        //I think I could hard code the precision and scale here
        get { return new[] { SqlTypeFactory.Decimal }; }
    }

    public Type ReturnedType
    {
        get { return typeof(MyCustomType); }
    }

    public bool IsMutable
    {
        get { return true; }
    }
}

And finally this is how I am mapping the object

public class SomeObjectMappingBase: ClassMap<SomeObject>
{
    protected SomeObjectMappingBase()
    {
        //Currently I do this
        Map(x => x.CustomTypeField).CustomType<MyCustomUserType>();
        //I would like to be able to do this but it does not work
        Map(x => x.CustomTypeField).CustomType<MyCustomUserType>().Scale(10).Precision(20);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:41:27+00:00Added an answer on May 29, 2026 at 9:41 am

    you can implement IParameterizedUserType or subclass MyCustomUserType for each combination of precision and scale.

    Update: unfortunatly i can’t think of a way to enable the syntax .CustomType<SomeObject>().Scale(x).Precision(y). instead

    option 1 to subclass

    class MyCustomUserTypeFor10And20 : MyCustomUserType
    {
        public override SqlType[] SqlTypes
        {
            get { return new[] { SqlTypeFactory.GetDecimal(10, 20) }; }
        }
    }
    
    
    Map(x => x.CustomTypeField).CustomType<MyCustomUserTypeFor10And20>();
    

    option 2 parameterizedType

    <property name="CustomProperty">
       <type name="MyCustomUserType">
          <param name="precision">10</param>
          <param name="scale">20</param>
       </type>
    </property>
    
    class MyCustomUserType : IParameterizedType
    {
        public void SetParameterValues(IDictionary<string, string> parameters)
        {
            precision = parameter["precision"];
            scale = parameter["scale"];
        }
    }
    

    option 3 get somehow this to work: dont map the custom type but set it on the config

    var mapping = config.GetClassMapping(typeof(...));
    
    foreach (var prop in mapping.PropertyIterator.Where(p => p.Type.ReturnedClass == typeof(decimal)))
    {
        prop.Type = new CustomType(typeof(MyCustomType), new Dictionary<string, string>
        {
            { "precision", prop.Type.SqlTypes(null)[0].Precision },
            { "scale", prop.Type.SqlTypes(null)[0].Precision }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a custom user type that works fine when used properties of
I have a custom user control that contains asp:ValidationSummary . It is placed on
I have a custom user control called ErrorNotificationBox. To place that on my page
I have a custom WinForms control (inherits from control, i.e. without user interface jsut
I have a WinForms user control Host with a custom UI Editor. Through that
I want to create a custom field type (MyCompLookup) that will be used in
I have a set of custom data types that can be used to manipulate
I have a content type which is created using CCK's(Establishment). I have used $form['#redirect']
I have a custom user control (ascx) that contains a textbox and a Javascript-based
I have a custom user control with a textbox on it and I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.