I'm trying to figure out how to create a NHibernate
UnitOfWork
using .NET Core Dependency Injection. I was just hard-coding the connection string before moving it to appsettings.json configuration file. Is this form of creating a UOW in the service.scope valid?
It works, but I'm not sure if this is the intended way to do it.
Startup.cs
services.AddScoped<IUnitOfWork, UnitOfWorkV>( x=> { return new UnitOfWorkV(Configuration); });
UnitOfWorkV.cs
public UnitOfWorkV(IConfiguration configuration)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(configuration.GetSection("ConnectionStrings:ConexionV").Value))
.ExposeConfiguration(ConfigureNhibernateValidator)
.Mappings(m =>
{
m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly());
m.HbmMappings.AddFromAssemblyOf<JobQueue>();
})
.BuildSessionFactory();
Session = _sessionFactory.OpenSession();
Session.FlushMode = FlushMode.Auto;
}
appsettings.json
{
"ConnectionStrings": {
"ConexionV": "Some connection string"
}
}