日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

.netCore2.0 依赖注入

發布時間:2024/4/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .netCore2.0 依赖注入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

依賴注入(ID)是一種實現對象及其合作者或者依賴想之間松散耦合的技術
對于傳統的方法來說,獲取類的方法通常用new如下

1 public class DIController : Controller 2 { 3 public IActionResult Index() 4 { 5 MyServices my = new MyServices(); 6 my.getName(); 7 return View(); 8 } 9 }

但是問題來了,如果我后期要修改MyServices類的時候,就需要在整個項目中來搜索修改

?

一:接口注入

為了實現解耦,我們需要建立IServices的接口,然后類實現接口,在控制器中進行構造函數,初始化接口,然后使用接口來進行操作,這樣就達到了解耦的目的,俗稱依賴注入

具體類代碼如下

1 namespace CoreWeb2.Services 2 { 3 public class MyServices:IServices 4 { 5 public string getName() 6 { 7 return "張三"; 8 } 9 } 10 public class OServices:IServices 11 { 12 public string getName() 13 { 14 return "李四"; 15 } 16 } 17 public interface IServices { 18 string getName(); 19 } 20 }

控制器代碼如下:

1 namespace CoreWeb2.Controllers 2 { 3 public class DIController : Controller 4 { 5 /// <summary> 6 /// 通過構造函數初始化接口 7 /// 在外界使用時候直接new出具體類,這樣就達到了程序解耦的目的,俗稱依賴注入 8 /// </summary> 9 public IServices _services; 10 public DIController(IServices services) 11 { 12 this._services = services; 13 } 14 public IActionResult Index() 15 { 16 //通過接口調用 17 _services.getName(); 18 return View(); 19 } 20 } 21 }

但是問題又來了,因為控制器默認構造函數是一個無參的,現在增加了參數傳遞,直接訪問會報錯誤,因而就需要在Startup.cs中的ConfigureServices方法中進行程序注入,將構造函數的參數注入到容器中,當程序訪問時會自動在容器中找參數,才可以使用

依賴注入有三種方式:

1、AddTransient

  每個請求創建一個

2、AddScoped

  一個域創建一個

3、AddSingleton

  單例,整個應用程序生命周期以內只創建一個實例?

1 //每個請求創建一個 2 services.AddTransient<IServices, MyServices>(); 3 //一個域創建一個 4 services.AddScoped<IServices, MyServices>(); 5 //單例,整個應用程序生命周期以內只創建一個實例 6 services.AddSingleton<IServices, MyServices>();

當程序進行依賴注入后,控制器中就可以訪問到當前的實例對象了,

?二:泛型注入

泛型接口

/// <summary>/// 數據倉儲/// </summary>/// <typeparam name="TEntity"></typeparam>public interface IRepository<TEntity> where TEntity : class{DbContext DbContext { get; }DbSet<TEntity> Entities { get; }IQueryable<TEntity> Table { get; }TEntity GetById(object id);void Insert(TEntity engine, bool isSave = true);void Update(TEntity engine, bool isSave = true);void Delete(TEntity engine, bool isSave = true);}

泛型接口實現

public class EfRepository<TEntity> : IRepository<TEntity> where TEntity:class{private GeneralDbContext _dbContext;public EfRepository(GeneralDbContext generalDbContext){_dbContext = generalDbContext;}public DbContext DbContext => _dbContext;public DbSet<TEntity> Entities => _dbContext.Set<TEntity>();public IQueryable<TEntity> Table => Entities;public void Delete(TEntity engine, bool isSave = true){Entities.Remove(engine);if (isSave){_dbContext.SaveChanges();}}public TEntity GetById(object id){return _dbContext.Set<TEntity>().Find(id);}public void Insert(TEntity engine, bool isSave = true){Entities.Add(engine);if (isSave){_dbContext.SaveChanges();}}public void Update(TEntity engine, bool isSave = true){Entities.Update(engine);if (isSave){_dbContext.SaveChanges();}}

泛型注入方式

? //將泛型注入 services.AddScoped(typeof(IRepository<>),typeof(EfRepository<>));?

關聯類GeneralDbContext

public class GeneralDbContext : DbContext{public GeneralDbContext(DbContextOptions options) : base(options){ }public DbSet<Category> Categorys{ get; set; }}

?

轉載于:https://www.cnblogs.com/happygx/p/8845835.html

總結

以上是生活随笔為你收集整理的.netCore2.0 依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。