public ViewResult Index([FromServices] ProductTotalizer totalizer) { //IRepository repository = HttpContext.RequestServices.GetService<IRepository>(); ViewBag.HomeController = _repository.ToString(); ViewBag.Total = totalizer.Repository.ToString(); return(View(_repository.Products)); }
// GET: /<controller>/ public ViewResult Index([FromServices] ProductTotalizer totalizer) { ViewBag.HomeController = repository.ToString(); ViewBag.Totalizer = totalizer.Repository.ToString(); //ViewBag.Total = totalizer.Total; return(View(repository.Products)); }
public ViewResult Index([FromServices] ProductTotalizer totalizer) { IRepository repository = (IRepository)HttpContext.RequestServices.GetService(typeof(IRepository)); ViewBag.Totalizer = totalizer.Repository.ToString(); return(View(repository.Products)); }
//public ViewResult Index() //{ // ViewBag.HomeController = repository.ToString(); // ViewBag.Totalizer = totalizer.Repository.ToString(); // return View(repository.Products); //} // ACTION INJECTION public ViewResult Index([FromServices] ProductTotalizer totalizer) { IRepository repository = (IRepository)HttpContext.RequestServices.GetService(typeof(IRepository)); // service locator pattern ViewBag.Repository = repository.ToString(); ViewBag.RepositoryDirectlyFromServices = this.repository.ToString(); ViewBag.Totalizer = totalizer.Repository.ToString(); ViewBag.TotalizerFromActionInjection = this.totalizer.Repository.ToString(); return(View(repository.Products)); }
public void ControllerTest() { // Arrange decimal total = 100; var data = new[] { new Product { Name = "Test", Price = total } }; var mockIRepository = new Mock <IRepository>(); mockIRepository.SetupGet(m => m.Products).Returns(data); ProductTotalizer totalizer = new ProductTotalizer(mockIRepository.Object); HomeController controller = new HomeController(mockIRepository.Object); // Act ViewResult result = controller.Index(totalizer); // Assert Assert.Equal(data, result.ViewData.Model); Assert.Equal(total, result.ViewData["Total"]); }
public HomeController(IRepository repo, ProductTotalizer total) { repository = repo; totalizer = total; }
private ProductTotalizer totalizator; // зачем мы создаем екземпляр а не используем тот что передается в конструкторе public HomeController(IRepository repo, ProductTotalizer total) { repository = repo; //зачем и как оно работает totalizator = total; }
public HomeController(IProductRepository repository, ProductTotalizer totalizer) { this._repository = repository; this._totalizer = totalizer; }
public Object GetAll([FromServices] ProductTotalizer product) //控制器动作注入 { IRepository repository = HttpContext.RequestServices.GetService <IRepository>(); //属性注入 return(new { IRepository = _repo.ToString(), ProductTotalizer = product.ToString() }); }
public IActionResult Index([FromServices] ProductTotalizer totalizer) { ViewBag.Total = totalizer.Total; return(View(_repository.Products)); }
public InjectionController(IRepository repository, ProductTotalizer totalizer) { _repository = repository; _totalizer = totalizer; }