How can I retrieve a Dialect instance in Fluent NHibernate mapping

238 Views Asked by At

Consider we have an application that uses Fluent NHibernate to access some data base (we don't know exactly what type of DB will it be). And consider we want to map some class that has an id of unsigned type (for example ulong):

public class MyClass
{
    public ulong id { get; set; }

    //Other stuff
    ...
}

We also have a custom IUserType that maps ulong as some type supported by DB (as long for example). Lets call it ULongAsLong.

My aim is to write a MyClass mapping so that its id would be mapped as unsigned bigint if DB supports unsigned types and as ULongAsLong if it doesn't. Something like this:

public class MyClassMap : ClassMap<MyClass>
{
    public MyClassMap()
    {
        //Here goes some function or something that retrieves a Dialect instance.
        var dialect = ...;
        if (supportsULong(dialect))
        {
            Id(x => x.id);                    
        }
        else
        {
            Id(x => x.id).CustomType<ULongAsLong>();
        }

        //Mapping everything else
        ...
    }

    private bool supportsULong(Dialect dialect)
    {
        //Some code that finds out if ulong is supported by DB.
    }
}

So the question is how can I retrieve a Dialect instance inside the mapping to make a decision whether to map an id as ulong or as a ULongAsLong.

1

There are 1 best solutions below

1
On

Look at ISessionFactoryImplementor, see https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Engine/ISessionFactoryImplementor.cs

Dialect.Dialect Dialect { get; }

Possible to get using:

ISessionFactory factory = ...; 
((ISessionFactoryImplementor)factory)