博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET手记-Autofac进阶(传递注册参数 Passing Parameters to Register)
阅读量:4626 次
发布时间:2019-06-09

本文共 2093 字,大约阅读时间需要 6 分钟。

当你注册组件时,可以为组件服务传入一系列参数,用于服务解析时使用。

 

可使用的参数类型 Available Parameter Types

 

Autofac提供了集中参数匹配类别:

  • NamedParameter - 直接通过名称匹配目标参数
  • TypedParameter - 通过类型来匹配目标参数
  • ResolvedParameter - 灵活的参数匹配

NamedParameter 和 TypedParameter只支持常量。

ResolvedParameter 能够使用从容器中动态解析的值作为参数, 例如通过名字解析出的服务。

 

反射组件的参数 Parameters with Reflection Components

 

当你注册基于反射的组件时,类型构造函数可能要求一些无法从容器中解析的参数。你可以通过直接提供此参数来完成组件注册。

假设你有一个配置阅读器(configuration reader),同时需要为它传入配置块命名(configuration section name):

 

public class ConfigReader : IConfigReader{  public ConfigReader(string configSectionName)  {    // Store config section name  }  // ...read configuration based on the section name.}

 

你可能会使用Lambda表达式来实现:

 

builder.Register(c => new ConfigReader("sectionName")).As
();

 

或者为反射组件注册时传入参数:

 

// Using a NAMED parameter:builder.RegisterType
() .As
() .WithParameter("configSectionName", "sectionName");// Using a TYPED parameter:builder.RegisterType
() .As
() .WithParameter(new TypedParameter(typeof(string), "sectionName"));// Using a RESOLVED parameter:builder.RegisterType
() .As
() .WithParameter( new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "configSectionName", (pi, ctx) => "sectionName"));

 

Lambda表达式组件的参数 Parameters with Lambda Expression Components

 

使用Lambda表达式,而不是在组件注册时直接传入参数能够保证动态参数在服务被调用时被传入。

在组件注册表达式中,你可以通过改变用语注册组件的委托签名来使用传入的参数。可以使用具有一个IComponentContext参数和一个IEnumerable<Parameter>参数的委托来替代只使用单个IComponentContext参数的委托。

 

// Use TWO parameters to the registration delegate:// c = The current IComponentContext to dynamically resolve dependencies// p = An IEnumerable
with the incoming parameter setbuilder.Register((c, p) => new ConfigReader(p.Named
("configSectionName"))) .As
();

 

当通过使用参数解析组件时,你的Lambda表达式将会使用传入的参数:

 

reader = scope.Resolve
(new NamedParameter("configSectionName", "sectionName"));

 

转载于:https://www.cnblogs.com/mantgh/p/5123727.html

你可能感兴趣的文章
JavaScript 实现鼠标拖动元素
查看>>
js 模糊查询 (360接口)
查看>>
python+rabbitMQ实现生产者和消费者模式
查看>>
“模态”对话框和“后退”按钮
查看>>
关于javascript实现的网站页面侧边悬浮框"抖动"问题
查看>>
linux_命令格式和命令提示符
查看>>
Cocos2d-X-3.0之后的版本的环境搭建
查看>>
when case group by 的用法集合
查看>>
洛谷P1908 逆序对
查看>>
转义符
查看>>
poj 1019
查看>>
asp.net mvc上传文件
查看>>
bitmq集群高可用测试
查看>>
主成分分析(PCA)原理详解
查看>>
短信验证接口网址
查看>>
Geohash距离估算
查看>>
Demon_背包系统(实现装备栏,背包栏,可以切换装备)
查看>>
记录:一次数据库被恶意修改配置文件的问题
查看>>
redis 持久化
查看>>
解决Jupyter notebook[import tensorflow as tf]报错
查看>>