ASP.NET MVC Interview questions and answers on Dependency Injection | ASP.NET Interview Questions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to quespond's youtube channel and what are we doing today today we will be running through five important asp.net interview questions around dependency injection and inversion of control when you are talking about asp.net interviews i'm dead sure that there will always be a question on dependency injection inversion of control what is the benefit of di you know the difference between scoped versus transient versus singleton where the interviewer takes you for a nice ride right so in this 20 minutes of video i can tell you that if you go through this video properly you should be able to answer all of these five questions right uh and before i start right i would like to just make two big announcements out here you know in this channel you know we have one hour of video which covers 25 important asp.net mvc interview questions with answers so down in the description link there is one hour of video which is on 25 important asp.net mvc interview questions with answers i would suggest please go through that video i am dead sure if you cover that one hour of video it is more than enough to cover uh in a lot of your questions to face lot of your questions during the interviews right and here is a small gift if you go and if you share this video somewhere on your linkedin on your facebook on your twitter you will get this ebook for free so once you share please go ahead and send a mail at you quespondquest1.com we would be more than happy to send you this ebook so let us get started with five important asp.net interview questions around dependency injection and inversion of control now before we get into the different types of dependency injection like scoped and transient and singleton you know first let us try to understand what exactly is dependency injection um and what is the benefit of it i'm sure that every interviewer out there who's taking asp.net interviews the first thing he will ask a basic concept and then he will prove further right so let us try to answer this question what exactly is dependency injection now always remember the golden rule of interviews always answer to the point do not hit around the push do not go in circles right so here it is dependency injection is nothing but it is a methodology wherein rather than caller creating the instance it is injected by someone else or it is injected by some framework or something right so caller means for example now you can see this is a home controller so rather than this home controller saying my db context x is equal to new my db context so rather than he creating the instance we just ask that in the constructor and it is injected by someone else now this someone else can be a framework this someone else can be any other mechanism for example here when we say in our configure services in the startup.cs services.ad scoped so the asp.net services actually goes and injects automatically this mydb context so i repeat the answer dependency injection is nothing but it is a methodology wherein rather than the caller creating the instance it is injected by someone else one more connected question which can come around dependency injection is what exactly is inversion of control now remember that inversion of control is not a technical thing it is more of a concept you know where we say that any kind of unnecessary things what you are doing you should delegate it to someone else and you should focus on your work for example this home controller out here will take a request it will actually display the home page it will do some logic you know which is against the ui and so on but his work is not to create the instances not to decide you know which instance to be created right so you can see here you know by using dependency injection we are implementing inversion of control concept so dependency injection is you know is like a technical thing which implements inversion of control so inversion of control is just a concept it is not even more of a software thing it is just a concept you know where we say that any kind of unnecessary things whatever that code is doing you should delegate it to someone else and that code should focus on his task and not on others task right definitely the another thing you know which the interviewer will surely ask is that what is the benefit of dependency injection now again answer to the point the main benefit of dependency injection is that you change at one place and the changes will reflect at a lot of places it helps you to achieve decoupled architecture decoupled architecture means for example now this home controller is just receiving db context he has no idea that this db context can be an sql server context it can be a mongodb context it can be a file context it can be an oracle context right so what i have to do now is you know so everywhere in all my controllers i will just say that receive this dependency injection that is mydb context and then from at one place i can say that wherever there is my db context inject sql server context so i change at this one place out here and all over my project sql server context is injected and if you say like okay now we are changing from sql server to oracle no problem just you say oracle context right so the whole goal of dependency injection is good architecture and how when what exactly is a good architecture a good architecture is a decoupled architecture where you change at one place or at less places and the changes is reflected across many of your code right so that's what exactly is dependency injection once you have answered the basic questions like what is dependency injection and what is the use of it and so on the interviewer will surely raise his bar ahead and say that okay what is the difference between scoped versus transient versus singleton right so first thing again to the point answer all of these three techniques you know define the lifetime of the injected object so whatever is your object injected out here all of these three things this scoped and singleton and transient they define you know that how much time that object will be alive and how the injection will happen so let us let us go more in depth in in depth so first let us start with singleton when you say services dot add singleton and when you go and you ask for that service out here for example you can see here i have said my services dot add singleton caching and here it is this is caching right only one instance of the object is created for all the http requests for all the clients for everything so throughout the server throughout the server there is one big global instance which is created for example you can see here this might this caching right caching object what i've done if you see the caching object code what i've done i have actually you know once the object is created i have created a guid now in case if fresh instances are created then this guide would be different right but because you know fresh instances will not be created for every request every request the same instance will be injected you know so for example now if you see here if i run this so there it is you can see now if you go here to caching you will find that this is the guide ah cache so this is the guide out here now let us say i request again so let's say i again i send a request to home you can see the same guid 0 dd 0 d9 0 dd 0 d9 so even if i say again 0 dd 0 d9 even if i log in as another user means for example if i open in a cognito mode in a cognitive mode we can think about that this is like a new user but even if it is coming from a different user also it's the same instance so when you actually go and say singleton that means the same instance of the object is created throughout the project right now let us talk about add scoped so one more way of injecting the object is by saying add scoped now when you are explaining this answer to the interviewer right two statements should come out very distinctly and clearly so that there is no confusion that with the interviewer the first one is for every single request ad scoped will inject a new instance i repeat this statement for every single request either it comes from the same user or either it comes from a different user the instances will be new for example you can see here this my db context is injected in a in a scoped way so if you see now this mydb context the guid it is now showing 656 dfc right now if i again come back now this is this is one request which is completed now i'm going for the second request for second request again you can see new instance is created there's a new guid e6df fe26 either come from the you are either sending the request from the same browser or if you are a new user right for example you can see now i'm trying to come as a new user different instance ba2 c86 so remember for every request different different instances will be injected so this is the first statement you will make the second statement is but within the same request if you are asking to dependency inject the object two times or three times the same instance will be injected means what for example now over here if i go and if i ask for my db context again let us say obj one or let us say i ask two times or three times so if i dependency inject in the constructor multiple times right the same instance will be injected so you can see now this is obj2 so now if i run this for the same request he will inject the same instance in a single http request right so if i run this so there it is you can see now this is obj guide is this let us let us look into obj1 so if you look at obj 1 the same instance if you look at obj 2 [Music] the same instance so you can see here within a same request the scope will dependency inject the same object so if you ask many times the object it will be dependency injected with the same instance okay so remember when you are explaining scope two statements for every http request new instance will be created of the db scoped object but within the singles http request if you are asking for the object two times or three times or four times the same instance will be injected so now let us talk about transient again when you are talking about transient to the interviewer two statements should come out point number one for every request a new instance will be created so same like scoped for every request a new instance will be created but within the request if you ask for the object multiple times new new instances will be injected i repeat this this is the second statement which is very opposite to scoped in case of scoped the same instance was injected but in case of transient new new instances will be injected within the same request right so if if i run this so there it is so if you see here this is obj guid now let us talk about obj1 you can see now obj one guid is different than obj obj2 guid is different than obj one and two so you can see all the guids are different right so two statements to explain transient first for every request a new instance will be created and within that same request if you ask the object two times or three times or multiple times in the constructor fresh fresh instances will be injected now comes the final nail in the coffin when should we use what so in what situation you will use uh scoped in what situation you will use transient and in what situation you will use singleton so let us first start with singleton singleton you know as it says it's one big global object you know which is shared across the whole server or whole website right so you know the situations you know where singleton will be used like caching you are caching country master state master currency master or configurations you know where you don't want to go and hit the database and take the database trip right so one is caching second one like hit counters you know you want to just and you know just count you know you know how many times you know the website was hitted by how many users hit counters any kind of shared service any kind of shared data so singleton is the most easiest one keep some good examples ready like i've given some examples here caching hit counter and shared services now let us talk about scoped in case of scoped uh first thing you know most of the time you will be using scoped so most of the time whenever you say you want to do dependency injection uh the singleton is very clear but when it comes between choosing between scoped and transient most of the time it will be scoped that is but natural because you know when you uh you know in a single request you would like to reuse the same instance right most of the time but now you need to remember that when you are saying scoped all of these instances created here point to the same reference means what i mean is if let's say you go here and if you change let us say this obj dot value to let us say shif everyone's value that is obj2 and obj obj will also change because it is the same instance so look at this so remember when you're talking about scoped the same instance is injected so if you see here for now if you see obj one's value is null obj once value is null object value is null obj 2 value is null but if i make like if i go and change any one of these instances because in the startup.cs i've said this is add scope so if i go and make any change in one of the objects all the objects value is changing you can see because they are pointing to the same instance right and definitely you know what you would like to do is by default you would like to have ad scoped you know for example let us say you are using this db context you know my db context so you see my db context uh so you you would like that in the single request you know all the objects should be added in the same commit and same transaction and the same context so for example let us say you say your ad let us say you'll say your add some value right some value one and let us say you use the second instance like obj one and you say okay again this data is connected to some value one right connected to some value one assume you know so like you are adding customer and you are adding the addresses of that customer so that means that if i say anywhere obj.com it everyone should get committed isn't it so in case you know when you're talking about like db context most of the time you will use scoped right why because you want to ensure that in the same connection in the same context you would like to do a commit you would like to do a rollback so most of the time it will be scoped now let us talk about transient right this is where you know some good examples we should have so first thing i will use transient you know in case in my constructor i'm i'm putting obj 1 obj 2 because here it definitely means that you are expecting new new copies of the instance or else you know if you are if you are saying scoped right it you you just put one value so when you are putting multiple values right that means already the decision is done that you want separate separate copies of the instance right so that is one sign you know where i see that add transient by default comes in but now think about it this obj is adding some value 1 so this obj dot comment will commit only value one now obj one is adding some another value right some another value and his commit is different so when you want that the instances should not have any connection between them they are separate instances they are separate copies for example you would like to say okay use this context you know to add some value one and it has no connection with some other value that is a different transaction right so in this case that means if it is a different transaction the context has to be different and it should be a fresh instance so you know in situations you know where you want complete different cloned copy and you want to operate on that one copy and the operation of the one copy should not affect the other copy you will use transient so summarizing when you talk about singleton shared service caching hit counter you know and those kind of things scoped you know if you want that the same instance has to be used in the same request like example you want that all the transactions should belong to one db context then you will use scoped but if you say no like you know i need two different copies and one copy should not affect the other right then definitely it is uh transient right and one of the other example of transient is like multi-threading in case of multi-threading you would like to create two different instances because you do not want any kind of multi-threading uh what you call side effects you know so one instance will work in one copy another instance will work in another copy right so just remember these examples while you're speaking in the interview so that brings us to the end of this session hope you have enjoyed this session in this session we covered the five important questions and don't forget if you go and if you share this video on your facebook on your twitter on your linkedin you will get this ebook for free you know which has around 40 plus questions around asp.net interviews interview questions with answers right and second don't forget there is a one hour of video you know which covers almost 30 questions around asp.net interview question with answers down below happy learning happy job hunting you
Info
Channel: Questpond
Views: 37,943
Rating: 4.9391479 out of 5
Keywords: asp.net Interview questions, dependency injection in asp.net, interview questions on asp.net, dependency injection interview questions, ASP.NET MVC Interview answers, asp.net interview Questions and Answers, asp net interview questions, model view controller MVC interview questions, MVC core interview questions, ASP.NET core interview questions, ASP.NET dependency injection interview questions, asp.net mvc, ASP dot net interview questions, interview questions asp.net mvc, DI mvc
Id: yVKTvtevM0w
Channel Id: undefined
Length: 19min 55sec (1195 seconds)
Published: Tue Aug 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.