DI – Dependency Injection in MVC Part 1

C# Design Patterns and SOLID Principles Understanding Dependency Injection and Inversion of Control
13 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

Okay, so now it's the time to understand dependency injection with ASP. NET MVC web application. Now, before I proceed to this video, I would like to tell you one thing that I am assuming that you have a basic knowledge of a modern MVC architecture. If you do not have, I have a separate course on MVC, I request you go through that course. And you can understand more about MVC architecture and how the MVC application works. But in this video series, I'm just going to focus on how we can do dependency injection in MVC application.

In my with the studio 2017. I have a 300 application selected, and I'm giving a simple name of this which is MVC, underscore di. So we will do dependency injection with MVC application. And I'm going to click on OK. The moment I do this is going to show me a page where I can select a template. I'm going to select my MVC template and this is going to be an MVC application associated with the dotnet framework and then I'm going to click on OK. Now the moment we are done with this this is not going to be an empty project this is MVC project is going to have a well design architecture ready made homepage with some of the default controllers like home controller and all that.

And I hope you already know all this MVC application structure. So once this is done, we'll have a folder like models views controllers, and we will have number of feature classes already coded inside that. Yes, so a project is created and you can see we have a folder like models, views controllers, if I expand this controllers, we have a default controller associated with this which is home controller.cs. And my home controller is already having some code like we have three actions with the name index about and contact Now, technically this home controller is a simple C sharp class. But as you can see this home controller C sharp class is connected with one parent class with the help of inheritance and this parent classes controller Now, just to add this controller class belongs to system dot web dot MVC, which is the root level library of this particular MVC framework.

And now, inside this we have a full hierarchy of this controller class to understand this thing, let me show you one slide. Now, as you can see on screen right now, if we create any of the controller in MVC application like home controller, if I create few more like account controller or controller like that, all these controller classes are going to have one parent class which is controller. Now, this controller is predefined created in MVC architecture. And if you see the structure of that this controller class has a parent class with the name controller base class. And then controller base is also Having one particular parent which is nothing but an interface within the eye controller. So, when Microsoft has designed this MVC architecture inside this assembly system dot web dot MVC, they have actually already taken care of this dependencies of AI controller and controller base.

And looking at the structure you can understand that this is a loosely coupled architecture, which is based on this abstraction of a controller. Now, this is something which is very good thing of MVC architecture. But as well as this is also complicated for all the developers who want to do dependency injection with this. Now, in MVC architecture logic is going to be written inside controllers, it means, inside this home controller account controller or our controller, we are going to write our logic. And when we do this logic, if I want to inject any dependencies using my own code, I have to use a constructor of these classes. And maybe inside this class constructors, we can pass our own parameters.

But there are 13 things which you need to take it in this and that's what I am going to explain in this particular video. So now let's go back to the code. So I'm going to do some dependency injection with this home controller itself, I do not want to waste my time in creating your controller site now. So let's do one thing, I'm going to right click on my project. And just for my easiness, I'm going to add a new folder with the name maybe components. So inside this component folder, I'm going to add my own things.

The first thing which I'm going to add is I'm going to add a new item, which is going to be an interface with the name let's say I Lago now this interface is going to be a very simple interface, which is just going to have some method inside that. But in my case, I'm just taking one simple public interface which is allowing me to log something. So this one is just having one simple method with the name log and This log method is just going to log something whenever I'm going to implement this interface. So we have a simple interface. Now let's add some code. So I'm going to add a new item, which is a normal simple C sharp class.

And we can have multiple concrete implementation of this interface. I'm creating one right now. So I'm just giving a name of this class default logger.cs. And inside this default logger, the first thing is we are going to implement a logger so that we can get the method log. And inside this log method, we have to add some logic to logging somewhere. So let's say that we are going to add one parameter here, which is of type string, and that will be my log data.

And this log data is going to be that parameter which I will log so while calling this method will pass on this parameter which is of type string, and same with this should also have this parameter. And with that now we are going to call system dot diagnostics dot debug dot write line. And I hope you know it's going to write any particular string inside that. And we can pass any values inside this but right now, we are going to log our parameter which is log data. And with that we are just typing one word which is default. So this is the default logger.

Whenever I log this thing I can get to know that thing. So this is a very simple logic of this particular method. Now, right now, up to this point, what we have done in this application is nothing fancy. It's just one simple interface which is connected with one class default logger. As we know, we can implement this interface in many classes. So I can have many concrete implementations of this abstraction.

But now the complicated class will start. So we have only one interface in one class combination and now I Want to inject this particular thing inside my home controller. As we all know, if I have a home controller, this is a simple C sharp class, but it is actually MVC controller. So like all the simple C sharp classes, every class is going to have one default constructor. And if we want to create a constructor, we can create a parameter tester also in there. So let's say right now I have two consumers.

I have first one public home controller, which is going to be my default constructor without any parameters. And let's say I'm going to have one more. And this constructor is going to have one parameter, which will be it should be of type I logo. Now, as we know that we have an interface, and I want that this object should connect in a loosely coupled way, we should pass on the parameter of ilogger. So I'm creating one variable, which is private. Read Only variable of type I logo.

Okay, this is not showing the logo here because I need to add one namespace, which is components so I'm just adding that on the top which is component and then using this we are adding a logo and we have given a name of the variable which is underscore logo. So this is my private variable which I have just added inside this and now it's the time to pass on the parameter which is should be also type of AI logo. So we are adding a logo type of parameters inside this which is logo and inside the constructor we will just specify that whatever we are specifying inside this underscore ilogger This will take the values from the parameter logo. Now this is a simple Easiest thing which we can do in dependency injection in normal C sharp classes. So in our code, we just have two constructors.

One is the default constructor and other one is parameter constructor. And inside my parameter constructor, I have passed on the parameter of my interface, which is showing me that these two things are loosely coupled. And the instance of this logo should be added into this particular constructor at runtime. And that's what the rule of dependency injection is. Now the point is, because this is MVC, if I ask you one simple question, that right now, if I want to run this application, and if I run this, which particular constructor is going to call First, we have one default constructor, and another one is parameter constructor, which particular constructor this will call first. The answer is quite obvious.

This will always call the default constructor first. Because in MVC applications, we are not going to create an instance of this class manually. I know if it's a normal C sharp class, you can create like Class Name OBJ equals to new class name and in that you can pass on the parameter like we have done in our previous video. But in this case this is not a normal C sharp class. And in MVC application if you have a controller, we are not going to create an instance of a home controller like home controller equals to new home controller, the instantiation of this controller is going to happen automatically when you run this application. And that will be done with the help of MVC architecture.

Sometimes, to use this particular parameter inside of a home controller, some web developers they just put a colon here and using the base keyword or this keyword read two different keywords we can use this or we can use a keyword which is known as this using this they try to force that the default constructor is going to use this parameter. So they try to do like this and then they specify the instance of a new and then they specify a particular Last instance associated with that. Now, if I try to do something like this, maybe core wise right now this is going to work. But is this the right way to do dependency injection in MVC? The answer is no. Actually, this is not at all dependency injection, if you try to do something similar to this and if you have done similar kind of thing in your MVC project, then you have made a mistake.

Now as per the dependency inversion principle, now I want to add one line. Now in this case, when I'm dealing with this, this is not at all dependency injection, because as for the dependency inversion principle, the high level modules, the high level modules like home controller and controller should not depend on the low level module like default logo. In this case, if you check the instance of default lager is tightly coupled with my home controller and this is something which is a total dependencies. So what we are trying to do right now is our high level modules are dependent on the low level modules and As for the dependency inversion principle, it is not true. A high level module should not depend on low level modules actually, high level module and low level modules both should depend on the abstraction. This is something which is also a rule of dependency inversion principle, intentionally when I covered dependency inversion principle, I have not focused on this because at that time you guys were not able to understand that.

So, right now, in this particular video, I'm saying that if you're doing something like this, this is not a right way because technically high level modules and low level modules both should depend on abstraction. In our case, the abstraction is this particular I love. But if you try to do something like this, output will work code will work, but this is not a dependency injection at all. Or this is not a right way to do that thing in MVC. So this is something which I don't want you to do. And that's why I'm going to remove this thing.

Now the question comes to this that if this is an Only then which is the right way to do dependency injection in MVC. Now, that's something which I'm going to show you in my next video. Stay tuned. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.