Ninject を IoC コンテナーとして使用して Nancy モジュールをテストしようとしています。私の問題は、Nancy に IoC バインディングを使用して NancyModule タイプを解決させることができないように見えることです。
最新の Nancy on Nuget、最新の Ninject でソースからビルドされた最新の Nancy.Bootstrap.Ninject を使用しています。
私のテスト設定は次のとおりです。
[TestFixtureSetUp]
public virtual void ClassSetup()
{
Assembly.Load(typeof (MyModule).Assembly.FullName);
var bootstrapper = new AspHostConfigurationSource();
this.host = new Browser(bootstrapper);
}
[Test]
public void test()
{
/*snip */
var id = entity.Id;
var response = host.Put("/path/to/{0}/".With(id.Encode()),
(with) =>
{
with.HttpRequest();
with.Header("Accept", "application/xml");
});
response.StatusCode.Should().Be(Nancy.HttpStatusCode.OK);
/*snip */
}
これが私のテスト セットアップです。これが私のホスト設定です(私のアセンブリで定義されています):
public class MyBootstrapper: Nancy.Bootstrappers.Ninject.NinjectNancyBootstrapper
{
public bool ApplicationContainerConfigured { get; set; }
public Ninject.IKernel Container
{
get { return ApplicationContainer; }
}
public bool RequestContainerConfigured { get; set; }
protected override void ConfigureApplicationContainer(Ninject.IKernel existingContainer)
{
this.ApplicationContainerConfigured = true;
base.ConfigureApplicationContainer(existingContainer);
Nancy.Json.JsonSettings.MaxJsonLength = Int32.MaxValue;
StaticConfiguration.DisableCaches = true;
}
protected override void ConfigureRequestContainer(Ninject.IKernel container, NancyContext context)
{
container.Load(new[] { new ServiceModule() });
}
protected override DiagnosticsConfiguration DiagnosticsConfiguration
{
get { return new DiagnosticsConfiguration { Password = @"12345" }; }
}
}
私のIoCバインディングは次のとおりです。
Bind<Nancy.NancyModule>()
.ToMethod(context =>
{
return new MyModule1(context.Kernel.Get<IMongoRepository<Guid, Entity>>(
Properties.Settings.Default.NamedCollection1),
context.Kernel.Get<IMongoRepository<Guid, Entity>>(
Properties.Settings.Default.NamedCollection2));
})
.Named(typeof(MyModule1).FullName);
Bind<Nancy.NancyModule>()
.ToMethod(context =>
{
return new MyModule2(context.Kernel.Get<IMongoRepository<Guid, Entity>>(
Properties.Settings.Default.NamedCollection3),
context.Kernel.Get<IMongoRepository<Guid, Entity>>(
Properties.Settings.Default.NamedCollection4));
})
.Named(typeof(MyModule2).FullName);
Nancy モジュールの構築を制御したい。Nancy のソースを調べたところ、nancy が適切なキーを持つタイプ NancyModule のすべての登録済みバインディングについて、構成済みの IoC コンテナーに要求するように見えます。以下のコードは、Nancy.Bootstrappers.Ninject アセンブリで定義されています。
protected override sealed NancyModule GetModuleByKey(IKernel container, string moduleKey)
{
return container.Get<NancyModule>(moduleKey);
}
キーは、キー ジェネレーター オブジェクトを使用して生成されているように見えます。
public class DefaultModuleKeyGenerator : IModuleKeyGenerator
{
/// <summary>
/// Returns a string key for the given type
/// </summary>
/// <param name="moduleType">NancyModule type</param>
/// <returns>String key</returns>
public string GetKeyForModuleType(Type moduleType)
{
return moduleType.FullName;
}
}
これが、バインディングが名前付きバインディングとして設定されている理由です。考え方は、Nancy が (モジュールの) 名前付きバインディングを要求すると、私の名前付きバインディングを選択するというものです。
期待どおりに機能していません。Ninject は、タイプ NancyModule に対して複数のバインディングが設定されていると不平を言っています。
繰り返しますが、私の目標は Nancy モジュールの構築を制御することです。
どんなアイデアでも大歓迎です。
PSモジュールに依存関係を注入することについて話すとき、私はこの質問のようにNinjectModulesを参照しません/参照しません