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

[Proposal] Class constructor variable declaration #3592

Closed
ShreyasJejurkar opened this issue Jun 21, 2020 · 3 comments
Closed

[Proposal] Class constructor variable declaration #3592

ShreyasJejurkar opened this issue Jun 21, 2020 · 3 comments

Comments

@ShreyasJejurkar
Copy link

ShreyasJejurkar commented Jun 21, 2020

Hi all,
This is my first issue with this repo. Please suggest edit if I made any mistake.

Explanation -
Most of the time I worked with TypeScript (Angular) and C# (ASP.NET Core) for building web applications. And in both of the framework, there is a concept of dependency injection of (mostly) services where specifically we add a service to dependency injection container (in startup.cs in case of asp.net core) and use them in the class where we want to use them like below.
Let's say we have EmployeeService and we are using in EmployeeController as below.

public class EmployeeController : ControllerBase
{
   private readonly IEmployeeService _IEmployeeService { get; }
   EmployeeController(IEmployeeService employeeService)
   {
       _IEmployeeService = employeeService;
   }
}

And then we use employeeService to get data. But if you see above there is quite a lot duplication is there that we have to create class instance variable and initialize in it constructor ( as we know already it's going to initialized from the container ).

TypeScript who is an inhouse colleague of CSharp 😜 has a pretty clean neat way of doing the same as below.

export class EmployeeComponent
{
   constructor(private employeeService: EmployeeService) {
      
   }
   // other methods can access service with this._employeeService
}

As you can see above is much cleaner, where the TS compiler is doing the same thing that it's creating an instance variable on class and initializing it from object which is coming from DI container.

So my proposal is that can we have something like this in C#.

Following is how it will look like in world of C#.

public class EmployeeController : ControllerBase
{
   EmployeeController(private readonly IEmployeeService employeeService)
   {
       // no need to do initialization, it will get done automatically. 
   }
  
   public IActionResult Index()
   {
       var serviceData = employeeService.GetEmployees();
       return View(serviceData);
   }
}

In case, if we need to pass that instance to base class we can easily do that like following.

public class EmployeeController : BaseController // BaseController is a custom controller 
{
    // Passing the employeeService instance to base controller 
   EmployeeController(private readonly IEmployeeService employeeService) : base(employeeService)
   {
   }
  
   public IActionResult Index()
   {
       var serviceData = employeeService.GetEmployees();
       return View(serviceData);
   }
}

I think this is a cleaner way of achieving this because most of the time I find myself just doing initialization of instance service fields with the object that I got from DI container in the constructor and nothing else.

And already I was very surprised that TS has already had this feature and C# don't. Kinda strange. 🙄
But anyway, these are my thoughts about this thing.

Please let me know your concerns and recommendations. Always looking forward.

Thanks.

@yaakov-h
Copy link
Member

#3279 (comment)

@ShreyasJejurkar
Copy link
Author

#3279 (comment)

Records are kinda different thing.
It won't happen that records are there in c# and now people will stop using classes! 🙄

@333fred
Copy link
Member

333fred commented Jun 21, 2020

Closing as a duplicate. The syntax proposed here is a near exact copy of #3279.

@333fred 333fred closed this as completed Jun 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants