You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I first read about Dependency Injection, I of course read the
15
-
[famous article](http://www.martinfowler.com/articles/injection.html) by
16
-
Martin Fowler... and to you I can say that I didn't get it. At all.
17
-
Please don't tell anyone ;)
14
+
When I first read about Dependency Injection, I of course read the [famous article](http://www.martinfowler.com/articles/injection.html) by Martin Fowler... and to you I can say that I didn't get it. At all. Please don't tell anyone ;)
18
15
19
-
But after the presentation at [Avega](http://www.avega.se) by [Niclas
20
-
Nilsson](http://www.niclasnilsson.se/) I finally got it. And the exemple
21
-
below is a small rip of the DI-part of the presentation but it works as
22
-
a reminder for me anyway.
16
+
But after the presentation at [Avega](http://www.avega.se) by [Nicias Nilsson](http://www.niclasnilsson.se/) I finally got it. And the example below is a small rip of the DI-part of the presentation but it works as a reminder for me anyway.
23
17
24
18
Here we go:
25
-
Lets say that we have a class called SalesController that managers the
26
-
business logic for sales. In order to work it has to get some product
27
-
information which is stored in a reposotory calles ItemInventory.
19
+
20
+
Lets say that we have a class called SalesController that managers the business logic for sales. In order to work it has to get some product information which is stored in a repository called ItemInventory.
28
21
29
22
Below is a example implementation of SalesController
30
-
`public class SalesController{ ItemInventory item; public SalesController() { ... } ...}`
31
-
This means that the SalesController is dependent to ItemInventory. That
32
-
is not that good since:
33
23
34
-
- if you want to exchange ItemInventory for another implementation it
35
-
will have major impact on SalesController
36
-
- the SalesController is quite hard to test, which in turn has to do
37
-
with it being hard to isolate, or simpler you can't mock-up
38
-
ItemInventory in a easy way
24
+
```c#
25
+
publicclassSalesController {
26
+
ItemInventoryitem;
27
+
publicSalesController() {
28
+
//...
29
+
}
30
+
//...
31
+
}
32
+
```
33
+
34
+
This means that the SalesController is dependent to ItemInventory. Thatis not that good since:
35
+
36
+
- if you want to exchange ItemInventory for another implementation it will have major impact on SalesController
37
+
- the SalesController is quite hard to test, which in turn has to do with it being hard to isolate, or simpler you can't mock-up ItemInventory in a easy way
39
38
40
39
Lets use interfaces instead:
41
-
`public class SalesController{ IItemInventory item; public SalesController() { ... } ...}`
42
-
We have now won that we easy can exchange ItemInventory against another
43
-
class that implements IItemInventory. But is still is(can be) hard to
44
-
test since you have to test ItemInventory as part of the test of
45
-
SalesController.
40
+
41
+
```c#
42
+
publicclassSalesController {
43
+
IItemInventoryitem;
44
+
publicSalesController() {
45
+
//...
46
+
}
47
+
//...
48
+
}
49
+
```
50
+
51
+
We have now won that we easy can exchange ItemInventory against another class that implements IItemInventory. But is still is(can be) hard to test since you have to test ItemInventory as part of the test of SalesController.
46
52
47
53
Dependency Injection to the fore!
48
-
`public class SalesController{ IItemInventory item; public SalesController(IItemInventory item) { this.item = item; } ...}`
49
-
In this way we can send in the dependency of the class (ItemInventory in
50
-
this case) to SalesController, or if you rather want we can **inject the
51
-
dependency** of SalesController.
52
54
53
-
Now we can, in a testcase, send in a mock-up of ItemInventory to easy
In this way we can send in the dependency of the class (ItemInventory in this case) to SalesController, or if you rather want we can **inject the dependency** of SalesController.
64
+
65
+
Now we can, in a testcase, send in a mock-up of ItemInventory to easy test the methods of SalesController.
55
66
56
-
Very short, to end this mega-post, Spring.NET gives you the possibility
57
-
to do these injections via configuration-files, which further simplifies
58
-
the testing process (i.e. a different config-file for the test-code).
67
+
Very short, to end this mega-post, Spring.NET gives you the possibility to do these injections via configuration-files, which further simplifies the testing process (i.e. a different config-file for the test-code).
0 commit comments