Free; SessionSource or SessionFactory for creating sessions?

I am using NHibernate + Fluent to process the database in my application. So far, I have used SessionSource to create ISession objects. I'm a little confused now about what comes from NHibernate or Fluent, and what I really should use to create my sessions.

ISession comes from NHibernate and SessionSource from Fluent. I am creating a SessionSource from FluentConfiguration and am currently using SessionSource to create sessions. This is my function for creating sessions. The FluentConfiguration and SessionSource resources are reused:

if (_sessionSource == null)
{
    _cfg = Fluently.Configure().Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("test.db"));
    _sessionSource = new SessionSource(_cfg.BuildConfiguration().Properties, new MappingsPersistenceModel());
    var session = _sessionSource.CreateSession();
    _sessionSource.BuildSchema(session);
    return session;
}
return _sessionSource.CreateSession(); 

Does this look reasonable? It sounds more attractive to use ISessionFactory to create sessions, so I tried using it. This comes from NHibernate, so I don’t know why this is a problem, but it fails when my sessions are created from ISessionFactory.

// Done once: 
_sessionFactory = _cfg.BuildSessionFactory();

// Done each time a session is requested: 
_sessionFactory.OpenSession()

Using this, I get a MappingExceptionwhen using the session, saying "No persister for: MyProject.Model.SomeModelClass".

Should I keep using SessionSource? Or am I missing something regarding the ISessionFactory?

+3
source share
3 answers

, SessionFactory , SessionSource. factory , , . , . , ?

private static ISession CreateSession()
{
    if (_sessionFactory == null)
    {
        _sessionFactory = Fluently.Configure().
            Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("test.db")).
            Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()).
            BuildSessionFactory();
    }
    return _sessionFactory.OpenSession();
}
+5

, . . factory .

 public class Common
    {
        public const string NHibernateSessionKey = "nhibernate.session.key";

        public static string ConnString
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["JewelSoftMySqlConn"].ConnectionString;
            }
        }

        public static ISessionFactory  FACTORY = CreateFactory();

        static ISessionFactory CreateFactory()
        {
            Configuration config = new Configuration();
            IDictionary props = new Hashtable();

            props.Add("hibernate.dialect", "NHibernate.Dialect.MySQLDialect");
            props.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
            props.Add("hibernate.connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
            props.Add("hibernate.connection.connection_string", Common.ConnString);
            config.AddProperties(props);

            config.AddInputStream(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(Resource.Models_hbm)));

            return config.BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            ISession currentSession = null;
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                currentSession = context.Items[NHibernateSessionKey] as ISession;
                if (currentSession == null)
                {
                    currentSession = FACTORY.OpenSession();
                    context.Items[NHibernateSessionKey] = currentSession;
                }
            }
            else//will work non web request, like in test environment
                currentSession = FACTORY.OpenSession();

            return currentSession;
        }
    }
+3

, ! NH . -, SessionSource, AFAIK, . ISessionFactory NH.

? , , , , , - cfg-.

+2
source

Source: https://habr.com/ru/post/1727597/


All Articles