首页 技术 正文
技术 2022年11月10日
0 收藏 492 点赞 4,975 浏览 3275 个字

目录

1、DI&&IOC简单介绍

2、UML类图中六种关联关系

3、.net core 中DI的使用

4、.net core DI初始化源码初窥

DI&&IOC简单介绍

  DI(依赖注入)是实现IOC(控制反转)的一种方式。面向对象设计六大基本原则的依赖倒置原则,高层类不应该依赖于低层类的实现 ,而应该依赖于它的抽象。所以我们现在工作中,经常是构造函数中,注入需要实现的类的接口;IOC描述的是当一个类需要另外一个类时,这个类的实现不应该由它来决定,实现由一个DI容器来实现,并将其注入到这个类中。

UML类图中六种关联关系

  顺便复习一波UML类图中的六种关系:

Core篇——初探依赖注入

.net core 中DI的使用

  .NET Core框架中,默认存在一个DI容器,使用的时候我们只需要将需要将接口服务放入DI容器。 这里,我们创建一个Movie类,引用efcore 添加一个数据库链接上下文。添加一个IMovieService 接口和MovieService 类。代码如下所示

  

    public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
:base(options)
{ }
public DbSet<Movie> Movie { get; set; } }

数据库链接上下文

     public class Startup
{
public IConfiguration Configuration { get; set; } public Startup()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json"); //将appsettings 配置文件读入系统配置 Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("MVCMovieContext")); //添加数据库链接字符串
});
services.AddRouting();
services.AddMvc();
services.AddScoped<IMovieService, MovieService>();//接口服务放入DI容器
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}

Startup类

     public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}

Movie类

     public interface IMovieService
{
Movie GetMovie(int Id);
}
public class MovieService : IMovieService
{
private readonly ApplicationDbContext _applicationDbContext; public MovieService(ApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
}
public Movie GetMovie(int Id)
{
var entity = _applicationDbContext.Movie.FirstOrDefault(t => t.ID == Id);
if (entity == null)
return null;
return entity;
}
}

接口以及服务

     public class HomeController : Controller
{
private readonly IMovieService _movieService;
public HomeController(IMovieService movieService)
{
_movieService = movieService;
}
public IActionResult Index(int Id)
{
var movie = _movieService.GetMovie(Id);
return View(movie);
}
}

Home控制器代码

Core篇——初探依赖注入

  

这里我们在MovieService中,注入了数据库链接上下文实例;在HomeController 中,注入了IMovieService 。

.net core DI初始化源码初窥

Core篇——初探依赖注入

  在 .Net Core 框架的Main函数中,

  1、WebHost 通过CreateDefaultBuilder 创建一个WebHostBuilder实例。WebHostBuilder调用 Build方法,在Build方法中,调用BuildCommonServices方法返回一个IServiceCollection。这个ServiceCollection是在BuildCommonServices方法中,实例化的一个,然后在实例化的ServiceCollection中加入一些默认的配置(例如HttpContextFactory,IHostingEnvironment)。

拿到IServiceCollection对象后,在Build方法中,对IServiceCollection IServiceCollectionIServiceCollection.ServiceProvider 进行一个初始化操作(Initialize)方法,然后返回WebHost。

  2、在Initialize方法中,执行EnsureApplicationServices方法,EnsureApplicationServices中又通过调用EnsureStartup方法,在EnsureStartup中,通过WebHostProvider拿到StartUp实例。EnsureApplicationServices拿到StartUp后调用ConfigureServices方法,参数是ServiceCollection来完成DI。(这也就是我们新建一个项目StartUp 中的ConfigureServices方法,然后各种services.addxxxx(),把接口和服务放入到IserviceCollection)

Jesse博客学习笔记。传送门=》 http://video.jessetalk.cn/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,157
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,625
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,469
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,241
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,878
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,044