2010-12-20 6 views
6

J'ai un problème avec le Ninject.Ninject + ASP.NET MVC + InRequestScope

Mes règles contraignantes:

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 et IMySQLServer sont:

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

Je veux que chaque utilisateur demande à mon asp.net crée le site mvc un seul SQLServer, un seul ISQLLogger et un seul DatabaseConnections. Mais ma solution ne fonctionne pas. Qu'est-ce que je fais mal? . = (

Répondre

6

Vous n'avez pas besoin de spécifier le WithConstructorArgument les paramètres Résoudre les constructeurs de vos objets injectés fait partie de ce que Ninject ne vous donc les définitions devrait ressembler à ceci:.

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

Merci, cela m'a aidé avec un problème similaire. –

Questions connexes