I'm building a App that use Context of EF in Singleton Pattern like NHibernate work with Session:
public class DbContextFactory
{
private static volatile DbContextFactory _dbContextFactory;
private static readonly object SyncRoot = new Object();
public DbContext Context;
public static DbContextFactory Instance
{
get
{
if (_dbContextFactory == null)
{
lock (SyncRoot)
{
if (_dbContextFactory == null)
_dbContextFactory = new DbContextFactory();
}
}
return _dbContextFactory;
}
}
public DbContext GetOrCreateContext()
{
if (this.Context == null)
this.Context = new DbContext(ConfigurationManager.AppSettings["DefaultConnectionString"]);
return Context;
}
}
I'm using Ninject to Inject the Context:
public class DbContextModule : NinjectModule
{
public override void Load()
{
Bind<IDbContext>().ToConstant(DbContextFactory.Instance.GetOrCreateContext());
}
}
I'm reading about that approach and some people are saying that is bad practice and I'll have problems.
Someone that know about this with EF can explain me in more details?