李大富

学习之路-Asp.core依赖注入(DI源码)学习1.0

首先声明英语好的人一定要学习源码,比较好理解。本人英语渣渣,学习比较慢

为什么要学习DI源码,因为本人写了个AOP组件,需要集成到DI。遇到了难题,所以先学DI源码,学习源码主要学习设计者的思维方式

下来步入正题在github下载源码地址 https://github.com/aspnet/DependencyInjection

 首先要学习标准也就是接口。才能明白其设计原理如下图

 

 主要有5个接口

public interface IServiceCollection : IList<ServiceDescriptor>
    {
    }
  这个接口简单定义了一个容器,容器里里面存放ServiceDescriptor(中文服务描述符)
/// <summary>
        /// Initializes a new instance of <see cref="ServiceDescriptor"/> with the specified <paramref name="factory"/>.
        /// </summary>
        /// <param name="serviceType">The <see cref="Type"/> of the service.</param>
        /// <param name="factory">A factory used for creating service instances.</param>
        /// <param name="lifetime">The <see cref="ServiceLifetime"/> of the service.</param>
        public ServiceDescriptor(
            Type serviceType,
            Func<IServiceProvider, object> factory,
            ServiceLifetime lifetime)
            : this(serviceType, lifetime)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            ImplementationFactory = factory;
        }

这个就是这个类的构造也很简单,从字面理解一个服务的描述者 里面用组合的思想包含了 serviceType(服务类型) lifetime(声明周期),以及一个委托类型工厂IserviceProvider(服务的供应商)

其实英语好了理解这个真的简单,至于这里为什么要用委托工厂暂时没有明白

现在看看这个接口虽然没有声明任何规范,但她继承 IList接口,符合他的规范 增加,删除,查询的规范。

感觉虽然接口简单 但是这个抽象的真的好,设计者以及达到对接口抽象大成了

  /// <summary>
    /// Provides an extension point for creating a container specific builder and an <see cref="IServiceProvider"/>.
    /// </summary>
    public interface IServiceProviderFactory<TContainerBuilder> --这是泛型接口
   { 
    /// <summary>
    /// Creates a container builder from an <see cref="IServiceCollection"/>.
    /// </summary> /// <param name="services">The collection of services</param>
    /// <returns>A container builder that can be used to create an <see cref="IServiceProvider"/>.</returns>
    TContainerBuilder CreateBuilder(IServiceCollection services);      
    从中可用创出一个容器生成器,DI的默认实现就是用这个接口规范什么都没干只返回参数。的具体的作用好处暂时还不能体会
    /// <summary>
    /// Creates an <see cref="IServiceProvider"/> from the container builder.
    /// </summary> /// <param name="containerBuilder">The container builder</param>
    /// <returns>An <see cref="IServiceProvider"/></returns>
   IServiceProvider CreateServiceProvider(TContainerBuilder containerBuilder);
提供一个服务供应商 ,没什么说的

这个接口的中文含义 服务供应商的工厂类型。工厂的作用主要是干什么?为什么要这样设计欢迎大家套论
}
    public interface IServiceScope : IDisposable
    {
        /// <summary>
        /// The <see cref="System.IServiceProvider"/> used to resolve dependencies from the scope.
        /// </summary>
        IServiceProvider ServiceProvider { get; }
    }
    public interface IServiceScopeFactory
    {
        /// <summary>
        /// Create an <see cref="Microsoft.Extensions.DependencyInjection.IServiceScope"/> which
        /// contains an <see cref="System.IServiceProvider"/> used to resolve dependencies from a
        /// newly created scope.
        /// </summary>
        /// <returns>
        /// An <see cref="Microsoft.Extensions.DependencyInjection.IServiceScope"/> controlling the
        /// lifetime of the scope. Once this is disposed, any scoped services that have been resolved
        /// from the <see cref="Microsoft.Extensions.DependencyInjection.IServiceScope.ServiceProvider"/>
        /// will also be disposed.
        /// </returns>
        IServiceScope CreateScope();
    }
 /// <summary>
    /// Optional contract used by <see cref="ServiceProviderServiceExtensions.GetRequiredService{T}(IServiceProvider)"/>
    /// to resolve services if supported by <see cref="IServiceProvider"/>.
    /// </summary>
    public interface ISupportRequiredService
    {
        /// <summary>
        /// Gets service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/> implementing
        /// this interface.
        /// </summary>
        /// <param name="serviceType">An object that specifies the type of service object to get.</param>
        /// <returns>A service object of type <paramref name="serviceType"/>.
        /// Throws an exception if the <see cref="IServiceProvider"/> cannot create the object.</returns>
        object GetRequiredService(Type serviceType);
    }

还有一个隐藏的重要接口

  //
    // 摘要:
    //     Defines a mechanism for retrieving a service object; that is, an object that
    //     provides custom support to other objects.
    public interface IServiceProvider
    {
        //
        // 摘要:
        //     Gets the service object of the specified type.
        //
        // 参数:
        //   serviceType:
        //     An object that specifies the type of service object to get.
        //
        // 返回结果:
        //     A service object of type serviceType. -or- null if there is no service object
        //     of type serviceType.
        object GetService(Type serviceType);
    }

 

posted on 2020-11-16 23:50  李大富  阅读(72)  评论(0编辑  收藏  举报

导航