Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular Like Constructor Dependency Injection #12492

Closed
sebastienlabine opened this issue Jul 23, 2019 · 2 comments
Closed

Angular Like Constructor Dependency Injection #12492

sebastienlabine opened this issue Jul 23, 2019 · 2 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates External This is an issue in a component not contained in this repository. It is open for tracking purposes.

Comments

@sebastienlabine
Copy link

Is your feature request related to a problem? Please describe.

I've been working with .net core for several years right now and I find it annoyingly repetitive and long to inject services. Here's why:

In order to inject a service, you need to :

  1. Register the service in Startup.cs. (which doesn't bother me)
  2. Create a propriety in the class that you are injecting the service into
  3. Assigning the proprety to the service
public class Controller : ControllerBase
{
        private readonly InjectedService _injectedService;

        public Controller(InjectedService injectedService)
        {
                _injectedService = injectedService;
        }
       
        public void SomeMethod() 
        {
            _injectedService.doSomething();
        } 
}

Describe the solution you'd like

From previous experience with Angular, I really loved their dependency injection that required only to inject the service as a private property in the constructor.(Angular Documentation)

It was quicker for the programmer to do so and less painfull in projects with multiple services injected per class.

Proposed Changes:
It would be appreciated if we could inject a service like this:

public class Controller : ControllerBase
{
        public Controller(private readonly InjectedService _injectedService)
        {
        }

        // OR with default readonly

        public Controller(private InjectedService _injectedService)
        {
        }

        public void SomeMethod() 
        {
            _injectedService.doSomething();
        } 
}
@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jul 23, 2019
@outring
Copy link

outring commented Jul 23, 2019

It's a language feature (in your Angular case, it's a TypeScript feature), not framework. There are some similar proposals in compiler repo, like this one: dotnet/roslyn#14853
But if you're planning on adding a new proposition, note that it must be posted to the language repo itself: https://github.com/dotnet/csharplang

@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @sebastienlabine.
This is not something we have control over so we're closing this issue.

@mkArtakMSFT mkArtakMSFT added the External This is an issue in a component not contained in this repository. It is open for tracking purposes. label Jul 25, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates External This is an issue in a component not contained in this repository. It is open for tracking purposes.
Projects
None yet
Development

No branches or pull requests

4 participants