DI框架有什么?

在上一节:手把手教你写DI_0_DI是什么?

我们已经理解DI是什么

接下来我们就徒手撸一撸,玩个支持构造函数注入的DI出来

首先我们回顾一下 构造函数注入 的代码形式, 大概长这模样:

class MovieLister
{
private IMovieFinder finder; public MovieLister(IMovieFinder finder) {
this.finder = finder;
}
}

那么我们就可以动手撸了

Emmmm...

等等,这个finder 从哪来? 我们自己new吗?自己new,我们还要DI干什么?又不是找对象

好吧,我们参照一下Microsoft.Extensions.DependencyInjection的设计,毕竟我们是小白,什么都不懂

其使用例子大致是这样:

var provider = new ServiceCollection()   // 声明服务定义集合,可以理解为 我们大家公司初创,要列出我们要招聘哪些人才干活

                .AddTransient<IXX,XX>()  // 添加一个瞬态的服务定义,也就是我们的招聘广告
// 对大家这些老板来说可以把 XX 这个“人”(类) 当成 IXX 这种“畜生”(接口)一样奴役,比如我们广告就是要个长得漂亮,手艺好,会IXX的搬砖工
// 瞬态可以理解成 我们的这份活儿比较危险,一个人干完一次之后就没法再干了,所以每次有活儿,我们都得重新招人 .BuildServiceProvider(); // 这是创建DI构造入口,可以理解成我们把招聘广告交给人才市场了,每次我们要干什么活儿,就可以打个电话叫人啦 var xx = provider.GetService(typeof(IXX)); // 等同于我们打个电话:喂,人才市场的蛇头吗? 我们要个会IXX的搬砖的。 等一会儿,蛇头就把会IXX送上我们的门啦
xx.DoSomethings(); // 等同于 xx 你去给我搬砖

哦哦哦,我们就是要撸这样的东西出来嗦

那我们看看每个都长啥鬼模样

ServiceCollection :


public class ServiceCollection : IServiceCollection
{
} //
// Summary:
// Specifies the contract for a collection of service descriptors.
public interface IServiceCollection : IList<ServiceDescriptor>, ICollection<ServiceDescriptor>, IEnumerable<ServiceDescriptor>, IEnumerable
{
}

哦哦,就是 ServiceDescriptor 集合嘛

AddTransient

public static IServiceCollection AddTransient<TService, TImplementation>(...

    var descriptor = new ServiceDescriptor(serviceType, implementationFactory, lifetime);
collection.Add(descriptor);
return collection;

哦哦,也就是 add ServiceDescriptor

ServiceDescriptor

public class ServiceDescriptor
{
public ServiceDescriptor(Type serviceType, Type implementationType, ServiceLifetime lifetime);
... //
// Summary:
// Specifies the lifetime of a service in an Microsoft.Extensions.DependencyInjection.IServiceCollection.
public enum ServiceLifetime
{
//
// Summary:
// Specifies that a single instance of the service will be created.
Singleton = 0,
//
// Summary:
// Specifies that a new instance of the service will be created for each scope.
//
// Remarks:
// In ASP.NET Core applications a scope is created around each server request.
Scoped = 1,
//
// Summary:
// Specifies that a new instance of the service will be created every time it is
// requested.
Transient = 2
}

哦哦哦,ServiceDescriptor 就是一些描述嘛

BuildServiceProvider 呢 ?

public static IServiceProvider BuildServiceProvider(this IServiceCollection services) {
...
return new ServiceProvider(xxx);
} //
// Summary:
// Defines a mechanism for retrieving a service object; that is, an object that
// provides custom support to other objects.
public interface IServiceProvider
{
//
// Summary:
// Gets the service object of the specified type.
//
// Parameters:
// serviceType:
// An object that specifies the type of service object to get.
//
// Returns:
// A service object of type serviceType. -or- null if there is no service object
// of type serviceType.
object GetService(Type serviceType);
}

索嘎,我们实现这些抽象就好啦

来,让我们思考一下,怎么撸成这些抽象的实现

下一章我们再讨论

下一章 徒手撸构造函数注入

手把手教你写DI_1_DI框架有什么?的更多相关文章

  1. Android开发之手把手教你写ButterKnife框架(三)

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52672188 本文出自:[余志强的博客] 一.概述 上一篇博客讲了, ...

  2. Android开发之手把手教你写ButterKnife框架(二)

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52664112 本文出自:[余志强的博客] 上一篇博客Android开 ...

  3. Android开发之手把手教你写ButterKnife框架(一)

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52662376 本文出自:[余志强的博客] 一.概述 JakeWhar ...

  4. 手把手教你写DI_2_小白徒手撸构造函数注入

    小白徒手撸构造函数注入 在上一节:手把手教你写DI_1_DI框架有什么? 我们已经知道我们要撸哪些东西了 那么我们开始动工吧,这里呢,我们找小白同学来表演下 小白同学 :我们先定义一下我们的广告招聘纸 ...

  5. 手把手教你写DI_0_DI是什么?

    DI是什么? Dependency Injection 常常简称为:DI. 它是实现控制反转(Inversion of Control – IoC)的一个模式. fowler 大大大神 "几 ...

  6. 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取

    版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 看完两篇,相信大家已经从开始的 ...

  7. [原创]手把手教你写网络爬虫(4):Scrapy入门

    手把手教你写网络爬虫(4) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 上期我们理性的分析了为什么要学习Scrapy,理由只有一个,那就是免费,一分钱都不用花! 咦?怎么有人扔西红柿 ...

  8. 网络编程懒人入门(八):手把手教你写基于TCP的Socket长连接

    本文原作者:“水晶虾饺”,原文由“玉刚说”写作平台提供写作赞助,原文版权归“玉刚说”微信公众号所有,即时通讯网收录时有改动. 1.引言 好多小白初次接触即时通讯(比如:IM或者消息推送应用)时,总是不 ...

  9. 手把手教你写Sublime中的Snippet

    手把手教你写Sublime中的Snippet Sublime Text号称最性感的编辑器, 并且越来越多人使用, 美观, 高效 关于如何使用Sublime text可以参考我的另一篇文章, 相信你会喜 ...

随机推荐

  1. linux: c语言 关闭标准输出STDOUT_FILENO对父子进程的影响

    简介标准 I/O 库(stdio)及其头文件 stdio.h 为底层 I/O 系统调用提供了一个通用的接口.这个库现在已经成为 ANSI 标准 C 的一部分.标准 I/O 库提供了许多复杂的函数用于格 ...

  2. Integer a=1,b=1,c=500,d=500;a==b,c==d;

    public class test { public static void main(String[] args){ Integer a=1,b=1,c=500,d=500; System.out. ...

  3. leetcode Reverse Nodes in k-Group翻转链表K个一组

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k  ...

  4. 全文思维导图------redis设计与实现

  5. Mysql之存储过程与存储函数

    1 存储过程 1.1 什么是存储过程 存储过程是一组为了完成某项特定功能的sql语句集,其实质上就是一段存储在数据库中的代码,他可以由声明式的sql语句(如CREATE,UPDATE,SELECT等语 ...

  6. redis-server文件启动cmd一闪而过

    工作上需要在本地装redis,所以就帮别人排查了一个问题,就是redis服务双击了之后不能起来,就是一个黑色的cmd框一闪而过,正常的是这样的: 然而,我当时第一次接触windows上的redis服务 ...

  7. 【Java从入门到精通】day08-包机制-JavaDoc生成文档

    1.包机制 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间. 包语句的语法格式为: package pkg1[.pkg2[.pkg3...]]; 一般利用公司域名倒置作为包名(如www ...

  8. SpringAop切面实现日志记录

    SpringAop切面实现日志记录代码实现:https://www.cnblogs.com/wenjunwei/p/9639909.html 问题记录 1.signature.getMethod(). ...

  9. com.aliyun.oss.ClientException: Connection error due to: Connection pool shut down

    com.aliyun.oss.ClientException: Connection error due to: Connection pool shut down[ErrorCode]: Unkno ...

  10. FL Studio进行侧链的三种方式(上)

    在本系列教程中,我们将学习如何在FL Studio中进行侧链.侧链是一种信号处理技术,通过它我们可以使用一个信号波形的振幅(音量)来控制另一个信号的某些参数.在电子音乐中,例如trance,house ...