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

你可能感兴趣的文章
Objective-C实现Euclidean GCD欧几里得最大公约数算法(附完整源码)
查看>>
Objective-C实现euclideanDistance欧氏距离算法(附完整源码)
查看>>
Objective-C实现euler method欧拉法算法(附完整源码)
查看>>
Objective-C实现euler modified变形欧拉法算法(附完整源码)
查看>>
Objective-C实现eulerianPath欧拉路径算法(附完整源码)
查看>>
Objective-C实现Eulers TotientFunction欧拉函数算法(附完整源码)
查看>>
Objective-C实现eulers totient欧拉方程算法(附完整源码)
查看>>
Objective-C实现EulersTotient欧拉方程算法(附完整源码)
查看>>
Objective-C实现eval函数功能(附完整源码)
查看>>
Objective-C实现even_tree偶数树算法(附完整源码)
查看>>
Objective-C实现Exceeding words超词(差距是ascii码的距离) 算法(附完整源码)
查看>>
Objective-C实现exchange sort交换排序算法(附完整源码)
查看>>
Objective-C实现ExponentialSearch指数搜索算法(附完整源码)
查看>>
Objective-C实现extended euclidean algorithm扩展欧几里得算法(附完整源码)
查看>>
Objective-C实现ExtendedEuclidean扩展欧几里德GCD算法(附完整源码)
查看>>
Objective-C实现external sort外排序算法(附完整源码)
查看>>
Objective-C实现Factorial digit sum阶乘数字和算法(附完整源码)
查看>>
Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
查看>>
Objective-C实现factorial recursive阶乘递归算法(附完整源码)
查看>>
Objective-C实现factorial阶乘算法(附完整源码)
查看>>