我正在寻找一个干净的模式来使用.Net扩展方法中的依赖项,而无需显式地新建或使用服务定位器:
public static class HttpContextExtensions
{
public static SomeClass ExtensionMethod(this HttpContext context)
{
//looking to avoid this
var dependency = ServiceLocator.GetService<DependencyType>();
return dependency.DoSomething(context);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里吠叫错了吗?我是否应该寻找更直接的解决方案context?如果可能的话,我想继续使用扩展程序.