程序员

详解ASP.NET Core 中的框架级依赖注入

作者:admin 2021-06-24 我要评论

1、ASP.NET Core 中的依赖注入 此示例展示了框架级依赖注入如何在 ASP.NET Core 中工作。 其简单但功能强大,足以完成大部分的依赖注入工作。框架级依赖注入支持...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

1、ASP.NET Core 中的依赖注入

此示例展示了框架级依赖注入如何在 ASP.NET Core 中工作。 其简单但功能强大,足以完成大部分的依赖注入工作。框架级依赖注入支持以下 scope:

  1. Singleton — 总是返回相同的实例
  2. Transient — 每次都返回新的实例
  3. Scoped — 在当前(request)范围内返回相同的实例

假设我们有两个要通过依赖注入来进行工作的工件:

  1. PageContext — 自定义请求上下文
  2. Settings — 全局应用程序设置

这两个都是非常简单的类。PageContext 类为布局页面提供当前页面标题的标题标签。

public class Settings 
{
 public string SiteName;
 public string ConnectionString;
}
public class PageContext
{
  private readonly Settings _settings;
  public PageContext(Settings settings)
  {
    _settings = settings;
  }
  public string PageTitle;
  public string FullTitle
  {
    get
    {
      var title = (PageTitle ?? "").Trim(); 
      if(!string.IsNullOrWhiteSpace(title) &&
        !string.IsNullOrWhiteSpace(_settings.SiteName))
      {
        title += " | ";
      }
      title += _settings.SiteName.Trim();
      return title;
    }
  }
}

2、注册依赖

在 UI 构建块中使用这些类之前,需要在应用程序启动时注册这些类。该工作可以在 Startup 类的 ConfigureServices() 方法中完成。

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  var settings = new Settings();
  settings.SiteName = Configuration["SiteName"];
  services.AddSingleton(settings);
  services.AddScoped<PageContext>();
}

现在可以将这些类注入到支持依赖注入的控制器和其他 UI 组件中。

3、向控制器注入实例

我们通过 Home 控制器中的 PageContext 类分配页面标题。

public class HomeController : Controller
{
  private readonly PageContext _pageContext;
  public HomeController(PageContext pageContext)
  {
    _pageContext = pageContext;
  }
  public IActionResult Index()
  {
    _pageContext.PageTitle = "";
    return View();
  }
  public IActionResult About()
  {
    _pageContext.PageTitle = "About";
    return View();
  }
  public IActionResult Error()
  {
    _pageContext.PageTitle = "Error";
 
    return View();
  }
}

这种分配页面标题的方式不错,因为我们不必使用 ViewData,这样更容易受支持多语言应用程序支持。

4、向视图注入实例

现在控制器的 action 中分配了页面标题,是时候在布局页面中使用标题了。 我在页面的内容区域添加了标题,所以在 tech.io 环境中也很容易看到。为了能在布局页面中使用到 PageContext,我使用了视图注入(下面代码片段中的第一行)。

@inject PageContext pageContext
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>@pageContext.FullTitle</title>
  <environment names="Development">
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" rel="external nofollow" />
    <link rel="stylesheet" href="~/css/site.css" rel="external nofollow" />
  </environment>
  <environment names="Staging,Production">
    <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" 
       asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css" rel="external nofollow" 
       asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
    <link rel="stylesheet" href="~/css/site.min.css" rel="external nofollow" asp-append-version="true" />
  </environment>
</head>
...
</html>

5、参考材料

ASP.NET 5 中的依赖注入(Gunnar Peipman)
ASP.NET Core:使用视图注入(Gunnar Peipman)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持站长技术。


原文链接:https://m.jb51.net/article/126646.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 阿里巴巴DevOps实践指南(八)| 以特性

    阿里巴巴DevOps实践指南(八)| 以特性

  • 阿里巴巴DevOps实践指南(五)| 业务驱

    阿里巴巴DevOps实践指南(五)| 业务驱

  • RISC-V工具链简介

    RISC-V工具链简介

  • 变局时代:RISC-V处理器架构的技术演变

    变局时代:RISC-V处理器架构的技术演变