我是Ninject的新手,我正在尝试使用MVC和
Linq的Ninject 2.我有一个sqlProductRepository类,我想知道的是,如果我在控制器中注入Repository对象,那么在构造函数中传递connectionstring的最佳方法是什么.
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; } } }
这是我的ProductController类,我注入了Repository:
public class ProductsController : Controller { private int pageSize = 4; public int PageSize { get { return pageSize; } set { pageSize = value; } } IProductRepository _productsRepository; [Inject] public ProductsController(IProductRepository productRepository) { _productsRepository = productRepository; } public ViewResult List(int page) { return View(_productsRepository.Products .Skip((page - 1) * pageSize) .Take(pageSize) .ToList() ); } }
有人可以指导我这个吗?
解决方法
您可以在绑定中进行设置
_kernel.Bind<IProductRepository>() .To<sqlProductRepository>() .WithConstructorArgument("connectionString",yourConnectionString );