zoukankan      html  css  js  c++  java
  • .Net Core 依赖注入(Dependency injection)

    一、简介

      依赖注入是一种实现对象及基合作者或者依赖项之间松散耦合的技术;

    二、代码实现

      (1)、定义接口

    namespace WebApplication1.Services
    {
        public interface IBookService
        {
            /// <summary>
            /// 获取描述
            /// </summary>
            /// <returns></returns>
            string GetDescrption();
        }
    }

      (2)、实现接口

    namespace WebApplication1.Services
    {
        public class BookServices : IBookService
        {
            public string GetDescrption()
            {
                var desc = "获取书本描述!";
                return desc;
            }
        }
    }

      (3)、在 Startup.css ConfigureServices 方法中配置引用

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
    
                //添加权限认证
                services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                    .AddCookie(
                    CookieAuthenticationDefaults.AuthenticationScheme, o =>
                    {
                        o.LoginPath = new PathString("/Login/Index");
                    });
    
                //第一种方式:调用创建一个实例
                services.AddTransient<IBookService, BookServices>();
                
                //第二种方式:一个请求域一个实例
                services.AddScoped<IBookService,BookServices>();
                
                //第三种方式:单例模式
                services.AddSingleton<IBookService,BookServices>();
            }

      (4)、注入使用

    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Services;
    
    namespace WebApplication1.Controllers
    {
        public class BookController : Controller
        {
            // 定义接口
            private readonly IBookService _bookService;
    
            //注入接口
            public BookController(IBookService bookService)
            {
                _bookService = bookService;
            }
            public IActionResult Index()
            {
                //调用方法
                var desc = _bookService.GetDescrption();
    
                return Content("");
            }
        }
    }

      (5)、实验截图

      (6)、安装 SqlServer 链接需要的安装包。

      首先需要安装  Microsoft.EntityFrameworkCore 和 Microsoft.EntityFrameworkCore.SqlServer 

      (7)、编写 CommonDbContext.cs 

    using Microsoft.EntityFrameworkCore;
    
    namespace WebApplication1
    {
        public class CommonDbContext : DbContext
        {
            public CommonDbContext(DbContextOptions options) : 
                base(options)
            {
            }
        }
    }

      (8)、在 Startup.cs 中添加注入

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
    
                //添加权限认证
                services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                    .AddCookie(
                    CookieAuthenticationDefaults.AuthenticationScheme, o =>
                    {
                        o.LoginPath = new PathString("/Login/Index");
                    });
    
                //调用创建一个实例
                services.AddTransient<IBookService, BookServices>();
    
                //一个请求域一个实例
                // services.AddScoped<IBookService,BookServices>();
    
                //单例模式
                // services.AddSingleton<IBookService,BookServices>();
    
                services.AddDbContextPool<CommonDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            }

      (9)、使用 CommonDbContext 

    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Services;
    
    namespace WebApplication1.Controllers
    {
        public class BookController : Controller
        {
            // 定义接口
            private readonly IBookService _bookService;
            private readonly CommonDbContext _commonDbContext;
    
            //注入接口
            public BookController(IBookService bookService, CommonDbContext commonDbContext)
            {
                _bookService = bookService;
                _commonDbContext = commonDbContext;
            }
            public IActionResult Index()
            {
                //调用方法
                var desc = _bookService.GetDescrption();
    
                return Content(desc);
            }
        }
    }
  • 相关阅读:
    React 组件生命周期
    React Ant Design Mobile 中 ListView 简单使用,搞懂每一行代码
    .net网站自动化部署-致两年前的遗留的问题
    【479】cross-entropy与softmax的求导
    【478】Victor Zhou深度学习链接
    网站高并发大流量访问的处理及解决方案
    年轻人的第一个 Docker 应用,大大提高生产力!
    Spring Boot 静态资源处理,妙!
    Spring 配置最好不要配置 xsd 版本号!
    自己动手实现一个简单的 IOC,牛皮!!
  • 原文地址:https://www.cnblogs.com/gzbit-zxx/p/13835208.html
Copyright © 2011-2022 走看看