Asp.net Core 2 - 如何在 Asp.net Core 2.0 中使用 ServiceLocator
Asp.net Core 2 - How to use ServiceLocator in Asp.net Core 2.0
我的Startup
是这样的:
public void ConfigureServices(IServiceCollection services)
{
// code here
Bootstraper.Setup(services);
}
而我的Bootstraper
class是这样的:
public static partial class Bootstraper
{
// code here
public static IServiceCollection CurrentServiceCollection { get;set;}
public static IServiceProvider CurrentServiceProvider
{
get { return CurrentServiceCollection.BuildServiceProvider(); }
}
public static void Setup(IServiceCollection serviceCollection)
{
// code here
SetupLog();
InitializeCulture();
InitializeDbContexts();
RegisterDataModelRepositories();
}
这是我的内容 RegisterDataModelRepositories()
:
CurrentServiceCollection.AddTransient<IDefAccidentGroupRepository>(p => new DefAccidentGroupRepository(ApplicationMainContextId));
CurrentServiceCollection.AddTransient<IDefGenderRepository>(p => new DefGenderRepository(ApplicationMainContextId));
简而言之:我只是希望能够在我的方法中使用服务定位器而不解决 class 构造函数中的依赖关系......有什么办法解决它......
依赖注入也可以在 action 的基础上完成。
引用Dependency injection into controllers: Action Injection with FromServices
Sometimes you don't need a service for more than one action within your controller. In this case, it may make sense to inject the service as a parameter to the action method. This is done by marking the parameter with the attribute [FromServices]
public IActionResult SomeAction([FromServices] IReportService reports) {
//...use the report service for this action only
return View();
}
只需确保所需的服务已在服务集合中注册。
services.AddTransient<IDefAccidentGroupRepository>(p => new DefAccidentGroupRepository(ApplicationMainContextId));
services.AddTransient<IDefGenderRepository>(p => new DefGenderRepository(ApplicationMainContextId));
services.AddTransient<IReportService, ReportService>().
好的,谢谢你的帮助...
有一个更简单更好的方法,我只需要添加另一个使用这些存储库的服务,然后在我的控制器中解析该服务并让 Asp.net Core 2.0 DI 为我解决问题 ...
public interface IActionService
{
IRepositoryA repA {get;set;}
IRepositoryB repB { get;set;}
DoTaskX();
DoTaskY();
}
然后在我的 ActionService 中:
public class ActionService : IActionService
{
public IRepositoryA repA {get;set;}
public IRepositoryB repB { get;set;}
public ActionService (IRepositoryA rep_a , IRepositoryB rep_b ) {
repA = rep_a;
repB = rep_b;
}
DoTaskX(){
// do task using repository A and B
}
}
然后我在 Startup.cs 中注册 IActionService 并在我的 ActionController 中解析它,生活变得更轻松,代码变得更清晰......
解决方案很简单,但我必须改变思路才能解决问题...
我的Startup
是这样的:
public void ConfigureServices(IServiceCollection services)
{
// code here
Bootstraper.Setup(services);
}
而我的Bootstraper
class是这样的:
public static partial class Bootstraper
{
// code here
public static IServiceCollection CurrentServiceCollection { get;set;}
public static IServiceProvider CurrentServiceProvider
{
get { return CurrentServiceCollection.BuildServiceProvider(); }
}
public static void Setup(IServiceCollection serviceCollection)
{
// code here
SetupLog();
InitializeCulture();
InitializeDbContexts();
RegisterDataModelRepositories();
}
这是我的内容 RegisterDataModelRepositories()
:
CurrentServiceCollection.AddTransient<IDefAccidentGroupRepository>(p => new DefAccidentGroupRepository(ApplicationMainContextId));
CurrentServiceCollection.AddTransient<IDefGenderRepository>(p => new DefGenderRepository(ApplicationMainContextId));
简而言之:我只是希望能够在我的方法中使用服务定位器而不解决 class 构造函数中的依赖关系......有什么办法解决它......
依赖注入也可以在 action 的基础上完成。
引用Dependency injection into controllers: Action Injection with FromServices
Sometimes you don't need a service for more than one action within your controller. In this case, it may make sense to inject the service as a parameter to the action method. This is done by marking the parameter with the attribute
[FromServices]
public IActionResult SomeAction([FromServices] IReportService reports) {
//...use the report service for this action only
return View();
}
只需确保所需的服务已在服务集合中注册。
services.AddTransient<IDefAccidentGroupRepository>(p => new DefAccidentGroupRepository(ApplicationMainContextId));
services.AddTransient<IDefGenderRepository>(p => new DefGenderRepository(ApplicationMainContextId));
services.AddTransient<IReportService, ReportService>().
好的,谢谢你的帮助... 有一个更简单更好的方法,我只需要添加另一个使用这些存储库的服务,然后在我的控制器中解析该服务并让 Asp.net Core 2.0 DI 为我解决问题 ...
public interface IActionService
{
IRepositoryA repA {get;set;}
IRepositoryB repB { get;set;}
DoTaskX();
DoTaskY();
}
然后在我的 ActionService 中:
public class ActionService : IActionService
{
public IRepositoryA repA {get;set;}
public IRepositoryB repB { get;set;}
public ActionService (IRepositoryA rep_a , IRepositoryB rep_b ) {
repA = rep_a;
repB = rep_b;
}
DoTaskX(){
// do task using repository A and B
}
}
然后我在 Startup.cs 中注册 IActionService 并在我的 ActionController 中解析它,生活变得更轻松,代码变得更清晰......
解决方案很简单,但我必须改变思路才能解决问题...