我的 Ninject 有问题。
我的绑定规则:
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>());
Where
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。但我的解决方案不起作用。我究竟做错了什么? =(
您不需要指定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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)