Welcome to QuestPond's YouTube channel
We will be running through 5 important asp.net interview questions around Dependency Injection
and Inversion of Control.
In asp.net interviews there is always be questions on dependency injection, inversion of control,
what is the benefit of DI, what is scope vs transient vs singleton where interviewer takes
you for a nice ride. If you go through this 20 minutes of video properly you should be
able to answer all of these 5 questions.
In this channel we have 1 hour of video which covers 25 Important asp.net mvc core interview
questions and answers. Go through that video, if you cover that 1 hour of video it is more
than enough to face lot of questions during the interview. If you share this video somewhere
on your LinkedIn, Facebook, twitter you will get this e book for free. After sharing the
video send email at quespond@questpond.com
Let us get started with 5 important asp.net interview questions around dependency injection
and inversion of control. Before we get into the different types of dependency injection
like scope, transient and singleton. First let us try to understand what exactly is dependency
injection and the benefit of it.
Every interviewer who is taking asp.net interview will ask a basic concept and then go further.
Always remember the golden rule of interviews always answer to the point do not beat around
the bush.
Dependency injection is a methodology wherein rather than caller creating the instance it
is injected by someone else or injected by some framework or something.
This is HomeController so rather than this HomeController saying MyDBContext x = new
MyDBContext rather than it creating the instance we just ask in the constructor and it is injected
by someone else, this someone else can be the framework or any other mechanism. When
we say our configure, startup.cs, services.AddScoped the asp.net services inject automatically
MyDBContext.
Dependency Injection is a methodology wherein rather than the caller creating the instance
it is injected by someone else. One more connected questions which can come around dependency
injection in asp.net is what is Inversion of Control? Inversion of Control is not a
technical thing it is more of a concept where we say any kind of unnecessary things what
you are doing should be delegated to someone else and you should focus on your work.
This HomeController will take the request and display the home page, it will do some
logic which is against the UI and so on but its work is to create the instances not to
decide which instance to be created. By using DI we are implementing IOC concept. Dependency
Injection is like a technical thing which implements Inversion of Control. IOC is just
a concept where any kind of unnecessary things whatever that code is doing should be delegated
to someone else and that code should focus on its task and not on others task.
The another thing which interviewer will surely ask is what is the benefit of dependency injection?
The main benefit of DI is that change at one place and changes will reflect at lot of places.
It helps to achieve decoupled architecture means this HomeController is just receiving
DBContext it has no idea that DBContext can be a SQLServerContext, MongoDBcontext, FileContext,
OracleContext. Every where in all the controllers will say receive this dependency injection
i.e MyDbContext then from one place I can say whenever there is MyDbContext inject SQLServerContext.
I changed at this one place our here and all over the project SQLServerContext is injected.
If changing from SQL Server to Oracle just say OracelContext. The whole goal of Dependency
Injection is good architecture and what exactly is a good architecture? A good architecture
is decoupled architecture where you change at one place or at less places and the changes
reflected across many of the code. That's what exactly is dependency injection.
Once the basic question is answered what is dependency and what is the use of it and so
on, the interviewer will surely raise his bar ahead and say what is the difference between
Scoped vs Transient vs Singleton?
To the point answer all of these three techniques define the lifetime of injected object. Whatever
is the object injected our here all of these three things is scoped, singleton and transient.
They define how much time the object will be live and injection will happen. Let us
start with singleton, when we say Services.AddSingleton and when you ask for that service, I have
set Services.AddSingleton
only one instance of the object is created for all the http request or all clients for
everything. Throughout the server there is one big global instance which is created for
example in this caching object once the object is created i have created a guid. Incase if
fresh instances are created this is guid would be different. Because fresh instances will
not be created for every request the same instance will be injected.
If I run this and in caching will find that this is the guid and send a request again
to home the same guid "0dd...........0d9" even if I login as another user or open in
incognito mode like a new user but even if it is coming from a different user also the
same instance.
When we say Singleton that means the same instance of the object is created throughout
the project.
Let us talk about AddScope, one more way of injecting the object is by using AddScoped.
When you are explaining this answer to the interviewer two statements should come out
very distinctly and clearly so there is no confusion with the interviewer. The first
one is for every single request AddScoped will inject a new instance. Every single request
either it comes from the same user or different user the instances will be new.
This MyDbContext is injected in a scoped way. Now this MyDbContext guid is now showing "c56.............dfc".
If I again come back, this is one request which is completed now doing second request
we can see new instance is created the new guid "e6df.............fe26". Either sending
the request from the same browser or new user. In this example i am trying to come as a new
user.
For every request different different instances will be injected, this is the first statement
you will give. The second statement is within the same request if you are asking dependency
injection inject the object two or three times the same instance will be injected means over
here if i will ask for MyDbContext obj1 or I ask two and three times. If i dependency
inject in the constructor multiple times the same instance will be injected. For the same
request it will inject the same instance in a single http request. This is obj guid , obj1
has same instance and obj2 has same instance. Within a same request the scoped will do dependency
inject the same object. If you ask many times the object it will be dependency injected
with the same instance.
When you are explaining scope two statements :- For Every http request new instance will
be created of the DbScoped object but within the single http request if you are asking
for the object two times or three times or four times the same instance will be injected.
Let us talk about Transient , when you talk about transient to interviewer two statements
should come out, point no. 1 For every request a new instance will be created so same like
scoped for every request a new instance get created.
Within the request if you ask for the object multiple times new new instances will be injected.
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. If I run this, here is obj.guid is different than the obj1.guid and obj2.guid.
All the guids are different.
Two statements to explain transient.....
Now comes the final nail in the coffin, when should we use what? In what situation we will
use Scope and Transient and Singleton?
Singleton is one big global object which is shared across the whole server or whole website
where singleton will be used like caching, your caching master state, master currency,
master configuration where we don't want to hit the database. Second one is HitCounter,
you want to just count how many times the website was hit by how many users hit counter.
Any kind of SharedService or SharedData singleton is a most easiest one keep some good examples
ready like here some examples given caching, hitcounter and SharedService.
Let us talk about Scoped, in case of scoped most of the time you will be using scoped.
Whenever you say you want to do dependency injection the singleton is very clear but
when it comes choosing between scoped and transient most of the time it will be scoped
because in a single request we use the same instance most of the time.
Remember that all of these instances created here point to the same reference means if
we change here this obj.value to shiv everyone's value obj2 will also change because it is
a same instance. When we talk about scoped the same instance is injected. For now obj1's
value is null, obj's value is null, obj2's value is null. If I change anyone of these
instances, in startup.cs I have set this is AddScoped and if I make any change in one
of the objects, all the object's value is changed because they are pointing to the same
instance.
By default we have AddScoped, let us say using this MyDbContext and in
single request all the objects should be added in the same committ, same transaction and
same context. Add the some value1 and second instance obj1.Add this
data is connected to some value1. Adding customer, adding address of that customer. That means
if I obj.Committ everyone should get committed so when we talk about DbContext most of the
time will use code to ensure in the same connection in the same context you would like to do a
committ, do rollback. Most of the time it would be scoped.
Let us talk about Transient, this is where some good examples we should have. Use transient
if in the constructor we putting obj1, obj2 because here it definitely means we expecting
new new copies of the instance or saying scoped would just put one value. Putting multiple
values means already the decision is done for separate separate copies of the instance,
that is one sign where I see AddTransient by default comes in. This obj is adding Some
Value1 so this obj.committ will commit only value1. Obj1 is adding some another value
with different committ.
When we want the instances should not have any connection between them they are separate
instance, separate copies like use this context to add Some Value1 and it has no connection
with some other value that is a different transaction. In this case that means it is
different transaction, the context has to be different and it should be a fresh instance.
In some situation we want complete different cloned copy and want to operate on that one
copy and the operation of the one should not affect the other copy use Transient. Singleton
is about Caching, Shared Services, HitCounter and other things. Scoped is used for same
instance has to be reused in the same request like all the transaction should belong to
one DbContext use Scoped but for two different copies where one copy should not affect the
other then it is Transient.
Another example of transient is multi-threading, in multi-threating we create two different
instances to avoid any kind of multi-threading side-effects. One instance will work in one
copy and another instance will work in one copy. See these examples while speaking in
the interview.
Hope you have enjoyed this session where we covered the 5 important questions and do not
forget to share this video on your Facebook, twitter or LinkedIn to get this this free
e-book which has around 40+ questions around asp.net interview questions and answers second
don't forget there is a one hour of video which covers almost 30 Questions for asp.net
interview questions with answers.
Happy Learning.... Happy Job Hunting....!!!