You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
exportclassEmployeeComponent{constructor(privateemployeeService: 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#.
publicclassEmployeeController:ControllerBase{EmployeeController(privatereadonly IEmployeeService employeeService){// no need to do initialization, it will get done automatically. }public IActionResult Index(){varserviceData= employeeService.GetEmployees();return View(serviceData);}}
In case, if we need to pass that instance to base class we can easily do that like following.
publicclassEmployeeController:BaseController// BaseController is a custom controller {// Passing the employeeService instance to base controller EmployeeController(privatereadonly IEmployeeService employeeService):base(employeeService){}public IActionResult Index(){varserviceData= 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.
The text was updated successfully, but these errors were encountered:
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.
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.
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#.
In case, if we need to pass that instance to base class we can easily do that like following.
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.
The text was updated successfully, but these errors were encountered: