【EF Core】实体类的依赖注入(转)

实体类也支持依赖注入,不过,目前版本只支持注入 EF Core 自己的服务类型,你在代码中添加的应用程序级服务类型不能注入(以后可能会支持)。总结一下,以下服务类型可以注入:

1、EF Core 框架内部注册的服务类型;

2、三个补充类型:

  a、当前 DbContext 实例(你从 DbContext 派生的类);

  b、当前实体相关的 IEntityType,可以获取实体模型相关信息;

  c、ILazyLoader 接口,延时加载时用得上。

现在要解决一个问题:我怎么知道 EF Core 框架内部哪些服务可以注入?这个活儿有两个方案:第一个方案是看 EF Core 源代码,在 EFCore\Infrastructure 下,注意看 EntityFrameworkServicesBuilder 类的 CoreServices 字段或 TryAddCoreServices 方法,基本齐全了。

    public static readonly IDictionary<Type, ServiceCharacteristics> CoreServices
        = new Dictionary<Type, ServiceCharacteristics>
        {
            { typeof(LoggingDefinitions), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IDatabaseProvider), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) },
            { typeof(IDbSetFinder), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IDbSetInitializer), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IDbSetSource), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IEntityFinderSource), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IStructuralTypeMaterializerSource), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(ITypeMappingSource), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IModelCustomizer), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IModelCacheKeyFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IModelSource), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IModelRuntimeInitializer), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IInternalEntrySubscriber), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IEntityEntryGraphIterator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IValueGeneratorCache), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(ISingletonOptionsInitializer), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(ILoggingOptions), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(ICoreSingletonOptions), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IModelValidator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(ICompiledQueryCache), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IValueConverterSelector), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IConstructorBindingFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IRegisteredServices), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IPropertyParameterBindingFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IParameterBindingFactories), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IMemberClassifier), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IMemoryCache), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IEvaluatableExpressionFilter), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(INavigationExpansionExtensibilityHelper), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(ILiftableConstantFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IExceptionDetector), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IJsonValueReaderWriterSource), new ServiceCharacteristics(ServiceLifetime.Singleton) },
            { typeof(IProviderConventionSetBuilder), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IConventionSetBuilder), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IDiagnosticsLogger<>), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IInterceptors), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(ILoggerFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IEntityGraphAttacher), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IKeyPropagator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(INavigationFixer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(ILocalViewListener), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IStateManager), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IConcurrencyDetector), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IInternalEntityEntryNotifier), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IValueGenerationManager), new ServiceCharacteristics(ServiceLifetime.Scoped) },
            { typeof(IChangeTrackerFactory), new Service
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

meslog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值