2016-06-24 14 views
0

現在のカルチャに基づいて、Ninjectバインディングを使用してDbContextの接続文字列を切り替えることができますか?私の現在の(動作していない)タラは以下の通りです。 Ninjectを使用している場合、文化に基づいて接続文字列を切り替える

private static IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     try 
     { 
      kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
      kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

      RegisterServices(kernel); 
      return kernel; 
     } 
     catch 
     { 
      kernel.Dispose(); 
      throw; 
     } 
    } 

    private static string GetCultureBasedConnectionString() 
    { 
     string culture = "de-DE"; // TODO Replce with Thread.CurrentThread.CurrentCulture.Name 
     string cultureBasedConnectionString = ConnectionStringHelper.GetConnectionStringWithCulture(culture); 
     return cultureBasedConnectionString; 
    } 

    private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind<ApplicationDb>().To<ApplicationDb>() 
      .InRequestScope() 
      .WithConstructorArgument("connectionString", context => GetCultureBasedConnectionString()); 
     . 
     . 
     . 
    } 

この

は、ここでは例として、 Ninject - dynamically specifying a connection string based on a sub domainに基づいているが、それは私がそのようにここで読んだことがある...アプリケーションが起動する場合を除き、各リクエストに応じて、私の GetCultureBasedConnectionString()方法に

を介してコールされませんNInjects Rebind()メソッドを使用していないが良いです。

This SO threadでも正しい方向に私を得ることはできませんでした。

答えて

1

はい、期待される動作を説明しています。バインディングコードは、Bindを呼び出したときに実行されます。型が取り出されるたびに呼び出されるわけではありません。 ToMethodで指定されたFunc/Methodは、バインディングが適用されるたびに実行されます。 ConnectionStringGetter()を取り除くため

kernel.Bind<ApplicationDb>().To<ApplicationDb>() 
     .InRequestScope() 
     .WithConstructorArgument(
      "connectionStringGetter", 
      context => GetCultureBasedConnectionString()); 

と:私はあなたがあなたのコードを簡素化することができると信じていますが

0

GetCultureBasedConnectionString()メソッドにコールバックし、これはのHttpRequestが行われるまで、今私のために働いているように見えるれ、GetCultureBasedConnectionString()メソッドの実行を延期するようだFunc<string>を使用するには、わずかな変化...が予想されることbehviourですか?

private static IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     try 
     { 
      kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
      kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

      RegisterServices(kernel); 
      return kernel; 
     } 
     catch 
     { 
      kernel.Dispose(); 
      throw; 
     } 
    } 

    private static string GetCultureBasedConnectionString() 
    { 
     string culture = Thread.CurrentThread.CurrentCulture.Name; 
     string cultureBasedConnectionString = ConnectionStringHelper.GetConnectionStringWithCulture(culture); 
     return cultureBasedConnectionString; 
    } 

    private static Func<string> ConnectionStringGetter() 
    { 
     var function = new Func<string>(GetCultureBasedConnectionString); 
     return function; 
    } 

    private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind<ApplicationDb>().To<ApplicationDb>() 
      .InRequestScope() 
      .WithConstructorArgument("connectionStringGetter", context => ConnectionStringGetter()); 
関連する問題