首页 > 其他分享 >.net core 依赖注入 DependencyInjection

.net core 依赖注入 DependencyInjection

时间:2024-07-31 13:40:02浏览次数:10  
标签:core service get 源码 DependencyInjection net public

.net core 依赖注入源码位于.net core的runtime源码 中 ,其他源码信息 

.net core独立模块源码:https://github.com/aspnet

.net core全家桶源码:https://github.com/dotnet/aspnetcore

.net core拓展库源码:https://github.com/dotnet/extensions

.net core标准库源码:https://github.com/dotnet/corefx

.net core的EFCore源码:https://github.com/dotnet/efcore

.net core的SDK源码:https://github.com/dotnet/sdk

.net core的runtime源码:https://github.com/dotnet/runtime

依赖注入模块主要的2个类 IServiceCollection 和  IServiceProvider

 

1.IServiceCollection源码

 /// <summary>
 /// Specifies the contract for a collection of service descriptors.
 /// </summary>
 public interface IServiceCollection : IList<ServiceDescriptor>
 {
 }

 

  [DebuggerDisplay("{DebuggerToString(),nq}")]
  [DebuggerTypeProxy(typeof(ServiceCollectionDebugView))]
  public class ServiceCollection : IServiceCollection
  {
      private readonly List<ServiceDescriptor> _descriptors = new List<ServiceDescriptor>();
      private bool _isReadOnly;

     .....
  }
ServiceCollection中包含一组 ServiceDescriptor,用于记录注册类的描述信息,
 public class ServiceDescriptor

{


   private ServiceDescriptor(Type serviceType, object? serviceKey, ServiceLifetime lifetime)
   {
       Lifetime = lifetime;
       ServiceType = serviceType;
       ServiceKey = serviceKey;
   }

   /// <summary>
   /// Gets the <see cref="ServiceLifetime"/> of the service.
   /// </summary>
   public ServiceLifetime Lifetime { get; }

   /// <summary>
   /// Get the key of the service, if applicable.
   /// </summary>
   public object? ServiceKey { get; }

   /// <summary>
   /// Gets the <see cref="Type"/> of the service.
   /// </summary>
   public Type ServiceType { get; }

   [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
   private Type? _implementationType;

   /// <summary>
   /// Gets the <see cref="Type"/> that implements the service.
   /// </summary>
   [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
   public Type? ImplementationType
   {
       get
       {
           if (IsKeyedService)
           {
               ThrowKeyedDescriptor();
           }
           return _implementationType;
       }
   }

   /// <summary>
   /// Gets the <see cref="Type"/> that implements the service.
   /// </summary>
   [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
   public Type? KeyedImplementationType
   {
       get
       {
           if (!IsKeyedService)
           {
               ThrowNonKeyedDescriptor();
           }
           return _implementationType;
       }
   }

   private object? _implementationInstance;

   /// <summary>
   /// Gets the instance that implements the service.
   /// </summary>
   public object? ImplementationInstance
   {
       get
       {
           if (IsKeyedService)
           {
               ThrowKeyedDescriptor();
           }
           return _implementationInstance;
       }
   }

   /// <summary>
   /// Gets the instance that implements the service.
   /// </summary>
   public object? KeyedImplementationInstance
   {
       get
       {
           if (!IsKeyedService)
           {
               ThrowNonKeyedDescriptor();
           }
           return _implementationInstance;
       }
   }

   private object? _implementationFactory;

   /// <summary>
   /// Gets the factory used for creating service instances.
   /// </summary>
   public Func<IServiceProvider, object>? ImplementationFactory
   {
       get
       {
           if (IsKeyedService)
           {
               ThrowKeyedDescriptor();
           }
           return (Func<IServiceProvider, object>?) _implementationFactory;
       }
   }

   /// <summary>
   /// Gets the factory used for creating Keyed service instances.
   /// </summary>
   public Func<IServiceProvider, object?, object>? KeyedImplementationFactory
   {
       get
       {
           if (!IsKeyedService)
           {
               ThrowNonKeyedDescriptor();
           }
           return (Func<IServiceProvider, object?, object>?) _implementationFactory;
       }
   }

   /// <summary>
   /// Indicates whether the service is a keyed service.
   /// </summary>
   public bool IsKeyedService => ServiceKey != null;

}

 




标签:core,service,get,源码,DependencyInjection,net,public
From: https://www.cnblogs.com/871735097-/p/18334442

相关文章

  • 我的编程经历,从天桥地摊Basic到西藏阿里的.Net AOT。(一,从井到Sharp)
    撇清一层歧义:标题中的阿里不是指阿里巴巴集团,喜马拉雅也不是指那个做音频频道的公司,文中所及内容以及我本人都与他们没有任何关联。依照地理正式名称:阿里指的是西藏西部阿里地区,喜马拉雅指的是青藏高原地球最高山脉。 从前我在博客园不叫这个名字,今天很多自己的早期文章我都屏蔽......
  • 探索Amazon Bedrock:使用Claude 3.5 Sonnet进行图像理解与分析,实现图像生成、验证、再
    引言在之前的文章中,我们介绍了利用AnthropicClaude3.5Sonnet的图像理解与分析功能,通过StabilityAIStableDiffusionXL(SDXL)生成的图像在AmazonBedrock上进行验证和再生成的使用案例。使用Claude3.5Sonnet和StableDiffusionXL:如何通过AmazonBedrock不断优化图像......
  • Net8将Serilog日志推送ES,附视频
    这是一个Serilog的实践Demo,包括了区别记录存放,AOP日志记录,EF执行记录,并且将日志推送到ElasticSearch。说在前面的话自从AI出来之后,学习的曲线瞬间变缓了,学习的路径也有了很大的变化。与本人来说以前大多数都先知晓理论再找相关的框架官网或博客,然后去实践Demo,再加入到代码......
  • 充血模型的EFCore实现
    充血模型的五个需求属性是只读的或者只能被类内部的代码修改。publicPhoneNumberPhoneNumber{get;privateset;}publicvoidChangePassword(stringpassword){if(password.Length<=3){thrownewArgumentOutOfRangeException("密码长度需......
  • 核心(Hutool-core)日期时间工具-DateUtil
    转换Date、long、Calendar之间的相互转换//当前时间Datedate=DateUtil.date();//当前时间Datedate2=DateUtil.date(Calendar.getInstance());//当前时间Datedate3=DateUtil.date(System.currentTimeMillis());//当前时间字符串,格式:yyyy-MM-ddHH:mm:ssStringno......
  • .NET 开源快捷的数据库文档查询和生成工具
    前言在实际项目开发中,需求变更和项目迭代是常态。要求我们能够迅速响应,对数据库结构进行相应的调整,如添加新表、更新现有表结构或增加字段等。为了确保团队成员之间的信息同步,实时更新和维护数据库文档变得至关重要。这不仅提升了数据库的可读性,也极大提高了开发效率和团队协作......
  • Kubenetes集群部署操作
    服务器操作系统:CentOS7.9NAT192.168.1.1564核20Gmaster192.168.1.1101核8Gnode01192.168.1.1111核8Gnode02192.168.1.1121核8Gnode03环境准备关闭防火墙####关闭防火墙systemctlstopfirewalld&&systemctldisablefirewalld&&iptables-F......
  • 核心(Hutool-core)克隆工具cn.hutool.clone.CloneSupport
    一、直接继承extendsCloneSupport这个类就完事了/**狗狗类,用于继承CloneSupport类@authorLooly*/privatestaticclassDogextendsCloneSupport{privateStringname="wangwang";privateintage=3;}当然,使用CloneSupport的前提是你没有继承任何的类,谁让Java......
  • 核心(Hutool-core)类型转换Convert类
    Java常见类型转换转换为字符串:inta=1;//aStr为"1"StringaStr=Convert.toStr(a);long[]b={1,2,3,4,5};//bStr为:"[1,2,3,4,5]"StringbStr=Convert.toStr(b);转换为指定类型数组:String[]b={"1","2","3","4&q......
  • 1、.Net UI框架:WPF - .Net宣传系列文章
    WPF(WindowsPresentationFoundation)是微软提供的一个用于构建Windows应用程序的UI框架,它是.NETFramework的一部分,并且也支持.NETCore和.NET5/6等后续版本。WPF以其强大的数据绑定、样式化和图形功能而闻名,非常适合开发具有丰富视觉效果的现代应用程序。主要特点:分......