I have created a Unit of work wrapper for my Nhibernate data access methods. I initialise my Session Factory inside the Static Constructor of the UnitOfWork class, hoping to have it initialised once and only once.
public class UnitOfWork : IUnitOfWork
{
private static readonly ISessionFactory _sessionFactory;
static UnitOfWork()
{
var oracleConfiguration = OracleDataClientConfiguration.Oracle10.ConnectionString(ConfigurationManager.ConnectionStrings[Constants.CONNECTION_STRING].ConnectionString);
_sessionFactory = Fluently.Configure()
.Database(oracleConfiguration)
.Mappings(m => m.FluentMappings.Add<MyMap>())
.BuildSessionFactory();
}
}
I then bind this unitOfWork dependency with Ninject Kernel during my application startup and then expect Ninject to resolve it in my data access repository's constructor. (I am using Constructor injection).
public class Module : NinjectModule
{
public override void Load()
{
Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
But after resolution, I could understand that the code inside the static constructor was never executed during object creation and as a result my Nhibernate Session factory is null.
I am definitely sure that I am missing something here. Could some one help me understand what is the correct way to use static constructor in Ninject ?
Thanks