user2568693
user2568693

Reputation:

Asp.net Core 2 - How to use ServiceLocator in Asp.net Core 2.0

My Startup is like this :

public void ConfigureServices(IServiceCollection services)
{
    // code here 
    Bootstraper.Setup(services);
}

And my Bootstraper class is like this :

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();
    }

and this is content of my RegisterDataModelRepositories():

CurrentServiceCollection.AddTransient<IDefAccidentGroupRepository>(p => new DefAccidentGroupRepository(ApplicationMainContextId));
CurrentServiceCollection.AddTransient<IDefGenderRepository>(p => new DefGenderRepository(ApplicationMainContextId));

in short : I just want to be able to use Service Locator in my methods without resolving dependency in class constructor ... is there any way around it ....

Upvotes: 1

Views: 10469

Answers (2)

user2568693
user2568693

Reputation:

well , thanks for your help ... There is a easier and better way for it , I just need to add another Service that use these repository and then resolve that service in my controller and let Asp.net Core 2.0 DI to solve the problem for me ...

public interface IActionService 
{
   IRepositoryA repA {get;set;}
   IRepositoryB repB { get;set;}

   DoTaskX();
   DoTaskY();
}

then in my 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
   }
}

then I register IActionService in Startup.cs and resolve itin my ActionController and life become easier and code become cleaner ...

the solution was easy but I had to change my mindset to solve the problem ...

Upvotes: -2

Nkosi
Nkosi

Reputation: 247078

Dependency injection can also be done on a by action basis.

Referece 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();
}

Just make sure that the required services are registered with the service collection.

services.AddTransient<IDefAccidentGroupRepository>(p => new DefAccidentGroupRepository(ApplicationMainContextId));
services.AddTransient<IDefGenderRepository>(p => new DefGenderRepository(ApplicationMainContextId));
services.AddTransient<IReportService, ReportService>().

Upvotes: 3

Related Questions