您如何称呼 DI 用于填充模型的方法?
What do you call the method DI uses to populate a model?
IHost 的实现像通用主机一样进行依赖注入。您使用这样的辅助方法填充服务集合
IConfigurationSection section;
section = config.GetSection("SectionName");
services.Configure<SectionModel>(section);
在 Configure
辅助方法中,一些魔术创建了 SectionModel 的实例并从 IConfigurationSection
对象填充它。
那叫什么?我需要在没有 DI 的情况下执行此操作,虽然我确信翻阅 Microsoft 源代码可能会产生答案,但我可以使用执行此操作的方法的名称更快地翻阅。
他们很可能只使用反射,但我不想自己实现它并且可能有不同的行为。
根据 documentation 这称为 Bind
并出现在 IConfigurationSection
.
首先创建配置部分和模型对象,然后像这样调用 Bind
var sectionModel = new SectionModel();
config.GetSection("SectionName").Bind(sectionModel);
@Nkosi 评论
The same can be done using
var sectionModel = config.GetSection("SectionName").Get<SectionModel>();
这是使用泛型对同一解决方案进行更优雅的表达。
IHost 的实现像通用主机一样进行依赖注入。您使用这样的辅助方法填充服务集合
IConfigurationSection section;
section = config.GetSection("SectionName");
services.Configure<SectionModel>(section);
在 Configure
辅助方法中,一些魔术创建了 SectionModel 的实例并从 IConfigurationSection
对象填充它。
那叫什么?我需要在没有 DI 的情况下执行此操作,虽然我确信翻阅 Microsoft 源代码可能会产生答案,但我可以使用执行此操作的方法的名称更快地翻阅。
他们很可能只使用反射,但我不想自己实现它并且可能有不同的行为。
根据 documentation 这称为 Bind
并出现在 IConfigurationSection
.
首先创建配置部分和模型对象,然后像这样调用 Bind
var sectionModel = new SectionModel();
config.GetSection("SectionName").Bind(sectionModel);
@Nkosi 评论
The same can be done using
var sectionModel = config.GetSection("SectionName").Get<SectionModel>();
这是使用泛型对同一解决方案进行更优雅的表达。