zoukankan      html  css  js  c++  java
  • ASP.NET Core系列:依赖注入

    1. 控制反转(IoC)

      控制反转(Inversion of Control,IoC),是面向对象编程中的一种设计原则,用来降低代码之间的耦合度。

    1.1 依赖倒置

      依赖原则:

      (1)高层次的模块不应该依赖于低层次的模块,它们都应该依赖于抽象。

      (2)抽象不应该依赖于具体,具体应该依赖于抽象。

    1.2 依赖注入

      依赖注入:把依赖部分(代码不可控或者经常变动的耦合的部分)变成一个抽象的成员(类、抽象类或接口),然后根据具体所需要的实例去灵活的注入依赖,来达到控制反转的效果,从而实现代码解耦。

      依赖注入是控制反转的一种具体实现方式。

      C#常用的依赖注入方式:

      (1)通过构造器进行依赖注入

      (2)通过属性的访问器进行依赖注入

      (3)通过接口实现依赖注入

      (4)通过反射,特性也可以实现依赖注入

    2. ASP.NET Core自带实现依赖注入

      依赖注入安装包:

    Install-Package Microsoft.Extensions.DependencyInjection

      ASP.NET Core 提供了一个内置的服务容器 IServiceProvider,在 Startup.ConfigureServices 方法中配置依赖注入。

      ASP.NET Core 注册服务生存期:

      (1)AddTransient:暂时生存期,每次从服务容器进行请求时创建。

      (2)AddScoped:作用域生存期,每个客户端请求(连接)一次创建。

      (3)AddSingleton:单一实例生存期服务,在第一次请求时创建,每个后续请求都使用相同的实例。

      示例:

    public interface ILogRepository
    {
        int Insert(Log log);
    }
    ILogRepository
    public class LogRepository : ILogRepository
    {
        public int Insert(Log log)
        {
            using (var context = new PortalContext())
            {
                context.Logs.Add(new Log
                {
                    UserName = log.UserName,
                    Content = log.Content,
                    CreateTime = DateTime.Now
                });
    
                return context.SaveChanges();
            }
        }
    }
    LogRepository

      Startup.cs

    using Microsoft.Extensions.DependencyInjection;
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<ILogRepository, LogRepository>();
    
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

      LogController.cs

    [Route("api/[controller]")]
    [ApiController]
    public class LogController : ControllerBase
    {
        private readonly ILogRepository _logRepository;
    
        public LogController(ILogRepository logRepository)
        {
            _logRepository = logRepository;
        }
    
        [HttpPost]
        public void Post([FromBody] Log value)
        {
            _logRepository.Insert(value);
        }
    }

    3. ASP.NET Core使用Autofac实现依赖注入

      添加安装包:

    Install-Package Autofac.Extensions.DependencyInjection

      Startup.cs:

    using Autofac;
    using Autofac.Extensions.DependencyInjection;
    
    // 使用第三方容器,Startup.ConfigureServices 必须返回 IServiceProvider
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    
        // Autofac
        var builder = new ContainerBuilder();
        builder.RegisterType<LogRepository>().As<ILogRepository>();
        builder.Populate(services);
        var container = builder.Build();
        return new AutofacServiceProvider(container);
    }
  • 相关阅读:
    poj 2104 C
    2015 百度之星初赛 1 2 2015ACM/ICPC亚洲区上海站 codeforces 851
    3.10补
    3.9补
    3.8补
    3.6补
    3.5补
    3.4补
    3.3补
    2.35补
  • 原文地址:https://www.cnblogs.com/libingql/p/11417512.html
Copyright © 2011-2022 走看看