39 lines
1.4 KiB
C#
39 lines
1.4 KiB
C#
using System.Reflection;
|
|
using FluentNHibernate.Cfg;
|
|
using FluentNHibernate.Cfg.Db;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MySqlConnector;
|
|
using NHibernate;
|
|
using NHibernate.Driver.MySqlConnector;
|
|
|
|
namespace Tiger.Storage;
|
|
|
|
public class NhSessionFactory : INhSessionFactory
|
|
{
|
|
private readonly ISessionFactory _sessionFactory;
|
|
|
|
public NhSessionFactory(IConfiguration configuration)
|
|
{
|
|
var stringBuilder = new MySqlConnectionStringBuilder
|
|
{
|
|
Server = configuration["Database:Host"],
|
|
Port = uint.Parse(configuration["Database:Port"] ?? "3306"),
|
|
UserID = configuration["Database:Username"],
|
|
Password = configuration["Database:Password"],
|
|
Database = configuration["Database:Database"],
|
|
Pooling = true,
|
|
MinimumPoolSize = uint.Parse(configuration["Database:MinPool"] ?? "5"),
|
|
MaximumPoolSize = uint.Parse(configuration["Database:MaxPool"] ?? "15")
|
|
};
|
|
|
|
_sessionFactory = Fluently.Configure()
|
|
.Database(MySQLConfiguration.Standard.ConnectionString(stringBuilder.ToString()).Driver<MySqlConnectorDriver>())
|
|
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
|
|
.BuildSessionFactory();
|
|
}
|
|
|
|
public ISession OpenSession()
|
|
{
|
|
return _sessionFactory.OpenSession();
|
|
}
|
|
} |