2010-12-20 11 views
6

Mam problem z Ninject.Ninject + ASP.NET MVC + InRequestScope

Moi wiążące zasady:

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

Gdzie

SQLServer, ISphinxQLServer i IMySQLServer to:

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; 
    } 
} 

Chcę, aby każdy żądanie użytkownika do mojego ASP.NET MVC miejscu tworzy pojedynczy SQLServer, pojedynczy ISQLLogger i pojedyncze DatabaseConnections. Ale moje rozwiązanie nie działa. Co ja robię źle? . = (

Odpowiedz

6

Nie trzeba określić WithConstructorArgument Rozwiązywanie parametry dla konstruktorów firmy wstrzyknięto obiektów jest częścią tego, co Ninject ma dla ciebie Więc definicje powinny wyglądać mniej więcej tak:.

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

Dzięki, pomogło mi to z podobnym problemem. –

Powiązane problemy