使用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>()
?
谢谢你的帮助!:-)