Search code examples
c#.netnhibernatefluent-nhibernatenhibernate-configuration

FluentNHibernate HiLo - can be maxLo read from table and not to be hardwired in code?


I am using

GeneratedBy.HiLo(string table, string column, string maxLo, string where);

for primary key. Currently I am looking for possibility how to load maxLo from table rather than store it as a constant in code. Value for NextHi is loaded from database table (ok, it must be otherwise whole concept would not be working at all). But I did not find a way how to load maxLo from table too. From quick code study it seems that it is not possible, but still maybe I am missing something.

Reason why I need it: I have business application and separate config application which needs to use same maxLo for consistency of ids if it inserts something into tables. Of course application can be run just exclusively. Two possible workarounds: - I can have some shared Dll where maxLo would be stored - I can use table in database and load maxLo on my own

But still it would be fine to do what I want without any workarounds.

FluentNHibernate version: 2.0.1.0


Solution

  • I wish to have the answer, but mine is: no. We have to pass this values as a setting. The reason is the way how is the table hi-lo generator implemented.

    Check the TableHiLoGenerator code .Configure()

    /// <summary>
    /// Configures the TableHiLoGenerator by reading the value of <c>table</c>, 
    /// <c>column</c>, <c>max_lo</c>, and <c>schema</c> from the <c>parms</c> parameter.
    /// </summary>
    /// <param name="type">The <see cref="IType"/> the identifier should be.</param>
    /// <param name="parms">An <see cref="IDictionary"/> of Param values that are keyed by parameter name.</param>
    /// <param name="dialect">The <see cref="Dialect.Dialect"/> to help with Configuration.</param>
    public override void Configure(IType type, IDictionary<string, string> parms, Dialect.Dialect dialect)
    {
        base.Configure(type, parms, dialect);
        maxLo = PropertiesHelper.GetInt64(MaxLo, parms, Int16.MaxValue);
        lo = maxLo + 1; // so we "clock over" on the first invocation
        returnClass = type.ReturnedClass;
    }
    

    The most interesting part is the comment:

    //Configures the TableHiLoGenerator by reading the value of <c>table</c>, 
    // <c>column</c>, <c>max_lo</c>, and <c>schema</c> from the <c>parms</c> parameter.
    

    And the parameter IDictionary<string, string> parms

    In extreme case, we can use this (and its fluent version: GeneratedBy.HiLo(...)) - to read properties from somewhere (e.g. with ADO.NET) and pass such values to configuration... It is really extreme, but could be the answer