2017-10-23 19 views
0

내가로부터 유창함 자 NHibernate를 사용하고 있습니다 : https://github.com/jagregory/fluent-nhibernateNHibernate에 유창함 - 여러 데이터베이스 구성

다른 데이터베이스 (다른 연결 문자열)

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

이 구성은 Autofac에 대해 여러 구성을 할 수 있습니다.

현재 동작이 나중에 발생하면 첫 번째 구성이 무시됩니다.

예상되는 결과는 ISession이 쿼리 할 엔터티에 따라 사용할 데이터베이스를 알 수 있어야합니다.

이것이 가능합니까?

참고 : http://devstoolbox.altervista.org/multiple-connections-using-nhibernate/ 에 언급 된 솔루션을 시도했지만 저를 위해 작동하지 않습니다.

답변

1

엔터티를 기반으로 사용할 연결 (세션)이 가능하지 않거나 아주 좋은 생각인지 알기 위해 nhibernate가 있어야합니다. 다른 dbs의 두 엔티티에 가입하면 어떻게 될까요? 예상되는 결과는 무엇입니까?

repo 또는 쿼리 클래스에서 "올바른"세션을 요청하면 어떻게됩니까? 이 클래스에서 쿼리를 실행할 db가 무엇인지에 대한 컨텍스트가 있어야합니다. 권리?

당신은 잘 설명 구성 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를 사용하여 언제든지 설정을 숨길 수 있습니다. –