2010-12-20 84 views
6

我對Ninject有問題。Ninject + ASP.NET MVC + InRequestScope

我有約束力的規則:

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

SQLSERVER,ISphinxQLServer和IMySQLServer是:

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

謝謝,這幫助我解決了類似的問題。 – 2011-03-24 18:38:13