博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET Core 中文文档 第四章 MVC(3.8)视图中的依赖注入
阅读量:7195 次
发布时间:2019-06-29

本文共 5340 字,大约阅读时间需要 17 分钟。

原文:

作者:
翻译:
校对:

ASP.NET Core 支持在视图中使用 。这将有助于提供视图专用的服务,比如本地化或者仅用于填充视图元素的数据。你应该尽量保持控制器和视图间的关注点分离()。你的视图所显示的大部分数据应该从控制器传入。

章节:

一个简单的示例

你可以使用 @inject 指令将服务注入到视图中。可以把 @inject 看作是给你的视图增加一个属性,然后利用依赖注入给这个属性赋值。

@inject 的语法:

@inject <type> <name>

一个使用 @inject 的例子:

@using System.Threading.Tasks@using ViewInjectSample.Model@using ViewInjectSample.Model.Services@model IEnumerable
@inject StatisticsService StatsService
To Do Items

To Do Items

  • Total Items: @StatsService.GetCount()
  • Completed: @StatsService.GetCompletedCount()
  • Avg. Priority: @StatsService.GetAveragePriority()
@foreach (var item in Model) {
}
Name Priority Is Done?
@item.Name @item.Priority @item.IsDone

这个视图显示了一个 ToDoItem 实例的列表和统计概览。概览信息是由注入的服务 StatisticsService 填入的。这个服务是在 Startup.cs 里的 ConfigureServices 方法中被注册到依赖注入项的。

// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940public void ConfigureServices(IServiceCollection services){    services.AddMvc();    services.AddTransient
(); services.AddTransient
(); services.AddTransient
();

StatisticsService 对通过仓储读取到的 ToDoItem 数据集执行一些计算。

using System.Linq;using ViewInjectSample.Interfaces;namespace ViewInjectSample.Model.Services{    public class StatisticsService    {        private readonly IToDoItemRepository _toDoItemRepository;        public StatisticsService(IToDoItemRepository toDoItemRepository)        {            _toDoItemRepository = toDoItemRepository;        }        public int GetCount()        {            return _toDoItemRepository.List().Count();        }        public int GetCompletedCount()        {            return _toDoItemRepository.List().Count(x => x.IsDone);        }        public double GetAveragePriority()        {            if (_toDoItemRepository.List().Count() == 0)            {                return 0.0;            }            return _toDoItemRepository.List().Average(x => x.Priority);        }    }}

示例中的仓储使用的是 in-memory 集合。上面的实现方法(所有对内存数据的操作)不推荐用于大型、远程存储的数据集。

这个示例通过绑定到视图的模型以及注入到视图的服务来显示数据:

959753-20160923100817168-73376219.png

填充查找数据

视图注入有助于填充 UI 元素中的选项数据,例如下拉列表。设想一个包括性别、州以及其他选项的用户资料表单。如果通过标准的 MVC 方式渲染这样一个表单,需要控制器为每一组选项都请求数据访问服务,然后将每一组待绑定的选项填充到模型或 ViewBag 中。

另一种方法则直接将服务注入到视图中以获取这些选项数据。这种方法将控制器需要的代码量减到了最少,把构造视图元素的逻辑移到视图本身中去。用来显示资料编辑表单的控制器 Action 只需要把用户资料实例传给表格就可以了(而不需要传各种选项数据,译注):

using Microsoft.AspNetCore.Mvc;using ViewInjectSample.Model;namespace ViewInjectSample.Controllers{    public class ProfileController : Controller    {        [Route("Profile")]        public IActionResult Index()        {            // TODO: look up profile based on logged-in user            var profile = new Profile()            {                Name = "Steve",                FavColor = "Blue",                Gender = "Male",                State = new State("Ohio","OH")            };            return View(profile);        }    }}

用来编辑这些选项的 HTML 表单包含选项中的三个下拉列表:

959753-20160923100851434-1589878758.png

这些列表是由注入到视图的一个服务填充的:

@using System.Threading.Tasks@using ViewInjectSample.Model.Services@model ViewInjectSample.Model.Profile@inject ProfileOptionsService Options    Update Profile

Update Profile

Name: @Html.TextBoxFor(m => m.Name)
Gender: @Html.DropDownList("Gender", Options.ListGenders().Select(g => new SelectListItem() { Text = g, Value = g }))
State: @Html.DropDownListFor(m => m.State.Code, Options.ListStates().Select(s => new SelectListItem() { Text = s.Name, Value = s.Code}))
Fav. Color: @Html.DropDownList("FavColor", Options.ListColors().Select(c => new SelectListItem() { Text = c, Value = c }))

ProfileOptionsService 是一个 UI 层的服务,旨在为这个表单提供所需数据 :

using System.Collections.Generic;namespace ViewInjectSample.Model.Services{    public class ProfileOptionsService    {        public List
ListGenders() { // keeping this simple return new List
() {"Female", "Male"}; } public List
ListStates() { // a few states from USA return new List
() { new State("Alabama", "AL"), new State("Alaska", "AK"), new State("Ohio", "OH") }; } public List
ListColors() { return new List
() { "Blue","Green","Red","Yellow" }; } }}

提示

不要忘记在 Startup.csConfigureServices 方法中把你想要通过依赖注入请求的类型注册一下。

重写服务

除了注入新服务之外,这种技术也可以用来重写之前已经在页面上注入的服务。下图显示了在第一个例子的页面上可用的所有字段:

959753-20160923100924418-1183660618.png

如你所见,默认的字段有 HtmlComponentUrl (同样还有我们注入的 StatsService)。假如你想要把默认的 HTML Helpers 替换成你自己的,你可以利用 @inject 轻松实现:

@using System.Threading.Tasks@using ViewInjectSample.Helpers@inject MyHtmlHelper Html    My Helper    
Test: @Html.Value

如果你想要扩展已有服务,你只需要在使用这种替换技术的同时,让你自己的服务继承或封装已有实现。

参考

  • Simon Timms 的博文:

转载地址:http://vltkm.baihongyu.com/

你可能感兴趣的文章
返回一个二维整数数组中最大联通子数组的和
查看>>
[学习笔记]阶和原根
查看>>
js事件委托
查看>>
计算机硬件
查看>>
gattAttribute_t 含义 中文解释
查看>>
jquery 选择器汇总
查看>>
Nodejs 学习资料
查看>>
设计模式(三) 抽象工厂模式
查看>>
Hbase之修改表结构
查看>>
通过浏览器学习前端的小技巧
查看>>
APP开发之AngularJS学习
查看>>
Sass:RGB颜色函数-Mix()函数
查看>>
phpMyAdmin 错误 缺少 mysqli 扩展。请检查 PHP 配置
查看>>
Win7网上邻居提示未授予用户在此计算机上的请求登录类型解决办法
查看>>
golang包快速生成base64验证码
查看>>
Visual studio 下C++工程相关经验
查看>>
七、SSR(服务端渲染)
查看>>
django--app(六)
查看>>
洛谷P3379 【模板】最近公共祖先(LCA)
查看>>
获取一个表单字段中多条数据并转化为json格式
查看>>