自定义组件
基本依赖注入
Ageis 提供以Attribute方式来快速依赖注入。
如何使用
- 注入类本身 ``` csharp [Lifetime(ServiceLifetime.Scoped))] public class MedicalManager : IMedicalManager, IMedicalHandler, IDisposable { ... }
// 上述代码等同于在Startup里实现下述代码
service.AddScoped
2. 注入类并绑定到对应接口/类上
``` csharp
[Lifetime(ServiceLifetime.Scoped,typeof(IMedicalManager)))]
public class MedicalManager : IMedicalManager, IMedicalHandler, IDisposable
{
...
}
// 上述代码等同于在Startup里实现下述代码
service.AddScoped<IMedicalManager,MedicalManager>();
- 多重注入类并绑定到对应接口/类上 ``` csharp [Lifetime(ServiceLifetime.Scoped,typeof(IMedicalManager)))] [Lifetime(ServiceLifetime.Scoped,typeof(IMedicalHandler)))] public class MedicalManager : IMedicalManager, IMedicalHandler, IDisposable { ... }
// 上述代码等同于在Startup里实现下述代码
service.AddScoped
属性说明
该属性支持两个参数
Lifetime(ServiceLifetime,Type)
ServiceLifetime
ServiceLifeTime是一个微软官方提供的枚举,有以下三种值
- ServiceLifetime.Singleton
- ServiceLifetime.Scoped
- ServiceLifetime.Transient
Type
注入的绑定类/接口的类型