2017-10-23 9 views
0

私は流暢NHibernateはを使用していますから:https://github.com/jagregory/fluent-nhibernateNHibernateの流暢 - 複数のデータベース構成

それがこの構成はAutofacにある別のデータベース(異なる接続文字列)

  builder.Register(c => 
      Fluently.Configure() 
       .Database(DatabaseConfiguration) // <-- Connection string 1 
       .Mappings(AutoMapping.Configurations) 
       .ExposeConfiguration(cfg => cfg.SetProperty("connection.isolation", "ReadCommitted")) 
       .ExposeConfiguration(cfg => cfg.SetProperty(Environment.CommandTimeout, c.Resolve<IConfig>().SqlCommandTimeoutSeconds.ToString())) 
       .BuildConfiguration()) 
      .SingleInstance(); 

      builder.Register(c => 
      Fluently.Configure() 
       .Database(ReportingDatabaseConfiguration) // <-- Connection string 2 
       .Mappings(AutoMapping.Configurations) 
       .ExposeConfiguration(cfg => cfg.SetProperty("connection.isolation", "ReadCommitted")) 
       .ExposeConfiguration(cfg => cfg.SetProperty(Environment.CommandTimeout, c.Resolve<IConfig>().SqlCommandTimeoutSeconds.ToString())) 
       .BuildConfiguration()) 
      .SingleInstance(); 

    builder.Register(c => 
      c.Resolve<Configuration>() 
       .BuildSessionFactory()) 
      .SingleInstance(); 

のための複数の設定を持つことが可能です。

現在の動作は、後の動作が最初の設定を上書きします。

私の期待する結果は、私が照会しているエンティティに応じて、どのデータベースを使うべきかを知ることができるということです。

これは可能ですか?

注::私はhttp://devstoolbox.altervista.org/multiple-connections-using-nhibernate/ に記載されている解決策を試しましたが、私にとっては役に立ちません。

答えて

1

エンティティに基づいてどの接続(セッション)を使用するかを知るためにnhibernateを使用することはできないか、非常に良い考えです。異なるdbsの2つのエンティティに参加するとどうなりますか?期待される結果は何でしょうか?

リポジトリまたはクエリクラスで「正しい」セッションをリクエストするとどうなりますか?このクラスでクエリが実行されると予想されるデータベースのコンテキストが必要です。右?

あなたはまあ説明し、構成のNHibernateのSessionFactory

public class NHConnection 
{ 
private string _connectionString; 
private Type _markerType; 

public WithConnectionString(string connectionString) 
{ 
    _connectionString = connectionString; 
    return this; 
} 

public NHConnection UseMarkerAssembly(Type markerAssembly) 
    { 
     _markerType = markerAssembly; 
     return this; 
    } 

public ISessionFactory Build() 
{ 
    var config = Fluently.Configure() 
      .Database(_connectionString) // <-- Connection string 2 

      //.Mappings(AutoMapping.Configurations) consider using a configurable markerAssembly for each db like: 
      .Mappings(m => 
      { 
       m.FluentMappings.AddFromAssembly(markerType.Assembly) 
      }); 

      .ExposeConfiguration(cfg => cfg.SetProperty("connection.isolation", "ReadCommitted")) 
      .ExposeConfiguration(cfg => cfg.SetProperty(Environment.CommandTimeout, c.Resolve<IConfig>().SqlCommandTimeoutSeconds.ToString())) 
      .BuildConfiguration()); 
    return config.BuildSessionFactory(); 
} 
} 

//Register the FactoryBuilder in your Autofac Module 

builder.Register(x => new NHConnection().WithConnectionString("your;connectionString:toDb1").UseMarkerAssembly(typeof(MarkerTypeAssemblyForDB1Mappings)).Build()).Keyed<ISessionFactory>("db1").SingleInstance(); 
builder.Register(x => new NHConnection().WithConnectionString("your;connectionString:toDb2").UseMarkerAssembly(typeof(MarkerTypeAssemblyForDB2Mappings)).Build()).Keyed<ISessionFactory>("db2").SingleInstance(); 
builder.Register<Func<string, ISessionFactory>>(c => 
     { 
      IComponentContext co = c.Resolve<IComponentContext>(); 
      return db => co.ResolveKeyed<ISessionFactory>(db); 
     });  


// Resolve the factory for DB 1, 2 or 3 in your query/repo class  

public class QueryClass{ 
    private _factoryLookUp Func<string, ISessionFactory> FactoryLookup { get; set; } 
    public void QueryClass(Func<DataDomain, ISessionFactory> factoryLookup) 
    { 
    _factoryLookUp = factoryLookup; 
    } 

    public executeYourQuery() 
    { 
    using(var session = factoryLookup("db1").OpenSession) 
    { 
     .... 
    } 
    } 

} 
+0

を返しますNH-接続クラスを登録し、これは私には意味を成しません。 私は、各Repoクラスではなく、Autofacでセッションの初期化を解決したいと考えていました。 あなたの助けをありがとう:) – Danielyap

+0

私はあなたを聞いています。 BaseRepBb1とBaseRepoDb2を使用すると、セットアップを非表示にすることができます –