博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET Core 依赖注入-集成 Autofac
阅读量:4036 次
发布时间:2019-05-24

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

概述

ASP.NET Core 支持依赖关系注入 (DI) 软件设计模式,这是一种在类及其依赖关系之间实现控制反转 (IoC) 的技术。

默认服务容器是 Microsoft.Extensions.DependencyInjection 。

内置的服务容器一般能满足简单的框架和部分消费者应用的需求。 建议使用内置容器,除非你需要的特定功能不受内置容器支持,例如:

属性注入

基于名称的注入

子容器

自定义生存期管理

对迟缓初始化的 Func<T> 支持

基于约定的注册

而大部分情况下,实际项目中往往是比较复杂的,所以可以使用其他第三方IOC容器,如Autofac;

Autofac

Autofac 是.Net世界中最常用的依赖注入框架之一. 相比.Net Core标准的依赖注入库, 它提供了更多高级特性, 比如动态代理和属性注入.

优点: 

它是C#语言联系很紧密,也就是说C#里的很多编程方式都可以为Autofac使用,例如可以用Lambda表达式注册组件

较低的学习曲线,学习它非常的简单,只要你理解了IoC和DI的概念以及在何时需要使用它们

XML配置支持

自动装配

具体使用

1、项目中引用

2、准备好类、接口

using System.Collections.Generic;namespace AspNetCoreExample.Services{    public interface IValuesService    {        IEnumerable
FindAll(); string Find(int id); }}
using System.Collections.Generic;using Microsoft.Extensions.Logging;namespace AspNetCoreExample.Services{    public class ValuesService : IValuesService    {        private readonly ILogger
_logger; public ValuesService(ILogger
logger) { _logger = logger; } public IEnumerable
FindAll() { _logger.LogDebug("{method} called", nameof(FindAll)); return new[] { "value1", "value2" }; } public string Find(int id) { _logger.LogDebug("{method} called with {id}", nameof(Find), id); return $"value{id}"; } }}

3、替换掉内置的Ioc

using System;using System.Linq;using Autofac.Extensions.DependencyInjection;using Microsoft.AspNetCore;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;namespace AspNetCoreExample{    public class Program    {        public static void Main(string[] args)        {            // The ConfigureServices call here allows for            // ConfigureContainer to be supported in Startup with            // a strongly-typed ContainerBuilder. If you don't            // have the call to AddAutofac here, you won't get            // ConfigureContainer support. This also automatically            // calls Populate to put services you register during            // ConfigureServices into Autofac.            var host = WebHost.CreateDefaultBuilder(args)                .ConfigureServices(services => services.AddAutofac())                .UseStartup
() .Build(); host.Run(); } }}
using System;using System.Linq;using Autofac;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;namespace AspNetCoreExample{    // ASP.NET Core docs for Autofac are here:    // https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html    public class Startup    {        public Startup(IHostingEnvironment env)        {            var builder = new ConfigurationBuilder()                .SetBasePath(env.ContentRootPath)                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)                .AddEnvironmentVariables();            this.Configuration = builder.Build();        }        public IConfigurationRoot Configuration { get; private set; }        public void Configure(IApplicationBuilder app)        {            app.UseMvc();        }        public void ConfigureContainer(ContainerBuilder builder)        {            // Add any Autofac modules or registrations.            // This is called AFTER ConfigureServices so things you            // register here OVERRIDE things registered in ConfigureServices.            //            // You must have the call to AddAutofac in the Program.Main            // method or this won't be called.            builder.RegisterModule(new AutofacModule());        }        public void ConfigureServices(IServiceCollection services)        {            // Use extensions from libraries to register services in the            // collection. These will be automatically added to the            // Autofac container.            //            // Note if you have this method return an IServiceProvider            // then ConfigureContainer will not be called.            services.AddMvc();        }    }}

4、注入我们准备好的测试类、接口

using Autofac;using AspNetCoreExample.Services;using Microsoft.Extensions.Logging;namespace AspNetCoreExample{    public class AutofacModule : Module    {        protected override void Load(ContainerBuilder builder)        {            // The generic ILogger
service was added to the ServiceCollection by ASP.NET Core. // It was then registered with Autofac using the Populate method. All of this starts // with the services.AddAutofac() that happens in Program and registers Autofac // as the service provider. builder.Register(c => new ValuesService(c.Resolve
>())) .As
() .InstancePerLifetimeScope(); } }}

5、小试牛刀,用起来

using System.Collections.Generic;using AspNetCoreExample.Services;using Microsoft.AspNetCore.Mvc;namespace AspNetCoreExample.Controllers{    ///     /// Simple REST API controller that shows Autofac injecting dependencies.    ///     /// 
[Route("api/[controller]")] public class ValuesController : Controller { private readonly IValuesService _valuesService; public ValuesController(IValuesService valuesService) { this._valuesService = valuesService; } // GET api/values [HttpGet] public IEnumerable
Get() { return this._valuesService.FindAll(); } // GET api/values/5 [HttpGet("{id}")] public string Get(int id) { return this._valuesService.Find(id); } }}

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

你可能感兴趣的文章
idea讲web项目部署到tomcat,热部署
查看>>
优化IDEA启动速度,快了好多。后面有什么优化点,会继续往里面添加
查看>>
JMeter 保持sessionId
查看>>
IDEA Properties中文unicode转码问题
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
Maximum Subsequence Sum
查看>>
PTA:一元多项式的加乘运算
查看>>