用流畅的配置替换Sharp Architecture的NHibernate.config

时间:2011-04-18 17:36:40

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

默认情况下,Sharp Architecture的templify包生成的解决方案使用NHibernate.config项目中的{SolutionName}.Web文件配置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);

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

很老的帖子。我会把它放在这里以防其他人感兴趣。在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;
    }