Skip to content

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 FeatureStore implementation of the IFeatureStore interface, which can be extended as needed.
  • An INHibernateConnectionFactory for creating session factories with default implementation.

Installation

First, install the package:

dotnet add package FeatureManagement.Database.NHibernate

Basic Setup

Use 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>();
    });

Custom Mapping Configuration

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);
        });
    }
}

Custom NHibernate Connection Factory

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();

Different Database Dialects

Configure NHibernate to work with different database dialects:

SQL Server

options.DatabaseConfigurer = MsSqlConfiguration.MsSql2012
    .ConnectionString(Configuration.GetConnectionString("SqlServer"));

PostgreSQL

options.DatabaseConfigurer = PostgreSQLConfiguration.Standard
    .ConnectionString(Configuration.GetConnectionString("PostgreSql"));

SQLite

options.DatabaseConfigurer = SQLiteConfiguration.Standard
    .ConnectionString(Configuration.GetConnectionString("Sqlite"));

Complete Example

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...
}

Clone this wiki locally