【问题标题】:Fluent; SessionSource or SessionFactory for creating sessions?流利;用于创建会话的 SessionSource 或 SessionFactory?
【发布时间】:2011-01-02 10:23:37
【问题描述】:

我正在使用 NHibernate + Fluent 来处理我的应用程序中的数据库。到目前为止,我一直在使用 SessionSource 来创建我的 ISession 对象。我现在对来自 NHibernate 或 Fluent 的内容以及我真正应该使用什么来创建会话感到有些困惑。

ISession 来自 NHibernate,SessionSource 来自 Fluent。我从 FluentConfiguration 创建 SessionSource,目前使用 SessionSource 创建会话。这是我创建会话的功能。 FluentConfiguration 和 SessionSource 被复用:

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(); 

这看起来合理吗?不过,使用 ISessionFactory 创建会话听起来更有吸引力,所以我尝试使用一个。这来自 NHibernate,所以我不知道这是否是一个问题,但是当我的会话是从 ISessionFactory 创建时它会失败。

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

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

使用这个我在使用会话时得到一个MappingException,说“No persister for: MyProject.Model.SomeModelClass”。

我应该继续使用 SessionSource 吗?还是我错过了有关 ISessionFactory 的一些东西?

【问题讨论】:

  • 您能解释一下您从会话工厂得到的错误吗?
  • 在我自己的应用程序中,我使用 BuildConfiguration() 方法并将其传递给会话工厂。如果您遇到错误,该消息可能会为我们提供更多信息来解决您的问题。
  • 在上面的描述中添加了例外。得跑了-稍后会回来跟进..

标签: c# .net nhibernate session fluent-nhibernate


【解决方案1】:

问题似乎是 SessionFactory 不知道映射,因为它们只提供给 SessionSource。在流利的配置期间添加映射并从中获取工厂似乎有所帮助。这给了我看起来更好的解决方案。对于那些有更多经验的人来说,这看起来合理吗?

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();
}

【讨论】:

  • +1 正要写这样的东西。这是要走的路,干得好。请注意,这个示例(至少类似)在 FluentNH 的优秀教程中。 (注意:两天后您可以将自己的答案标记为已接受)
  • 感谢您的评论和确认我是在正确的方式。非常感谢您给我的帮助!
【解决方案2】:

请看这门课可能对你有帮助。我已经写了创建工厂和会话的方法。

 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;
        }
    }

【讨论】:

  • 感谢您的回复。从我可以看到你没有在这里使用 Fluent?只有NHibernate?我的问题与同时使用 Fluent 和 NHibernate 有关..
  • 对不起@@Bambuska ...感谢我的新想法..很长一段时间我也想学习这种使用Nhibernate的方法
  • 没问题!查看 Fluent Wiki。写得非常好,应该对入门有很好的帮助:wiki.fluentnhibernate.org/Getting_started
【解决方案3】:

我知道你的感受! fluent 和 NH 之间的区别在开始时可能会相当混乱。在我看来,你不应该使用 SessionSource,AFAIK 它只在测试场景中真正有用。我建议您直接从 NH 使用 ISessionFactory。

你能发布你的错误吗?您似乎使用正确,因此配置或 cfg 对象可能有问题。

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2021-02-13
    • 2014-06-14
    • 2019-03-04
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多