【问题标题】:Replace Sharp Architecture's NHibernate.config with a Fluent Configuration用 Fluent 配置替换 Sharp Architecture 的 NHibernate.config
【发布时间】:2011-04-18 17:36:39
【问题描述】:

默认情况下,从 Sharp Architecture 的 templify 包生成的解决方案使用 {SolutionName}.Web 项目中的 NHibernate.config 文件配置 NHibernate。我想用我自己的流畅配置替换它,并且仍然让 Sharp Architecture 的其余部分正常工作。

任何帮助将不胜感激。 :)

解决方案:以下是我的工作方式:

IPersistenceConfigurer configurer = OracleClientConfiguration.Oracle10
    .AdoNetBatchSize(500)
    .ShowSql()
    .ConnectionString(c => c.FromConnectionStringWithKey("NHibernate.Localhost"))
    .DefaultSchema("MySchema")
    .ProxyFactoryFactory("NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")
    .UseReflectionOptimizer();

NHibernateSession.Init(
    webSessionStorage,
    new string[] { Server.MapPath("~/bin/MyProject.Data.dll") },
    new AutoPersistenceModelGenerator().Generate(),
    null,
    null,
    null,
    configurer);

【问题讨论】:

    标签: asp.net-mvc nhibernate fluent-nhibernate sharp-architecture


    【解决方案1】:

    iirc 用于配置 nhibernate 的 NhibernateSession 类有一堆重载,其中一个使您能够通过代码对其进行配置。

    【讨论】:

    • 我看不出我会在哪里发送 Fluent 配置变量,不。
    • 嗨-对不起,默认情况下你是正确的,我看到你只能传递配置属性的字典,但最终在内部该类使用流利的配置返回会话工厂。您可能必须修改该类以使其接受流畅的配置。
    • 谢谢,我让它与一堆完全不自然的空值一起工作。 :D
    【解决方案2】:

    很老的帖子。我会把它留在这里以防其他人感兴趣。在 SharpArch 1.9.6.0 上,您可以向 NHibernateSession.cs 添加两个方法。这将允许您传入一个 FluentConfiguration 对象。

        public static FluentConfiguration Init(ISessionStorage storage, FluentConfiguration fluentConfiguration)
        {
            InitStorage(storage);
            try
            {
                return AddConfiguration(DefaultFactoryKey, fluentConfiguration);
            }
            catch
            {
                // If this NHibernate config throws an exception, null the Storage reference so 
                // the config can be corrected without having to restart the web application.
                Storage = null;
                throw;
            }
        }
    
        private static FluentConfiguration AddConfiguration(string defaultFactoryKey, FluentConfiguration fluentConfiguration)
        {
            var sessionFactory = fluentConfiguration.BuildSessionFactory();
            Check.Require(!sessionFactories.ContainsKey(defaultFactoryKey),
                "A session factory has already been configured with the key of " + defaultFactoryKey);
    
            sessionFactories.Add(defaultFactoryKey, sessionFactory);
    
            return fluentConfiguration;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-09-04
      • 1970-01-01
      • 2012-02-14
      • 2012-09-04
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      相关资源
      最近更新 更多