Working with Services without using Dependency Injection | ASP.NET Core 5.0 Web API Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
before learning the practical concept of dependency injection in asp.net core web application let's create few services in this application and let's try to establish the communication with the controller you can create your services in the same application or you can create one class library application and you can define all the services over there to define the services in the same application let's create a new folder let's give it a name depository and let's add a new product repository this repository is a very simple class now let's add a corresponding model in the models folder inside this model let's add few properties just for the learning let's keep only two properties in this product model now inside this product repository let's add few methods and because we have not introduced the database as of now so we will be using in-memory data and for that let's create a new field over here now inside this add product method whatever data we will get in this product object we will add it to this products list let's do that products dot add and over here let's use this product object now let's add some code to add the id automatically so we can write product dot id is equals to products dot count plus one and we can simply return this id from this method that's it let's add one more method to get all the products and this time we can simply return only the products that's it we have created this very simple repository in this application and now let's add a new controller in this application use the api controller hit this api controller empty click on the add button give it a name this time we can write product controller hit the add button and inside this controller let's define one method now in this application we have a controller we have a repository now it is time to establish the communication in between the controller and the repository and for that first we will be using the old way so for that we can write private deal only the type of our repository that is product repository let's resolve the namespace create a new constructor in this controller and now by using this product repository we can call all the methods that we have defined in this repository like this dot add product once we will add the product in the repository we will get all the products in a new variable and for that we can use the same repository product repository dot get all products and we can simply return all these products in the 4k method that's it now let's run this application in debug mode and let's put a breakpoint over here open the postman and over here we can write the url this is the domain then we have the api and then we have product and we are using a post method so we have to choose post from here and inside the body we need to pass some data because we are passing only the name let's say the name is start just hit the send button and here you can notice the output whatever data we are sending over here in the body of the request we are getting the same data as an output but here we have the id of the product also if i try to send some other data like start one hit the send button then this time the previous data is gone but we have the new data if we put a breakpoint over here just to debug the code hit the sun button every time we are sending a new request from the client to this application new instance of this product repository is created for us and we are using the new instance in all the calls so here we go the data the id is 0 and this is the name of the product hit the f10 button to go inside this method let's use the f11 the count of the products is 0 that is why the id will be 1 because we are having 0 plus 1 so the id will be 1 we have added a new product in this list successfully let's verify so here you can notice that we have one product in this products list click the f10 button and here we are in the second goal so from this method we are getting all the products and we are simply returning all of them over here in this okay method so here you can see that we have only one product and it is working fine we have already discussed some drawbacks of using the services in the controller by using this approach let's focus on all of them once again at this time in this application you can see that we are using this product repository and in this product repository we are having only the in-memory data but for example suppose in future we are having a new actual repository that is working with the database let's say the name is sql product repository or in future you are also working with mysql so the name of the repository is mysql product repository now let's say you got a new requirement that instead of working with this product repository we have to work with the sql repository so what we will do we will go to the product controller and over here we will update the name of the repository it is very simple but no now here since we are learning about the concept only so we are having only one controller now just assume that we are using this service inside 50 controllers so we have to make changes in all those 50 controllers and that is not a best approach the second problem is that if you will try to write the unit test case for this add product method then you will be in trouble because every time you will call this method the new instance will be created automatically and to resolve all these problems we work with the dependency injection in few upcoming videos we will talk about the dependency injection way of using the services in the controller file
Info
Channel: WebGentle
Views: 17,219
Rating: undefined out of 5
Keywords: asp.net core web api, web api asp.net core 5, web api asp.net core, .net 5 web api, api in .net core 5.0, asp.net core web api tutorial, asp.net web api core 5, Restful web api asp.net core, how to build restful apis with asp.net core 5, how to build restful apis with asp.net core, asp.net core, asp.net core web api tutorial for beginners, restful web api tutorial asp.net core, asp.net core web api tutorial webgentle, asp.net core web api tutorial nitish
Id: pCzrw2CPA4g
Channel Id: undefined
Length: 7min 29sec (449 seconds)
Published: Fri Apr 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.