0

NHibernate を使用するアプリケーションがあり、Fluent NHibernate を使用してエンティティをマッピングしています。正常に動作しますが、NHibernate のネイティブな方法を使用して SessionFactory を作成したいと考えています。これは、私のチームが他のプロジェクトでこのライブラリを使用するためです。nhibernate.cfg.xml を移動するには、この柔軟性が必要です。私の質問は: nhibernate のネイティブな方法で SessionFactory の構成に Fluent Mappings を設定するにはどうすればよいですか?

構成方法で次のようなことを試します。

private static ISessionFactory Configure()
{
    if (_factory != null)
        return _factory;

    var configuration = new Configuration().Configure();

        // I could set my assembly of mapping here, but it's on our internal framework
    var fluentConfiguration = Fluently.Configure(configuration)
        //.Mappings(c => c.FluentMappings.AddFromAssembly(typeof(ProductMap)))
        .BuildConfiguration();

    _factory = fluentConfiguration.BuildSessionFactory();

    return _factory;
}

xmlで設定しようとしましたが、うまくいきません。

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>     
        <!-- other configs here...-->     
        <mapping assembly="MyApplication.Data.Mapping" />
    </session-factory>
</hibernate-configuration>

このマッピングをxmlに設定FluentConfigurationし、メソッドの宣言に渡してISessionFactory.

君たちありがとう。

4

1 に答える 1

0

Fluentmappings を考慮しないため、構成内のマッピングは機能しません (Nhibernate は FluentNhibernate を認識しません)。コードで設定する必要があります。私が考えることができる最良のオプションは、sessionfactory を構築する前に構成オブジェクトを変更するフックを実装することです。

private static ISessionFactory Configure()
{
    if (_factory != null)
        return _factory;

    var configuration = new Configuration().Configure();

    foreach(var alteration in alterations)
    {
        alteration.AddTo(configuration);
    }

    _factory = fluentConfiguration.BuildSessionFactory();

    return _factory;
}

// in your alteration
Configuration AddTo(Configuration config)
{
    return Fluently.Configure(config)
               .Mappings(c => c.FluentMappings.AddFromAssembly(typeof(ProductMap)))
               .BuildConfiguration();
}
于 2012-09-14T06:51:55.030 に答える