一、创建一个.NET 6应用程序
这里使用 .NET6.0 WebAPI
应用
二、声明接口
public interface IAuthService
{
bool CheckToken();
}
三、实现接口
class AuthServiceImpl : IAuthService
{
public bool CheckToken()
{
Console.WriteLine("check token");
return true;
}
}
四、配置IOC容器
下面是在 program
类中的代码
var services = new ServiceCollection();
services.AddSingleton<IAuthService, AuthServiceImpl>();
五、获取服务
通过在 Controller
的构造函数中注入IAuthService
private readonly IAuthService _service;
public WeatherForecastController(IAuthService service)
{
_service = service;
}
[HttpGet(Name = "test")]
public bool Get()
{
return _service.CheckToken();
}
启动后,通过swagger
发起请求,验证接口。
基本IOC容器
流程已实现。但是这样存在一个弊端,每个接口和实现都要在program
中手动注册一遍,还要在Controller
构造函数中进行依赖注入,有没有能自动实现注册代替program
中的手动注册?
接下来,对上述流程进行改良。
六、改良思路
定义一个AutowiredAttribute
标记,通过Atrribute
标记的方式,在实现类上标记其要实现的接口服务,然后实现一个服务加载类ServiceLoader
,在这个类中反射获取所有具备AutoIocAttribute
的实现类,然后注册到ServiceCollection
中。
6.1、定义特性标记AutowiredAttribute
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class AutowiredAttribute : Attribute
{
/// <summary>
/// 接口
/// </summary>
public Type Iface { get; set; }
/// <summary>
/// 实现类名
/// </summary>
public string ImplClassName { get; set; }
public AutowiredAttribute(Type iface, [CallerMemberName] string implClassName = "")
{
Iface = iface;
ImplClassName = implClassName;
}
}
6.2、实现服务加载类
利用IServiceCollection
作为服务容器
public class ServiceLoader
{
private static object _lock = new object();
private static AppRuntime _inst;
private readonly IServiceCollection _iocService = new ServiceCollection();
private readonly ICollection<Assembly> _iocAssembly = new HashSet<Assembly>();
private IServiceProvider _iocServiceProvider = null;
public static ServiceLoader Instance
{
get
{
if (_inst == null)
{
lock (_lock)
{
_inst = new ServiceLoader();
_inst.Startup(typeof(ServiceLoader).Assembly);
}
}
return _inst;
}
}
public T GetService<T>()
{
EnsureAutoIoc<T>();
return _iocServiceProvider.GetService<T>();
}
private void EnsureAutoIoc<T>()
{
Startup(typeof(T).Assembly);
}
public void Startup(Assembly ass)
{
if (_iocAssembly.Any(x => x == ass))
{
return;
}
_iocAssembly.Add(ass);
var types = ass.GetTypes().Where(x => x.GetCustomAttribute<AutowiredAttribute>() != null);
foreach (var item in types)
{
var autoIocAtt = item.GetCustomAttribute<AutowiredAttribute>();
AddTransient(autoIocAtt.Iface, item);
}
//_iocServiceProvider = _iocService.BuildServiceProvider();
Interlocked.Exchange(ref _iocServiceProvider, _iocService.BuildServiceProvider());
}
private void AddTransient(Type iface, Type impl)
{
_iocService.AddTransient(iface, impl);
}
}
6.3、在实现类加上标记
[Autowired(typeof(IAuthService))]
class AuthServiceImpl : IAuthService
{
public bool CheckToken()
{
Console.WriteLine("check token");
return true;
}
}
6.4、在 Controller 中调用
var svc = ServiceLoader.Instance.GetService<IAuthService>();
svc.CheckToken();
至此一个基本的完整IOC容器已实现。
上面这篇文章是利用特性加载,将MVC中service中的加载项,抽取出来;利用类动态加载实现的;理解这篇文章需要首先搞懂几个概念点:
C#特征,使用特征能干什么,什么时候使用;
MVC中程序的加载顺序,以前的项目中有startup.cs负责服务的加载,依赖项,app的启用;现在都在program.cs中可以直接顶行书写;这两种方式的区别;
Assembly使用 类加载程序集、浏览程序集的元数据和构成部分、发现程序集中包含的类型以及创建这些类型的实例。
码字不易,如果您觉的我的文章对您有帮助的话,建议您在经济能力之内慷慨打赏一元给我买瓶水, 这将是我下一步继续书写本题目的动力;如果您囊肿羞涩也没有关系,希望您点个关注,写点评论;您的支持将是我创作之路上的无线动力;青山依旧绿水长流,希望我们下期来能再见。