DI – Dependency Injection in MVC Part 2

C# Design Patterns and SOLID Principles Understanding Dependency Injection and Inversion of Control
10 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
€66.45
List Price:  €94.93
You save:  €28.48
£55.32
List Price:  £79.03
You save:  £23.71
CA$98.04
List Price:  CA$140.06
You save:  CA$42.02
A$107.72
List Price:  A$153.89
You save:  A$46.17
S$94.09
List Price:  S$134.42
You save:  S$40.33
HK$544.68
List Price:  HK$778.15
You save:  HK$233.46
CHF 61.92
List Price:  CHF 88.46
You save:  CHF 26.54
NOK kr774.79
List Price:  NOK kr1,106.89
You save:  NOK kr332.10
DKK kr495.67
List Price:  DKK kr708.13
You save:  DKK kr212.46
NZ$118.82
List Price:  NZ$169.76
You save:  NZ$50.93
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,339.40
List Price:  ৳11,913.94
You save:  ৳3,574.54
₹5,914.23
List Price:  ₹8,449.26
You save:  ₹2,535.03
RM311.24
List Price:  RM444.65
You save:  RM133.41
₦118,083.62
List Price:  ₦168,698.12
You save:  ₦50,614.50
₨19,391.36
List Price:  ₨27,703.14
You save:  ₨8,311.77
฿2,410.17
List Price:  ฿3,443.25
You save:  ฿1,033.08
₺2,423.71
List Price:  ₺3,462.60
You save:  ₺1,038.88
B$418.75
List Price:  B$598.25
You save:  B$179.49
R1,273.04
List Price:  R1,818.71
You save:  R545.66
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩97,732.12
List Price:  ₩139,623.30
You save:  ₩41,891.18
₪255.55
List Price:  ₪365.09
You save:  ₪109.53
₱4,110.61
List Price:  ₱5,872.56
You save:  ₱1,761.94
¥10,630.43
List Price:  ¥15,186.98
You save:  ¥4,556.55
MX$1,418.07
List Price:  MX$2,025.91
You save:  MX$607.83
QR254.36
List Price:  QR363.39
You save:  QR109.02
P953.37
List Price:  P1,362.02
You save:  P408.64
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.89
List Price:  E£4,970.06
You save:  E£1,491.16
ብር8,820.94
List Price:  ብር12,601.88
You save:  ብር3,780.94
Kz63,759.63
List Price:  Kz91,089.09
You save:  Kz27,329.46
CLP$68,404.72
List Price:  CLP$97,725.22
You save:  CLP$29,320.50
CN¥507.53
List Price:  CN¥725.08
You save:  CN¥217.54
RD$4,206.74
List Price:  RD$6,009.89
You save:  RD$1,803.14
DA9,351.13
List Price:  DA13,359.34
You save:  DA4,008.20
FJ$158.85
List Price:  FJ$226.94
You save:  FJ$68.09
Q538.41
List Price:  Q769.20
You save:  Q230.78
GY$14,600.65
List Price:  GY$20,858.96
You save:  GY$6,258.31
ISK kr9,615.92
List Price:  ISK kr13,737.62
You save:  ISK kr4,121.70
DH699.24
List Price:  DH998.96
You save:  DH299.72
L1,281.52
List Price:  L1,830.82
You save:  L549.30
ден4,084.06
List Price:  ден5,834.63
You save:  ден1,750.56
MOP$559.24
List Price:  MOP$798.95
You save:  MOP$239.71
N$1,266.15
List Price:  N$1,808.86
You save:  N$542.71
C$2,568.06
List Price:  C$3,668.81
You save:  C$1,100.75
रु9,427.87
List Price:  रु13,468.97
You save:  रु4,041.09
S/262.66
List Price:  S/375.25
You save:  S/112.58
K281.39
List Price:  K402.01
You save:  K120.61
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,903.42
List Price:  ZK2,719.29
You save:  ZK815.86
L330.70
List Price:  L472.45
You save:  L141.75
Kč1,679.39
List Price:  Kč2,399.23
You save:  Kč719.84
Ft27,552.80
List Price:  Ft39,362.83
You save:  Ft11,810.03
SEK kr765.72
List Price:  SEK kr1,093.94
You save:  SEK kr328.21
ARS$70,549.59
List Price:  ARS$100,789.45
You save:  ARS$30,239.85
Bs482.26
List Price:  Bs688.98
You save:  Bs206.71
COP$307,907.55
List Price:  COP$439,886.78
You save:  COP$131,979.23
₡35,643.95
List Price:  ₡50,922.11
You save:  ₡15,278.16
L1,765.02
List Price:  L2,521.57
You save:  L756.54
₲545,488.80
List Price:  ₲779,303.12
You save:  ₲233,814.31
$U2,997.83
List Price:  $U4,282.79
You save:  $U1,284.96
zł286.31
List Price:  zł409.03
You save:  zł122.72
Already have an account? Log In

Transcript

So far we know that what are the things we should not do in MVC dependency injection. Now it's the time to understand how exactly we should do dependency injection in MVC applications, especially with this kind of scenarios of home controllers and all. So let's see that, I'm going to right click on my component folder, and inside this we are going to add a new class. And I'm giving the name of this class custom controller factory.cs. Now, this is something which is my own class custom controller factory, but I've given the name like this because to do dependency injection in MVC, I'm going to use one of the predefined facility of MVC, which is coming from an interface with the name II controller factory. Now if you remember in the previous videos, I have discussed with you that There are 13 containers of IOC which we can use and those predefined containers of IOC s are going to help us to do inversion of control and dependency injection in some of the applications.

Now, in this case, we are going to use one of the predefined thing which is known as I can do a factory interface which helps me to create my kind of instantiation with the dependency injection of any custom controller. So, let's add a namespace using system dot web dot MVC, which is a main namespace of MVC we already discussed this thing and now using this I can add one interface. So, that is I controller factory you can see is there which is pretty fine. Now like all the interfaces when we have interface we are going to implement this interface. And if I implement this interface, you can see I have three predefined methods which are coming into this. So the there is a method with the name create controller, which actually helps me to create an issue instantiation of my controller with my kind of parameters and customization, there is a method in the bottom, which is release controller, which is technically going to dispose that particular controller instance from the memory as and when we need that thing.

And there is a method in between which is get controller session behavior, which just helped me to maintain the behavior of that particular controller in a particular session. So, this is something which I'm going to utilize now. So I'm going to add a code inside this and I want that this custom controller factory class can help me to create my kind of instantiation of a logo. And that's why our core goal goal like this equals to new and then we can instantiate with any of the concrete implementations, we have only one right now, which is default logo, so I'm instantiating with this. Now this logo is the instance of Before logging, and then based on this, we need to associate with a home controller. Sampling a variable controller equals to new.

And then I'm going to create an instance of a home controller. The moment I do this thing, this is not taking home control right now because I do not have a nice face of controllers. So I'm detecting that now. And inside this, I'm going to pass my logo so that this one is going to associate with my parameterised constructor. So I hope you agree with me this is an instance of home controller and the moment I pass a parameter, this will go to Home controller and this will call my parameter constructor, which is associated with the logo. Now exactly after this we are going to just type one line and that is we are going to return controller.

So this create controller is He's not actually creating a new controller is creating my own controller with my kind of parameter. And that customized controller we are going to return from this particular method. Now once we are done with this inside the second method, which is giving me the session behavior, we are just going to return whatever session behavior we have dot default so we don't do any fancy thing inside that. And in fact the release controller we have to dispose the controller when we need so I am just calling it disposable. I'm giving a name of that. disposable equals to controller as disposable and we'll make sure this is going to be a small see Santa Claus controller.

It's just my kind of variable and now once we have this disposable I'm just going to check if this disposable is not equals to No. If we have something in that then we are just going to call disposable dot dispose this this course is a predefined method which will dispose that thing. This core is super simple, I hope you're able to understand this thing if you have ever disposing the object using a disposable. So, we have a simple controller factory interface which is implemented inside the class and we have three methods create controller, get controller session behavior and release controller and we have associated this with our home controller and we can dispose that as and then we need that thing. Now once we are done with this Now the next step is task is how we can associate this custom controller factory with our home controller always.

To do this thing we have to take the help of our global dot ASX. So we know that in MVC application you already Have lobola ASX so I'm going inside this global dot ASX and I'm going to add some lines of code inside this. So we have application start, which is the starting point of application execution exactly outside this application start, I want to create my own method. So I'm creating a method, private word, and I'm giving a name of this method that register my factory. So, this is going to be registering my factory which we have created and inside this method, we are going to write some logic. So I am using my interface I controller factory and giving a name of this like factory equals to new and then we are just going to call our custom controller factory into this.

So we should have the class which is custom controller factory and specifying it here. I'm going to pass on this because it's not detecting we just need to add a reference of this. So, we are just adding these components inside this and now this factory is the instance of custom controller factory same way inside the register my factory I can create one more controller factory kind of variable which can associate with nearly another kind of custom controller factory The only one so we are just utilizing this one inside this and now I'm calling controller builder, which is another predefined class helping me to deal with this my custom controller factory. So controller builder dot current dot status, the method set controller factory in which we are specifying that we are going to set this factory as a current factory. The moment we are done with this, you can see that this method is just helping me to create my own factory and registering my factory into that.

Now all I need to do is I need to call this method on the top of all of this So it's something like this when the application has to start first is going to register my own factory with my kind of things and with that is going to associate my controller factory instance. Once that is done, then all the predefined things will happen and in this, your predefined home controller or the other controllers are going to work. In short, without touching the high level modules, we have just created our own low level modules, which are based on this abstraction. If Microsoft did not provide this kind of icon to the factory, that's also possible that we can create our own containers and we can create this thing manually. But because Microsoft has provided this thing specially for MVC, they're using this controller factory and using this we are just configuring this thing in such a way that my home controller is now connected with my ilogger without disturbing the predefined things without disturbing the high level modules.

Our high level module and low level module both are connected with abstraction. So by following dependence envision principle with inversion of control with dependency injection in MVC. This is what we have done inside this. Let me click on Save all. And. And if you want to check this thing, just, you have to do one thing.

I'm going to put a breakpoint one in my default constructor, and another one in my parameter constructor. And I want to see that actually, this is hitting directly to my parameter constructor or not. Because if everything is correct, this will not go to my default constructor at all. This will always go to my parameter constructor when I run this application. So let me change my browser to maybe web browser, which is Microsoft H. And inside that, we can choose the browser. So I'm just going to select maybe H or chrome or whatever is there.

And then I'm going to click on Save All I'm going to run this. The moment we run this thing is going to start debugging because I'm running with debugging. And if it is setting a breakpoint, I want to see which one breakpoint This is hitting. And you can see that when I'm running this thing is now hitting my breakpoint. And this is directly hitting my parameter constructor. It's not at all going to my default constructor.

And that's what we have achieved with the help of this dependency injection inside MVC application. I hope you're getting this application code. And same way if you want to create multiple custom controller factories, or you want to create some more concrete implementations of ilogger you can use the same way into your multiple controllers. I hope you understood this video. 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.