Jea*_*uis 6 asp.net-mvc ninject
我对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>());
Run Code Online (Sandbox Code Playgroud)
哪里
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望每个用户请求到我的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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2706 次 |
| 最近记录: |