实体类也支持依赖注入,不过,目前版本只支持注入 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

最低0.47元/天 解锁文章
1623

被折叠的 条评论
为什么被折叠?



