Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Dependency Injection inject out of constructor [closed]

I need to inject out of constructor, everything I declared in Setup.

Ho can I do it ? How can I inject services out of constructor ? Something like Injector service in Angular 2.

INJECT SERVICES WITHOUT CONSTRUCTOR IN CONTROLLERS

something like this

    public class ControllerBase : Controller
    {
        protected IRepository<Test> _test;
        protected IRepository<Test1> _test1;
        protected IRepository<Test2> _test2;

        public ControllerBase(INJECTOR injector)
        {
            _test = injector.inject(IRepository<Test>);
            _test1 = injector.inject(IRepository<Test1>);
            _test2 = injector.inject(IRepository<Test2>);
        }
    }

    public class SomeController : ControllerBase
    {
        public SomeController(INJECTOR injector)
            : base(injector)
        {

        }
    }

THANKS FOR ANSWER @Rick van den Bosch

FOR THOSE WHO STILL CAN'T GET WHAT I WANTED:

public class ControllerBase : Controller
{
    protected IRepository<Test> _test;
    protected IRepository<Test1> _test1;
    protected IRepository<Test2> _test2;

    public ControllerBase(IServiceProvider injector)
    {
        _test = injector.GetService<IRepository<Test>>();
        _test1 = injector.GetService<IRepository<Test1>>();
        _test2 = injector.GetService<IRepository<Test2>>();
    }
}

public class SomeController : ControllerBase
{
    public SomeController(IServiceProvider injector)
        : base(injector)
    {
        //HERE I HAVE ALL 3 REPO NOW WITHOUT EXTRA LINES
    }
}
public class SomeController1 : ControllerBase
{
    public SomeController1(IServiceProvider injector)
        : base(injector)
    {
        //HERE I HAVE ALL 3 REPO NOW WITHOUT EXTRA LINES
    }
}
like image 724
Tornike Choladze Avatar asked Jun 13 '17 11:06

Tornike Choladze


People also ask

Is it difficult to inject dependency by constructor?

Frameworks that apply the Constrained Construction anti-pattern can make using Constructor Injection difficult. The main disadvantage to Constructor Injection is that if the class you're building is called by your current application framework, you might need to customize that framework to support it.

Can you inject IServiceProvider?

You can inject an instance of type IServiceProvider into any method of a class. You can also take advantage of the ApplicationServices property of the IApplicationBuilder interface and the RequestServices property of the HttpContext class to retrieve an IServiceProvider instance.

Does dependency injection dispose?

Disposal of services The container is responsible for cleanup of types it creates, and calls Dispose on IDisposable instances. Services resolved from the container should never be disposed by the developer. If a type or factory is registered as a singleton, the container disposes the singleton automatically.

Should I use transient or scoped?

Use Transient lifetime for the lightweight service with little or no state. Scoped services service is the better option when you want to maintain state within a request. Singletons are created only once and not destroyed until the end of the Application. Any memory leaks in these services will build up over time.


1 Answers

You can inject the service as a parameter to the action method. This is done by marking the parameter with the attribute [FromServices].

This looks something like this:

public IActionResult About([FromServices] IDateProvider dateProvider)
{
    ViewData["Message"] = $"Current date is {dateProvider.CurrentDate}";

    return View();
}

If you're looking for default services in a BaseController: you could go about that several ways:

1. Still use a constructor
This would look something like this:

public class HomeController : BaseController
{
    public HomeController(IDateProvider dateProvider) : base(dateProvider)
    {
    }
}

and

public class BaseController
{
    protected IDateProvider _dateProvider;

    protected BaseController(IDateProvider dateProvider)
    {
        _dateProvider = dateProvider;
    }
}

This way the IDateProvider is available to both the BaseController and all inheriting Controllers.

2. Resolve services manually
This way you resolve the service manually. This could be in the BaseController, and only when you need them (lazy). For more info, see this post.

For simplicity and readability I would probably choose the constructor one.

like image 110
rickvdbosch Avatar answered Nov 07 '22 00:11

rickvdbosch