Reputation: 4435
Is there any way to get Configuration details from instance of ISessionFactory in NHibernate?
Upvotes: 3
Views: 4390
Reputation: 5046
If you have an instance of the NHibernate session around, maybe this SO answer would help? https://stackoverflow.com/a/2429881/2472
Upvotes: 0
Reputation: 4180
If you use dependency injenction:
public IServiceCollection ConfigureServices(IServiceCollection services)
{
// Add Configuration
var configuration = BuildHNibernateConfiguration(_appSettings.connectionString);
services.AddSingleton(configuration);
// Add SessionFactory
services.AddSingleton<ISessionFactory>(s => s.GetRequiredService<Configuration>().BuildSessionFactory());
// Add Session
services.AddScoped<ISession>(s => s.GetRequiredService<ISessionFactory>().WithOptions().Interceptor(new AppInterceptor(s)).OpenSession());
and then somewhere else:
var cfg = serviceProvider.GetRequiredService<Configuration>();
Upvotes: 0
Reputation: 1
From Session I use the following
Session.Connection.ConnectionString;
My class is
UserIO : Base<User>
User is entity.
Upvotes: 0
Reputation: 733
I have written an extension class that maps a session factory and configuration over a hashtable. And you can easily get a configuration for a session factory in any place of your code but you must set a configuration for a factory first.
public static class SessionFactoryConfigurationBindingExtension
{
private static readonly Dictionary<ISessionFactory, Configuration> _mappings = new Dictionary<ISessionFactory, Configuration>();
private static readonly Object _mappingsLocker = new Object();
public static Configuration GetConfiguration(this ISessionFactory sessionFactory)
{
lock (_mappingsLocker)
{
if (_mappings.ContainsKey(sessionFactory))
{
return _mappings[sessionFactory];
}
else
{
return null;
}
}
}
public static void SetConfiguration(this ISessionFactory sessionFactory, Configuration configuration)
{
lock (_mappingsLocker)
{
_mappings[sessionFactory] = configuration;
}
}
}
Upvotes: 1
Reputation: 24614
the ISessionFactory doesn't expose the configuration that was used to create the session factory, and I'm not sure the concrete implementation does either.
However, why don't you consider injecting the configuration to? Maybe you are not using dependency injection, if you do, just register it into the kernel.
Otherwise, consider using a wrapper class that keeps both the configuration and the ISessionFactory.
Upvotes: 2
Reputation: 15303
If you are using app.config
or hibernate.xml.cfg
I use the following to expose configuration:
NHibernate.Cfg.Configuration normalConfig = new NHibernate.Cfg.Configuration().Configure();
I pass the above in when I configure my session factory and I just expose this configuration object in my static session factory class.
Upvotes: 1