【ABP 源码学习】依赖注入部分

2022年6月24日 1646点热度 1人点赞 0条评论
内容纲要

1,
IServiceCollection 可以被替换服务。
ServiceDescriptor 可以通过 .Scoped() 等生成要注入的自定义服务。

    public static IServiceCollection AddAbpDynamicOptions<TOptions, TManager>(this IServiceCollection services)
        where TOptions : class
        where TManager : AbpDynamicOptionsManager<TOptions>
    {
        services.Replace(ServiceDescriptor.Scoped(typeof(IOptions<TOptions>), typeof(TManager)));
        services.Replace(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<TOptions>), typeof(TManager)));

        return services;
    }

2,
将项目插入到指定位置上。
ABP 为了能够将要用的服务快速检索,通过 Insert 将服务注入插到容器的开头位置。


        //Add to the beginning for fast retrieve
        services.Insert(0, ServiceDescriptor.Singleton(typeof(ObjectAccessor<T>), accessor));
        services.Insert(0, ServiceDescriptor.Singleton(typeof(IObjectAccessor<T>), accessor));

痴者工良

高级程序员劝退师

文章评论