C#依赖注入(一)初识
使用没有依赖注入的服务
//代码文件 WithoutDi/GreetService.cs
public class GreetingSevice
{
public string Greet(string name) => $"Hello,{name}";
}
类HomeController使用服务
//代码文件 WithoutDi/HomeController.cs
public class HomeController
{
public string Hello(string name)
{
var service = new GreetingService();
service.Greet(name);
}
}
//主函数
static void Main()
{
var controller = new HomeConlller();
string result = controller.Hello("Stephanie");
Console.WriteLine(result);
}