.NET 支持依賴項注入 (DI) 軟件設(shè)計模式,這是一種在類及其依賴項之間實現(xiàn) 控制 (IoC) 的反轉(zhuǎn) 的技術(shù)
在設(shè)計能夠進行依賴注入的服務(wù)時:
- 避免有狀態(tài)的、靜態(tài)類和成員。 通過將應(yīng)用設(shè)計為改用單一實例服務(wù),避免創(chuàng)建全局狀態(tài)。
- 避免在服務(wù)中直接實例化依賴類。 直接實例化會將代碼耦合到特定實現(xiàn)。
- 不在服務(wù)中包含過多內(nèi)容,確保設(shè)計規(guī)范,并易于測試。
如果一個類有很多已注入的依賴關(guān)系,這可能表明該類擁有過多的責任,并且違反了單一責任原則 (SRP)。 嘗試通過將某些職責移動到一個新類來重構(gòu)類。
使用的包Microsoft.Extensions.DependencyInjection
參考微軟文檔
.NET 依賴項注入
https://learn.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection
使用依賴關(guān)系注入
https://learn.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection-usage
依賴關(guān)系注入指南
https://learn.microsoft.com/zh-cn/dotnet/core/extensions/dependency-injection-guidelines文章來源:http://www.zghlxwxcb.cn/news/detail-415169.html
Program.cs
using WzfgsInfoReportNET6.IBLL;
using WzfgsInfoReportNET6.IBLL_impl;
using WzfgsInfoReportNET6.IDao;
using WzfgsInfoReportNET6.IDao_impl;
using WzfgsInfoReportNET6.Models;
var builder = WebApplication.CreateBuilder(args);
#region 業(yè)務(wù)層接口,實現(xiàn)注冊
builder.Services.AddSingleton<IAnMenusBll, AnMenusBll>();
builder.Services.AddSingleton<IAnRoleBll, AnRoleBll>();
builder.Services.AddSingleton<IAnUserBll, AnUserBll>();
builder.Services.AddSingleton<IArticle2Bll, Article2Bll>();
#endregion
#region 數(shù)據(jù)層接口,實現(xiàn)注冊
builder.Services.AddSingleton<IAdoptedRecordDao, AdoptedRecordDao>();
builder.Services.AddSingleton<IAnMenusDao, AnMenusDao>();
builder.Services.AddSingleton<IAnRoleDao, AnRoleDao>();
#endregion
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
AppHelpter.App = app;
//var kkk = app.Services.GetRequiredService<IAnMenusDao>();
//var bbb = WebApplicationBuilderHelpter.GetServices<ITaskBll>();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
工具類,獲取注冊的實例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace WzfgsInfoReportNET6.Models
{
/// <summary>
/// 依賴注冊,獲取接口實例
/// </summary>
/// 創(chuàng)建時間:2023-4-11 16:48:42。作者:xxx
public sealed class AppHelpter
{
/// <summary>
/// 程序
/// </summary>
public static WebApplication App { get; set; }
/// <summary>
/// 獲取接口實例
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GetServices<T>() where T : notnull
{
T instance = App.Services.GetRequiredService<T>();
return instance;
}
}
}
調(diào)用文章來源地址http://www.zghlxwxcb.cn/news/detail-415169.html
public class AnRoleBll : IAnRoleBll
{
readonly IAnRoleDao anRoleDao= AppHelpter.GetServices<IAnRoleDao>();
readonly IAnUserDao anUserDao= AppHelpter.GetServices<IAnUserDao>();
readonly IRoleFunctionsDao roleFunctionsDao = AppHelpter.GetServices<IRoleFunctionsDao>();
public async Task<Result> AddAsync(AnRole model)
{
int count = await anRoleDao.AddAsync(model);
if (count > 0)
{
return new Result(true);
}
return new Result("新增失敗");
}
}
到了這里,關(guān)于.NET使用依賴注入,控制反轉(zhuǎn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!