开发者

Replace Sharp Architecture's NHibernate.config with a Fluent Configuration

By default, the solution generated from Sharp Architecture's templify package conf开发者_高级运维igures NHibernate using an NHibernate.config file in the {SolutionName}.Web project. I would like to replace it with a fluent configuration of my own and still have the rest of Sharp Architecture work correctly.

Any help will be much appreciated. :)

Solution: Here's how I got it to work:

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


iirc the NhibernateSession class that is used to configure nhibernate has a bunch of overloads one of them giving you the ability to configure it via code.


Very old post. I'll leave it here in case someone else is interested. On SharpArch 1.9.6.0 you can add two methods to NHibernateSession.cs. This will let you pass-in a FluentConfiguration object.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜