-
Notifications
You must be signed in to change notification settings - Fork 2
NHibernate Database Provider
Teuz edited this page Apr 3, 2025
·
1 revision
For easy integration with NHibernate, you can use the FeatureManagement.Database.NHibernate package.
This package provides:
- A default
FeatureStoreimplementation of theIFeatureStoreinterface, which can be extended as needed. - An
INHibernateConnectionFactoryfor creating session factories with default implementation.
First, install the package:
dotnet add package FeatureManagement.Database.NHibernateUse the default NHibernateConnectionFactory and configure the services:
services.AddDatabaseFeatureManagement<FeatureStore>()
.UseNHibernate(options =>
{
options.DatabaseConfigurer = MsSqlConfiguration.MsSql2012
.ConnectionString(Configuration.GetConnectionString("DefaultConnection"));
options.ConfigureMapping = mapper => mapper.FluentMappings
.AddFromAssemblyOf<FeatureMap>();
});Create custom mapping classes for Feature and FeatureSettings:
public class FeatureMap : ClassMapping<Feature>
{
public FeatureMap()
{
Table("Features");
Id(x => x.Id, map => map.Generator(Generators.GuidComb));
Property(x => x.Name, map =>
{
map.Length(100);
map.NotNullable(true);
map.Unique(true);
});
Property(x => x.RequirementType);
Bag(x => x.Settings, col =>
{
col.Key(k => k.Column("FeatureId"));
col.Cascade(Cascade.All | Cascade.DeleteOrphans);
},
rel => rel.OneToMany());
}
}
public class FeatureSettingsMap : ClassMapping<FeatureSettings>
{
public FeatureSettingsMap()
{
Table("FeatureSettings");
Id(x => x.Id, map => map.Generator(Generators.GuidComb));
Property(x => x.FilterType, map => map.Length(100));
Property(x => x.Parameters, map => map.Type(NHibernateUtil.StringClob));
ManyToOne(x => x.Feature, map =>
{
map.Column("FeatureId");
map.NotNullable(true);
});
}
}Implement your own NHibernate connection factory:
public class MyNHibernateConnectionFactory : INHibernateConnectionFactory
{
private readonly ISessionFactory _sessionFactory;
public MyNHibernateConnectionFactory()
{
var configuration = new Configuration();
configuration.DataBaseIntegration(db =>
{
db.ConnectionString = "Server=localhost;Database=FeatureManagement;Trusted_Connection=True;";
db.Dialect<MsSql2012Dialect>();
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
db.LogFormattedSql = true;
db.LogSqlInConsole = true;
});
configuration.AddMapping(new FeatureMap());
configuration.AddMapping(new FeatureSettingsMap());
_sessionFactory = configuration.BuildSessionFactory();
}
public ISessionFactory GetSessionFactory()
{
return _sessionFactory;
}
}Then register your custom factory:
// Register custom factory
services.AddSingleton<INHibernateConnectionFactory, MyNHibernateConnectionFactory>();
// Use the registered factory
services.AddDatabaseFeatureManagement<FeatureStore>()
.UseNHibernate();Configure NHibernate to work with different database dialects:
options.DatabaseConfigurer = MsSqlConfiguration.MsSql2012
.ConnectionString(Configuration.GetConnectionString("SqlServer"));options.DatabaseConfigurer = PostgreSQLConfiguration.Standard
.ConnectionString(Configuration.GetConnectionString("PostgreSql"));options.DatabaseConfigurer = SQLiteConfiguration.Standard
.ConnectionString(Configuration.GetConnectionString("Sqlite"));public void ConfigureServices(IServiceCollection services)
{
// Register NHibernate feature management
services.AddDatabaseFeatureManagement<FeatureStore>()
.UseNHibernate(options =>
{
// Configure database connection
options.DatabaseConfigurer = MsSqlConfiguration.MsSql2012
.ConnectionString(Configuration.GetConnectionString("DefaultConnection"))
.ShowSql();
// Configure mappings
options.ConfigureMapping = mapper => mapper.FluentMappings
.AddFromAssemblyOf<FeatureMap>();
// Additional configuration
options.ConfigurationSetup = config =>
{
config.SetProperty(NHibernate.Cfg.Environment.BatchSize, "20");
config.SetProperty(NHibernate.Cfg.Environment.CommandTimeout, "60");
};
})
.WithCacheService();
// Other service registrations...
}