I have an implemetation of session factory to be singleton like this:
public sealed class MySessionFactory
{
private static volatile MySessionFactory _instance;
private ISessionFactory _sessionFactory;
private static volatile object _locker = new object();
private MySessionFactory()
{
}
public MySessionFactory Intance
{
get
{
if (_instance != null)
return _instance;
lock (_locker)
{
if (_sessionFactory == null)
{
_instance = new MySessionFactory();
}
}
return _instance;
}
}
public ISession OpenSession()
{
if (_sessionFactory != null)
return _sessionFactory.OpenSession();
lock (_locker)
{
if (_sessionFactory == null)
{
var cfg = FluentNHibernate.Cfg.Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.PostgreSQLConfiguration.Standard.ConnectionString("connectionString").UseReflectionOptimizer())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsAssembly>());
_sessionFactory = cfg.BuildSessionFactory();
}
}
return _sessionFactory.OpenSession();
}
}
If i remove the volatile of static variable _instance, I will get some benefits with this change? Or this is a good practice pattern?
Aucun commentaire:
Enregistrer un commentaire