2010-12-20 15 views
6

私はNinjectに問題があります。Ninject + ASP.NET MVC + InRequestScope

マイ結合規則:

のSQLServer、ISphinxQLServerとIMySQLServerがある

this.Bind<ISphinxQLServer>().To<SQLServer>(); this.Bind<IMySQLServer>().To<SQLServer>(); this.Bind<ISQLLogger>().To<StandardSQLLogger>() .InRequestScope(); this.Bind<DatabaseConnections>() .ToMethod(x => ConnectionFactory.GetConnections()) .InRequestScope(); this.Bind<SQLServer>().ToSelf() .InRequestScope() .WithConstructorArgument("connections", Kernel.Get<DatabaseConnections>()) .WithConstructorArgument("logger", Kernel.Get<ISQLLogger>()); 

public class SQLServer: ISphinxQLServer, IMySQLServer 
{ 
    public DatabaseConnections Connections { get; internal set; } 
    public ISQLLogger Logger { get; internal set; } 

    public SQLServer(DatabaseConnections connections) 
    { 
     this.Connections = connections; 
    } 

    public SQLServer(DatabaseConnections connections, ISQLLogger logger) 
    { 
     this.Connections = connections; 
     this.Logger = logger; 
    } 
} 

私は私のasp.net MVCのサイトに、各ユーザー要求が作成する必要単一のSQLServer、単一のISQLLogger、および単一のDatabaseConnectionsが含まれます。しかし、私の解決策は動作しません。私は間違って何をしていますか? =(

答えて

6

あなたはWithConstructorArgumentを指定する必要はありません、あなたの注入されたオブジェクトのコンストラクタにパラメータを解決するので、定義がより次のようになりますNinjectがあなたのために何をするかの一部です:。

this.Bind<SQLServer>() 
    .ToSelf() 
    .InRequestScope(); 

this.Bind<ISphinxQLServer>() 
    .ToMethod(x => x.Kernel.Get<SQLServer>()); 

this.Bind<IMySQLServer>() 
    .ToMethod(x => x.Kernel.Get<SQLServer>()); 

this.Bind<ISQLLogger>() 
    .To<StandardSQLLogger>() 
    .InRequestScope(); 

this.Bind<DatabaseConnections>() 
    .ToMethod(x => ConnectionFactory.GetConnections()) 
    .InRequestScope(); 
+0

。ありがとう、これは同様の問題で私を助けた。 –

関連する問題