2010-12-20 16 views
6

Tengo un problema con el Ninject.Ninject + ASP.NET MVC + InRequestScope

Mis reglas de enlace:

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

Dónde

SQL Server, ISphinxQLServer y IMySQLServer son:

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

Quiero que cada petición de usuario a mi sitio asp.net mvc crea un solo SQLServer, un solo ISQLLogger y una sola DatabaseConnections. Pero mi solución no funciona. ¿Qué estoy haciendo mal? . = (

Respuesta

6

No es necesario especificar el WithConstructorArgument La resolución de los parámetros a los constructores de los objetos inyectados es parte de lo que hace para usted Ninject Así las definiciones debe ser de la misma familia:.

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

Gracias, esto me ayudó con un problema similar. –

Cuestiones relacionadas