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

你可能感兴趣的文章
NSRange 范围
查看>>
NSSet集合 无序的 不能重复的
查看>>
NSURLSession下载和断点续传
查看>>
NSUserdefault读书笔记
查看>>
NS图绘制工具推荐
查看>>
NT AUTHORITY\NETWORK SERVICE 权限问题
查看>>
NT symbols are incorrect, please fix symbols
查看>>
ntelliJ IDEA 报错:找不到包或者找不到符号
查看>>
NTFS文件权限管理实战
查看>>
ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
ntp server 用法小结
查看>>
ntpdate 通过外网同步时间
查看>>
NTPD使用/etc/ntp.conf配置时钟同步详解
查看>>
NTP及Chrony时间同步服务设置
查看>>
NTP配置
查看>>
NUC1077 Humble Numbers【数学计算+打表】
查看>>
NuGet Gallery 开源项目快速入门指南
查看>>
NuGet(微软.NET开发平台的软件包管理工具)在VisualStudio中的安装的使用
查看>>
nuget.org 无法加载源 https://api.nuget.org/v3/index.json 的服务索引
查看>>