net core 內置方式
介面
public interface ITestService
{
string Test();
}
實作
public class TestService : ITestService
{
public string Test()
{
return "依賴注入";
}
}
在Startup下的ConfigureServices中進行注入
services.AddTransient<ITestService, TestService>();
在控制器中進行使用
private readonly ITestService _TestService;
public HomeController(ITestService TestService)
{
_TestService = TestService;
}
public IActionResult Index()
{
_TestService .Test();
return View();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/212388.html
標籤:.NET技术
上一篇:.NET EF實作NoLock
