1

コンストラクター引数「データベース」を受け入れるタイプIRoleRepositoryがあり、それ自体がコンストラクター引数「ConnectionStringName」をとるIDbRepositoryのタイプを受け入れます。GetServiceメソッドを持つ依存関係リゾルバーがあります。次のコードは機能しますが、バインド時とNinject3.0のGet時よりも良い方法があることを期待していました。それぞれが独自の「ConnectionStringName」を持つ複数のIDBRepositoryインスタンスがある場合があることに注意してください。

_repository = EngineContext.Current.GetService<IRoleRepository>(
                        new ConstructorArgument("database",
                            EngineContext.Current.GetService<IDbRepository>(
                                new ConstructorArgument(SystemConstants.ConnectionStringName, SystemConstants.ConfigurationDatabase))));
4

2 に答える 2

2

WithConstructorArgumentバインディングと一緒にコンストラクター引数を指定するために使用できます。

kernel.Bind<IDbRepository>().To<DbRepository>()
      .WithConstructorArgument(
           SystemConstants.ConnectionStringName, 
           SystemConstants.ConfigurationDatabase);

またはToConstructor()を使用します

kernel.Bind<IDbRepository>().ToConstructor(
    x => new DbRepository(
             SystemConstants.ConfigurationDatabase, 
             x.Inject<ISomeOtherDependency>())
于 2012-04-13T13:48:01.227 に答える
0

OK私は私が欲しいものを見つけたと信じています:

バインド時にこれを使用することにより:

            Bind<IDbRepository>().To<SqlServerRepository>()
            .WhenInjectedInto<IRoleRepository>()
            .WithConstructorArgument(SystemConstants.ConnectionStringName, SystemConstants.ConfigurationDatabase);

これにより、Gettimeでこれを使用できます。

_repository = EngineContext.Current.GetService<IRoleRepository>();

これはもちろん、IDbRepositoryが注入されているより具体的なリポジトリに基づいてIDbRepositoryのコンストラクター引数を変更できることを意味します。例えば:

            Bind<IDbRepository>().To<SqlServerRepository>()
            .WhenInjectedInto<ITimerJobStore>()
                .WithConstructorArgument(SystemConstants.ConnectionStringName, SystemConstants.ConfigurationDatabase);

        Bind<ITimerJobStore>().To<TimerJobSqlStore>();
于 2012-04-13T14:08:20.563 に答える