我正在与.net核心和依赖项注入一起工作。但是我对性能不确定。通常,服务是在构造函数中定义的,但是我不需要在方法内部提供某些服务。会不会导致性能损失?
这在下一行中是不利的使用吗?
public ValuesController(ICompanyService companyService, IOnlineUserService onlineUserService, IXService xService, ..., ..., ..., ...,)
{
}
在下面的代码行中使用它是否还有更多优势?
public virtual IActionResult UseCompanyServiceMethod()
{
ICompanyService _companyService = CtorContainer.Provider.GetRequiredService<ICompanyService>();
/*
...
*/
}
public virtual IActionResult AnyMethod()
{
IOnlineUserService onlineUserService = CtorContainer.Provider.GetRequiredService<IOnlineUserService>();
IXService xService = CtorContainer.Provider.GetRequiredService<IXService>();
... = CtorContainer.Provider.GetRequiredService <...> ();
/*
...
*/
}
public virtual IActionResult Method()
{
... = CtorContainer.Provider.GetRequiredService <...> ();
... = CtorContainer.Provider.GetRequiredService <...> ();
... = CtorContainer.Provider.GetRequiredService <...> ();
... = CtorContainer.Provider.GetRequiredService <...> ();
/*
...
*/
}
CtorContainer.cs
public static class CtorContainer
{
public static IConfiguration Configuration { get; set; }
public static IMapper Mapper { get; set; }
public static IServiceProvider Provider { get; set; }
}
startup.cs
public void ConfigureServices(IServiceCollection services)
{
CtorContainer.Provider = services.BuildServiceProvider();
}