On Apr 4, 2:47 pm, Lorenzo Dieryckx <lorenzo.diery...@gmail.com>
wrote:
> Hi,
>
> I've noticed that the default_schema setting only works for generated
> SQL.
> user-provided SQL (in my case: via Map().Formula()) seems unaffected
> by the default_schema setting.
>
> Is there any way around this?
> I've tried the naive approach by trying to read the default_schema
> from the configuration in the mapping-class and then changing the SQL
> in the .Formula() call to include the default_schema, but even
> accessing the configuration from the mapping-file seems non-trivial.
>
> Any help would be greatly appreciated!
>
> Regards,
> Lorenzo

I solved this myself.

Since it seems impossible to access any configuration in the ClassMap
(and I didn't want to start reading config files manually there) and
since you can't have dependencies injected in the ClassMap via some
IoC framework (Classmaps need a parameterless constructor),
I had to do it the "old fashioned way" with a singleton class:

The singleton:

public class DefaultSchemaProvider
{
    private static DefaultSchemaProvider _instance = null;
    private string _defaultSchema = null;

    public static DefaultSchemaProvider Instance
    {
        get
        {
            if (_instance == null)
                _instance = new DefaultSchemaProvider();
            return _instance;
        }
    }

    public void SetDefaultSchema(string defaultSchema)
    {
        _defaultSchema = defaultSchema;
    }

    public string GetDefaultSchema()
    {
        return _defaultSchema;
    }
}

During configuration:

    var nhibernateCfg = new NHibernate.Cfg.Configuration();
    nhibernateCfg.Configure(ConfigurationFile);
 
DefaultSchemaProvider.Instance.SetDefaultSchema(nhibernateCfg.GetProperty("default_schema"));

in the classmap:

    public class CustomerMap : ClassMap<Customer>
    {
        public CustomerMap()
        {
            var defaultSchema =
DefaultSchemaProvider.Instance.GetDefaultSchema() + ".";

            Table("Customers");

            Id(x => x.Id).Column("Customer_id");
            Map(x => x.Description).Column("Name");
            Component(c => c.CustomerType, c =>
            {
                c.Map(x => x.Id).Column("Customer_type");
                c.Map(x => x.Description).Formula(
                   "(SELECT ct.value FROM " + defaultSchema +
"customer_types ct WHERE ct.field='description' AND ct.typeid =
Customer_type)");
            });
            HasMany(x =>
x.ArticleLinks).KeyColumn("VENR").Access.CamelCaseField(Prefix.Underscore);
        }
    }

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibernate@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to