博客
关于我
【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/

你可能感兴趣的文章
NVIDIA GPU 的状态信息输出,由 `nvidia-smi` 命令生成
查看>>
NVIDIA-cuda-cudnn下载地址
查看>>
nvidia-htop 使用教程
查看>>
nvidia-smi 参数详解
查看>>
Nvidia驱动失效,采用官方的方法重装更快
查看>>
nvmw安装node-v4.0.0之后版本的临时解决办法
查看>>
nvm切换node版本
查看>>
nvm安装以后,node -v npm 等命令提示不是内部或外部命令 node多版本控制管理 node多版本随意切换
查看>>
ny540 奇怪的排序 简单题
查看>>
NYOJ 1066 CO-PRIME(数论)
查看>>
nyoj------203三国志
查看>>
nyoj58 最少步数
查看>>
OAuth2 + Gateway统一认证一步步实现(公司项目能直接使用),密码模式&授权码模式
查看>>
OAuth2 Provider 项目常见问题解决方案
查看>>
OAuth2 vs JWT,到底怎么选?
查看>>
Vue.js 学习总结(14)—— Vue3 为什么推荐使用 ref 而不是 reactive
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
查看>>
OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
查看>>