排名
7
文章
192
粉丝
15
评论
16
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

前言
ASP.NET Core从框架层对依赖注入提供支持。也就是说,如果你不了解依赖注入,将很难适应 ASP.NET Core的开发模式。
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。
依赖注入示例
接口部分
- //先定义一个电影院的接口
- public interface ICinemaService
- {
- Task<IEnumerable<Cinema>> GetllAllAsync();//查询所有的Cinema值
- Task<Cinema> GetByIdAsync(int id);//查询指定ID的Cinema
- Task AddAsync(Cinema model);//创建Cinema值
- }
- //在定义一个电影的接口
- public interface IMovieService
- {
- Task AddAsync(Movie model);
- Task<IEnumerable<Movie>> GetByCinemaAsync(int cinemaId);
- }
实体类部分
- //电影院的实体
- public class Cinema
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Location { get; set; }//地址
- public int Capacity { get; set; }//容纳多少观众
- }
- //电影的实体
- public class Movie
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public int CinemaId { get; set; }
- public string Starring { get; set; }
- public DateTime ReleseDate { get; set; }
- }
- //售卖情况实体
- public class Sales
- {
- public int CinemaId { get; set; }
- public int MovieId { get; set; }
- public int AudienceCount { get; set; }//卖出多少票
- }
服务实体
- //电影院服务实体赋值
- public class CinemaMemoryService : ICinemaService
- {
- private readonly List<Cinema> _cinema = new List<Cinema>();
-
- public CinemaMemoryService()
- {
- _cinema.Add(new Cinema
- {
- Name = "天堂电影院",
- Location = "上海",
- Capacity = 1000
- });
- _cinema.Add(new Cinema
- {
- Name = "疯人电影院",
- Location = "北京",
- Capacity = 10000
- });
- }
-
- public Task<IEnumerable<Cinema>> GetllAllAsync()
- {
- return Task.Run(() => _cinema.AsEnumerable());
- }
-
- public Task<Cinema> GetByIdAsync(int id)
- {
- return Task.Run(() => _cinema.SingleOrDefault(x => x.Id == id));
- }
-
- public Task AddAsync(Cinema model)
- {
- var maxId = _cinema.Max(x => x.Id);
- model.Id = maxId + 1;
- _cinema.Add(model);
- return Task.CompletedTask;
- }
-
- }
- //电影实体服务赋值
- public class MovieMemoryService : IMovieService
- {
- private readonly List<Movie> _movies = new List<Movie>();
-
- public MovieMemoryService()
- {
- _movies.Add(new Movie
- {
- Id = 1,
- CinemaId = 1,
- Name = "这个杀手不太冷",
- ReleseDate = new DateTime(2018, 1, 1),
- Starring = "Tommy"
- });
- _movies.Add(new Movie
- {
- Id = 2,
- CinemaId = 2,
- Name = "笑傲江湖",
- ReleseDate = new DateTime(2018, 1, 1),
- Starring = "Tommy"
- });
- _movies.Add(new Movie
- {
- Id = 2,
- CinemaId = 2,
- Name = "小萝莉与猴神大叔",
- ReleseDate = new DateTime(2018, 1, 1),
- Starring = "Tommy"
- });
- }
-
- public Task<IEnumerable<Movie>> GetByCinemaAsync(int cinemaId)
- {
- return Task.Run(() => _movies.Where(x => x.CinemaId == cinemaId));
- }
-
- public Task AddAsync(Movie model)
- {
- var maxId = _movies.Max(x => x.Id);
-
- model.Id = maxId + 1;
- _movies.Add(model);
- return Task.CompletedTask;
- }
- }
添加好服务过后,在Startup 类中ConfigureServices 方法中添加刚刚写的两个服务
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllersWithViews();
- //添加刚刚新建的两个服务
- services.AddSingleton<ICinemaService, CinemaMemoryService>();
- services.AddSingleton<IMovieService, MovieMemoryService>();
- }
最后在控制器中使用 就OK了
- public class HomeController : Controller
- {
-
- private readonly ILogger<HomeController> _logger;
- private readonly ICinemaService _Cinema;
- private readonly IMovieService _Move;
- public HomeController(ILogger<HomeController> logger,ICinemaService Cinema, IMovieService Move)
- {
- _logger = logger;
- _Cinema = Cinema;
- _Move = Move;
- }
-
- public IActionResult Index()
- {
- return View();
- }
-
- public IActionResult Privacy()
- {
- return View();
- }
-
- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
- public IActionResult Error()
- {
- return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
- }
- }
这样一个依赖注入的流程就算是走完了
评价
剑轩
补充点
依赖注入的注入方式有:构造函数注入,属性注入,FromServices注入,手动获取注入HttpContext.RequestServices.GetService,视图注入@inject