博客
关于我
【Autofac打标签模式】Component和Autowired
阅读量:477 次
发布时间:2019-03-06

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

Autofac 打标签模式 开源 DI 框架扩展说明

1. 将类型注册到 DI 容器

通过 Autofac,可以将自定义类型注册到 DI 容器中,以便容器能够管理这些类型的实例。以下示例展示了如何将 Student 类注册到容器:

// Student 类public class Student{    public Student(string name) {}}
// 在配置中注册container.RegisterType(typeof(Student));

2. 注册当前类型和父类

当一个类继承自父类时,可以选择将父类和子类都注册到 DI 容器中。例如,Student2 类继承自 Person 类,可以通过以下方式注册:

// Student2 类public class Student2 : Person{    public Student2(string name) : base(name) {}}
// 注册 Student2 类container.RegisterType(typeof(Student2));// 注册 Person 类container.RegisterType(typeof(Person));

通过 Person 类可以获取 Student2 的实例。

3. 注册当前类型和接口

当一个类实现接口时,可以选择将该类和接口都注册到 DI 容器中。例如,Student3 类实现 ISay 接口,可以通过以下方式注册:

// Student3 类public class Student3 : ISay{    public Student3(string name) {}    public void Say() {}}// ISay 接口public interface ISay{    void Say();}
// 注册 Student3 类container.RegisterType(typeof(Student3));// 注册 ISay 接口container.RegisterType(typeof(ISay));

通过 ISay 类可以获取 Student3 的实例。

4. 继承父类或接口时如何指定注册类型

当类继承自父类或实现接口时,通常情况下只能通过接口来获取实例。例如:

// Student3 类继承自 Person 类并实现 ISay 接口public class Student3 : Person, ISay{    public Student3(string name) : base(name) {}    public void Say() {}}
// 通过 ISay 类获取 Student3 实例container.Resolve(typeof(ISay)) // 返回 Student3 实例

不能通过 Student3 类直接获取实例。

5. 指定实例的单例或新建模式

要指定实例的获取模式,可以在注册时设置 AutofacScope 属性:

// 注册 Student 类为单例模式container.RegisterType(typeof(Student), AutofacScope.Singleton);// 注册 Student 类为每次新建实例container.RegisterType(typeof(Student), AutofacScope.Transient);

如果不指定 AutofacScope,默认为 Transient 模式,每次获取一个新实例。

6. 多次注册同一个类型

当同一个类型被多次注册时,可以通过指定不同的 Key 值来区分获取的实例。例如:

// Student3 和 Student4 类实现 ISay 接口public class Student3 : ISay{    public Student3(string name) {}    public void Say() {}}public class Student4 : ISay{    public Student4(string name) {}    public void Say() {}}
// 注册 ISay 类并指定 Keycontainer.RegisterType(typeof(ISay), "Student3", typeof(Student3));container.RegisterType(typeof(ISay), "Student4", typeof(Student4));

通过 ISay + "Student3" 可以获取 Student3 实例,ISay + "Student4" 可以获取 Student4 实例。

7. 支持自动实例化对象

可以通过设置 AutoActivate 属性来指定实例化时是否自动启动:

// 注册 Student 类并设置为自动实例化container.RegisterType(typeof(Student), AutofacScope.AutoActivate);// 设置为单例模式container.Resolve(typeof(Student), AutofacScope.SingleInstance);

8. 设置实例化时运行指定方法

可以通过设置 InitMethodDestroyMethod 来指定实例化时运行的方法:

// 注册 Student 类并设置初始化方法container.RegisterType(typeof(Student), new MyInitializationMethod());// 注册销毁方法container.RegisterDispose(typeof(Student), new MyCleanupMethod());

InitMethodDestroyMethod 只能是无参数方法。


以上内容为关于 Autofac 打标签模式的详细说明,涵盖了类型注册、接口注册、单例模式、多次注册以及实例化方法等多个方面。

转载地址:http://jqmbz.baihongyu.com/

你可能感兴趣的文章
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
Nginx配置参数中文说明
查看>>
Nginx配置好ssl,但$_SERVER[‘HTTPS‘]取不到值
查看>>
Nginx配置实例-负载均衡实例:平均访问多台服务器
查看>>
Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>
NIO基于UDP协议的网络编程
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>