IOC
的作用這裡省略…只對如何使用進行說明。
這裡使用 .NET6.0 WebAPI
應用
public interface IAuthService
{
bool CheckToken();
}
class AuthServiceImpl : IAuthService
{
public bool CheckToken()
{
Console.WriteLine("check token");
return true;
}
}
下面是在 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
中。
[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;
}
}
利用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);
}
}
[Autowired(typeof(IAuthService))]
class AuthServiceImpl : IAuthService
{
public bool CheckToken()
{
Console.WriteLine("check token");
return true;
}
}
var svc = ServiceLoader.Instance.GetService<IAuthService>();
svc.CheckToken();
至此一個基本的完整IOC容器
已實現。
本文來自部落格園,作者:宣君{https://www.nhit.icu/},轉載請註明原文連結:https://www.cnblogs.com/ycit/p/17680238.html