ASP.NET Core 5 MVC — Mod07 — RepositoryExample 範例下載

MIS2000 Lab.
6 min readFeb 26, 2021

--

微軟認證教材 20486D放在 github的範例比較舊,似乎是 .NET Core 2.x版

https://github.com/MicrosoftLearning/20486D-DevelopingASPNETMVCWebApplications/tree/master/Allfiles/Mod07/Democode/02_RepositoryExample_end

我稍微調整後,改成 VS 2019 / .NET Core 5的版本

放在 https://onedrive.live.com/?id=6F7F668080F24B20%212031&cid=6F7F668080F24B20

下載檔名 — MVC_04補充範例_Core5_VS2019_RepositoryExample.rar

搭配 第四天課程 Repository & Interface for (.NET Core 5 MVC)

但 .NET Core 多了一些設定,可參考下面的說明 — 相依性插入 / 依賴注入 (Dependency Injection)

線上課程 https://9vs1.com/go/?i=3061493ef814

ASP.NET Core 與 MVC5 雙平台 — Repository 倉庫與 Interface 介面

https://9vs1.com/go/?i=3061493ef814

== 參考文件 ==

請參考微軟官方文件說明 — https://docs.microsoft.com/zh-tw/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0

.NET Core 中的 相依性插入 / 依賴注入 (DI、Dependency Injection)

相依性插入 (DI) / 依賴注入 (Dependency Injection) 可透過下列方式解決這些問題:
— 使用
介面 或 基底類別 來將相依性資訊抽象化。
— 在服務容器中
註冊 相依性。 ASP.NET Core 提供內建服務容器 IServiceProvider。 服務通常會在應用程式的方法中註冊 Startup.ConfigureServices (註:Startup.cs檔)
— 將服務「插入」到服務使用位置之類別的建構函式。 架構會負責建立相依性的執行個體,並在不再需要時將它捨棄。

在 範例應用程式中, IMyDependency 介面會定義 WriteMessage 方法:
public
interface IMyDependency
{
void WriteMessage(string message);
}

這個 介面 是由具象型別 MyDependency 所實作:
public class MyDependency :
IMyDependency
{
public void WriteMessage(string message)
{
Console.WriteLine($”MyDependency.WriteMessage Message: {message}”);
}
}
範例應用程式會 IMyDependency 使用具象類型來註冊服務 MyDependency 。

AddScoped方法會使用範圍存留期(單一要求的存留期)來註冊服務。 將在此主題稍後將說明服務存留期。

(註:Startup.cs檔 的 ConfigureServices 區塊)

public void ConfigureServices(IServiceCollection services)
{
services.
AddScoped<IMyDependency, MyDependency>();

services.AddRazorPages();
}

可以參考這篇文章

Understanding AddTransient Vs AddScoped Vs AddSingleton In ASP.NET Core

線上課程 https://9vs1.com/go/?i=3061493ef814

ASP.NET Core 與 MVC5 雙平台 — Repository 倉庫與 Interface 介面

https://9vs1.com/go/?i=3061493ef814

我將思想傳授他人, 他人之所得,亦無損於我之所有;

猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。 — — Thomas Jefferson

線上課程,遠距教學 (Web Form 49hr) https://dotblogs.com.tw/mis2000lab/2016/02/01/aspnet_online_learning_distance_education_VS2015

線上課程,遠距教學 (ASP.NET MVC 75~80hr) https://dotblogs.com.tw/mis2000lab/2018/08/14/ASPnet_MVC_Online_Learning_MIS2000Lab

ASP.NET MVC線上課程 第一天 免費看 (5.5小時)

寫信給我,不要私訊 — mis2000lab (at) yahoo.com.台灣school (at) mis2000lab.net

ASP.NET遠距教學、線上課程(Web Form + MVC)。 第一天課程, “完整” 試聽。

……………. facebook社團 https://www.facebook.com/mis2000lab

……………. YouTube (ASP.NET) 線上教學影片 https://www.youtube.com/channel/UC6IPPf6tvsNG8zX3u1LddvA/

--

--

MIS2000 Lab.
MIS2000 Lab.

Written by MIS2000 Lab.

ASP.NET 線上教學 / 線上教程 — .Net Core MVC + WebAPI 與 Web Form。ASP.NET專題實務。免費試聽4~5小時,請來信索取 mis2000lab(at)yahoo. com. tw 或 school(at)mis2000lab. net 謝謝

No responses yet