手把手教你写DI_1_DI框架有什么?

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);
}

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

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

下一章我们再讨论

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

猜你喜欢

转载自www.cnblogs.com/fs7744/p/9924569.html