Asp.net mvc Ninject和连接字符串
我对Ninject非常陌生,正在尝试使用MVC和Linq的Ninject 2。我有一个SqlProductRepository类,我想知道的是,如果我在控制器中注入存储库对象,那么在构造函数中传递connectionstring的最佳方式是什么Asp.net mvc Ninject和连接字符串,asp.net-mvc,linq,ioc-container,ninject,Asp.net Mvc,Linq,Ioc Container,Ninject,我对Ninject非常陌生,正在尝试使用MVC和Linq的Ninject 2。我有一个SqlProductRepository类,我想知道的是,如果我在控制器中注入存储库对象,那么在构造函数中传递connectionstring的最佳方式是什么 public class SqlProductRepository:IProductRepository { private Table<Product> productsTable; public SqlProductRe
public class SqlProductRepository:IProductRepository
{
private Table<Product> productsTable;
public SqlProductRepository(string connectionString)
{
productsTable = (new DataContext(connectionString)).GetTable<Product>();
}
public IQueryable<Product> Products
{
get { return productsTable; }
}
}
有人能给我指点一下吗 您可以在绑定中设置它
_kernel.Bind<IProductRepository>()
.To<SqlProductRepository>()
.WithConstructorArgument("connectionString",yourConnectionString );
_kernel.Bind()
.至()
.使用Constructor参数(“connectionString”,即您的connectionString);
您正在做:
new DataContext(connectionString)
在您的代码中-这就是您试图通过使用DI容器从代码中推出的类的更新和绑定。至少,请考虑添加一个<代码> ICONNECTSTRIN选择器< /代码>接口或类似的东西。您不希望有20个Bind
调用来调用20个存储库——您需要的是更高级别的抽象
我建议最好的解决方案是,您应该在构造函数中要求一个
IDataContext
或一个IDataContextFactory
,并让它担心。在将SqlProductRepository
绑定到IPProductRepository
接口
public class LinqToSqlModule : NinjectModule
{
public override void Load()
{
Bind<IProductRepository>().To<SqlProductRepository>()
.WithConstructorArgument(connectionString, "connectionstring");
}
}
接下来修改SqlProductRepository
类的构造函数以接受DataContext
对象
public class SqlProductRepository : IProductRepository
{
private readonly DataContext context;
public ProductRepository(DataContext context)
{
this.context = context;
}
public IQueryable<Product> Products
{
get { return context.GetTable<Product>(); }
}
}
公共类SqlProductRepository:IPProductRepository
{
私有只读数据上下文;
公共产品存储库(DataContext上下文)
{
this.context=上下文;
}
公共卫生产品
{
获取{return context.GetTable();}
}
}
顺便说一句,您不必用
Inject
属性修饰构造函数。默认情况下,Ninject将选择参数最多的构造函数。请参阅下面的代码快照:
//Bind the default connection string
public void BindDataContext()
{
ConstructorArgument parameter = new ConstructorArgument("connectionString", "[Config Value]");
Bind<DataContext>().ToSelf().InRequestScope().WithParameter(parameter);
}
//Re-Bind the connection string (in case of multi-tenant architecture)
public void ReBindDataContext(string cn)
{
ConstructorArgument parameter = new ConstructorArgument("connectionString", cn);
Rebind<DataContext>().ToSelf().InRequestScope().WithParameter(parameter);
}
//绑定默认连接字符串
public void BindDataContext()
{
ConstructorArgument参数=新ConstructorArgument(“connectionString”,“配置值]”);
Bind().ToSelf().InRequestScope().WithParameter(参数);
}
//重新绑定连接字符串(在多租户体系结构的情况下)
public void ReBindDataContext(字符串cn)
{
ConstructorArgument参数=新的ConstructorArgument(“connectionString”,cn);
在RequestScope()中使用参数重新绑定().ToSelf().WithParameter(参数);
}
欲了解更多信息,请访问以下链接
public class SqlProductRepository : IProductRepository
{
private readonly DataContext context;
public ProductRepository(DataContext context)
{
this.context = context;
}
public IQueryable<Product> Products
{
get { return context.GetTable<Product>(); }
}
}
//Bind the default connection string
public void BindDataContext()
{
ConstructorArgument parameter = new ConstructorArgument("connectionString", "[Config Value]");
Bind<DataContext>().ToSelf().InRequestScope().WithParameter(parameter);
}
//Re-Bind the connection string (in case of multi-tenant architecture)
public void ReBindDataContext(string cn)
{
ConstructorArgument parameter = new ConstructorArgument("connectionString", cn);
Rebind<DataContext>().ToSelf().InRequestScope().WithParameter(parameter);
}