AddSingleton vs AddScoped vs AddTransient

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is putt 44 of asp.net core tutorial in this video we'll discuss the difference between add singleton adds scoped and add transient methods in asp.net core with an example this is the same application that we've been working with so far in this video series here is our I employ a repository interface in this example we'll only be using these two methods get all employees and add as the name implies the sket all employees method returns us the list of all employees and this add method adds the new employee that we provide to this method as an argument to our repository and this is just the interface here is the class that implements our interface I employee repository here's the implementation for add method as you can see the new employee object that we are providing as an argument to this method it is adding that employee to the private field underscore employee list which is obviously a list of employee objects and then if we look at get all employees method this method is returning us the list of all employees that we have again in this private field underscore employee list the important point to keep in mind is this is an in-memory list meaning every time we restart our application will lose the changes that we make to this private field underscore employee list whenever we create an instance of this class mock employee repository obviously its constructor is called and as you can see within the constructor we are initializing this private field underscore employee list with three employee objects to start with here is our startup dot CS file and within this file we have a startup class and within the class configure services method as the name implies this is a function that we use to configure services required for our application as a moment we have this interface I employee repository and this mock employee repository class provides the implementation for that interface so to tie these two together we are using add singleton method of this interface i service collection so basically by including this line here we are saying if someone asks for I employee repository then provide them and in students of the smock employee repository class in addition to add singleton method we also have these two methods add scope and add transient for now let's use this add singleton method and see the behavior that we get here is of a home controller notice the home controller has a dependency on I employee repository we are injecting I employee repository into the home controller using its constructor we are then using this injected dependency within the create action method this is the create action method that responds to the HTTP POST request here's our create employee form after we provide all the details of the new employee and then when we click this create button this form is posted to the server this HTTP POST request is handled by this create action method the new employee that we want to create is passed as a parameter to this create action method which we are then passing to the add method of employee repository this method adds it to the employee repository after we have successfully added the employee object to the repository we are then redirecting the user to the detail section method so we could see the details of that newly added employee for the time being let's not redirect the user to the detail section after we successfully add the employee we want to stay on the create view itself so let's comment this line which redirects us to the detail section method next on our create view we want to display the total number of employees that we have in our employee repository for that let's make a copy of this development and then change the bits that are required we want to display the total employees count right here we don't need another create button so let's get rid of that first we want to include this little text total employees count equals now there are several approaches to compute the total number of employees that we have in our employee repository the approach that I am going to take is inject employee repository service into this view as well now remember to inject the employee repository service into a home controller we are using the constructor to be able to inject a service into view we use at inject directive in our case the service that we want to inject is I employee repository let's call the variable underscore EMP repository we can now use the service instance to compute the total number of employees so at this location we want to switch to c-sharp mode so we use add on this service we have get all employees method which is going to return us the list of all employees on this and we use the count method we get the total number of employees now here's the important bit to understand after we provide the details of the new employee that we want to create and issue a post request by clicking this create button to handle this post request our application needs an instance of AI employee repository in two places first within our home controller in this create action method that responds to the post request so to be able to add the new employee to our employee repository we need an instance of I employee repository service next we also need an instance of the service in our create view why do we need it in the create view to be able to compute the total number of employees that we have in the employee repository and at the moment we have registered our service using add singleton method so with all these changes in place let's see the behavior that we get notice when we issue a get request to slash home slash create we see the total employees count is 3 let's understand what's happening behind the scenes to serve this request an instance of home controller is created and if we take a look at our home controller notice it has a dependency on I employee repository this is the first time an instance of I employee repository is requested so asp.net core creates an instance of the class that implements this interface as you can see in this configure services method in our case an instance of mock employee repository class is created and injected into the home controller we have issued a get request to the create action of our home controller so within our home controller this create action method that handles the incoming HTTP GET request all we are doing within this action method is executing return new statement since the name of the action method is create this create view is then executed if we scroll all the way to the top you can see this view also has a dependency on I employee repository since we are using add singleton method to register of a service instead of creating another instance of this mock employee repository class for the create view it is going to reuse the instance that asp.net core has created when this home controller has first requested an instance of I employee repository so that same instance will also be injected into the create view the view then uses the service instance to calculate the total number of employees if you recollect when we create an instance of the service class within the constructor we are populating this private field underscore employee list with 3 employee objects so the total count of employees that we see here is 3 now let's add a new employee and see what happens to the total count notice when I click the Create button the total employees count is 4 if I click it again it increases to 5 one more time 6 it goes on so every time we click this button the employee count increases by 1 the important point to keep in mind is every time we click this create button we are issuing a new HTTP request since we are using add singleton method to register of a service there's only one instance of the service created when the application first requests an instance of the service that instance is then reused throughout our application across all the HTTP requests that is the reason every time we click this create button the total employees count increases by one with a singleton service there's only a single instance an instance is created when the service is first requested and that single instance is used by all HTTP requests throughout the application now let's use add scoped method instead of ADD method to register a service and see the behavior that we get with a scoped service we get the same instance within the scope of a given HTTP request but a new instance across different HTTP requests let's understand what this means with an example we made a change to our application code so when we issue a get request the application restarts and we should see the count of employees as three there we go now let's provide the details of a new employee and click the Create button notice the employees count increased to four from this point no matter how many times we click this create button the total employees count stays at four let's understand why we are getting this behavior when we issued a get request to slash home slash create we get a new service instance because remember with a scoped service every new HTTP request gets a new service instance with a new service instance we are creating three employee objects so when we issued a get request to slash home slash create we saw the total employees count as three we then provided the details of a new employee and then issued a post request by clicking the create button when we issue a post request within our home controller this is the three action method that handles that incoming post request remember when we issue a post request it is another new request so we get another new instance of the service with a new instance of the service by default we have a total of three employees but then they create view within our home controller is adding another new employee so that total count increases - for our create view can see that increased account because after adding the employee to the employee repository we are rear-ending the create view and if we take a look at the create view create view also has a dependency on I employee repository service but here a new instance of the service will not be provided the instance that we have used to add the employee to the employee repository that same instance is reused in the create view why is that that's because both these actions that is adding the employee to the employer repository and rendering this create view are happening as part of the same HTTP request they are happening in the same scope of that HTTP request and remember with a scoped service we get the same instance within the scope of a given HTTP request but a new instance across different HTTP requests so when this create button is clicked adding this new employee to the employee repository and re-rendering this create view both these actions are happening in the scope of this same HTTP POST request so we see the total employees count increased to four after the value is increased to four no matter how many times we click this create button the account does not go beyond four why is that well that's because every time we click this create button we issue a new HTTP request and remember with a scope service with every new HTTP request we get a new service instance and with a new service instance the default initial count of employees is three and when we click the Create button we add the fourth employee so we see the total employees count as four next time I click the Create button we get another new service instance and with that new service instance again the initial count of employees is three where a new employee the count becomes four and that's what we see in the browser so every time we click the Create button the same process repeats and hence the count doesn't go beyond four because with every request and new service instance is created with a scope service now let's use add transient method instead of add scoped method to register a service and see the behavior that we get with the transient service and new instance is provided every time an instance is requested whether it is in the scope of the same HTTP request or across different HTTP requests notice when we issue a get request to slash home slash create we see three as the total count of employees now let's provide the details of a new employee and then click a button notice no matter how many times I click the Create button the total employees count does not move beyond three let's understand why this is happening remember with a transient service and new instances provided every time an instance is requested whether it is in the scope of the same HTTP request or across different HTTP requests so if we relate this to our example notice our home controller is asking for an instance of I employee repository but that transient service we get a brand-new instance so with a new instance of a service the initial default count of employees is 3 and then the create action within our home controller is adding a new employee to the employee repository so that count becomes 4 and then it rear Enders the create view and if we take a look at the create view notice the create view is also asking for an instance of my employee repository remember again with a transient service every time we ask an instance we get a brand new instance so another new instance of the service is created and but this new instance again the initial count is 3 and that is what we see on the web page so the point that I'm trying to make is home controller has its own instance of the service instance and the create view has its own instance of the service instance so this obviously means that change is made by the home controller to its service instance cannot be seen by the create view and the vice versa is also true the table on this slide summarizes the difference between a scope service transient service and a singleton service that's it in this video thank you for listening you you [Music]
Info
Channel: kudvenkat
Views: 118,305
Rating: 4.9421902 out of 5
Keywords: addsingleton .net core, addsingleton example, asp.net core addsingleton example, asp.net core addsingleton vs addscoped, c# asp.net core addsingleton, asp.net core addtransient vs addscoped, asp.net core addscoped example, asp.net core addscoped vs addtransient, asp.net mvc addtransient, asp.net mvc addscoped
Id: v6Nr7Zman_Y
Channel Id: undefined
Length: 15min 23sec (923 seconds)
Published: Wed Apr 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.