小编Joe*_*R94的帖子

AddSingleton(S, T) 代替 AddSingleton(T) 的好处

使用services.AddSingleton<SomeService, SomeServiceImplementation>()而不是有services.AddSingleton<SomeServiceImplementation>()什么好处?

例如我有一个示例界面

interface ISampleInterface
{
   void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

和一个样本类:

class SampleClass : ISampleInterface
{
  public void DoSomething()
  {
    console.write("hi");
  }
}
Run Code Online (Sandbox Code Playgroud)

不,我做 services.AddSingleton<SampleClass>()

为什么或何时使用services.AddSingleton<ISampleInterface, SampleClass>()

谢谢你的帮助!:-)

c# asp.net-core blazor

0
推荐指数
1
解决办法
79
查看次数

标签 统计

asp.net-core ×1

blazor ×1

c# ×1