-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyInjection.cs
94 lines (81 loc) · 2.39 KB
/
DependencyInjection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/*
* Dependency Injection
* Dependency Injection (DI) is a software design pattern.
* It allows us to develop loosely-coupled code.
* The intent of Dependency Injection is to make code maintainable.
* Dependency Injection helps to reduce the tight coupling among software components.
* DI reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically.
*
* 1. Constructor Injection
* 2. Property/Setter Injection
* 3. Method Injection
*
*/
interface IPerson
{
void PersonDetails();
}
class Employee : IPerson
{
public void PersonDetails()
{
Console.WriteLine("Employee Details");
}
}
class Customer : IPerson
{
public void PersonDetails()
{
Console.WriteLine("Customer Details");
}
}
class DependencyInjection
{
//1. Constructor Injection
//IPerson p1;
//public DependencyInjection(IPerson p1)
//{
// this.p1 = p1;
//}
//2. Property/Setter Injection
//public IPerson p1 { get; set; }
//public void print()
//{
// p1.PersonDetails();
//}
//3. Method Injection
IPerson p1;
public void print(IPerson person)
{
this.p1 = person;
p1.PersonDetails();
}
}
class DI_Check
{
static void Main()
{
//1. Constructor Injection
//DependencyInjection DI = new DependencyInjection(new Customer());
//OR
//DependencyInjection DI = new DependencyInjection(new Employee());
//DI.print();
//2. Property/Setter Injection
//DependencyInjection DI = new DependencyInjection();
//DI.p1 = new Employee();
//or
//DI.p1 = new Customer();
//3. Method Injection
DependencyInjection DI = new DependencyInjection();
DI.print(new Employee());
DI.print(new Customer());
Console.ReadKey();
}
}
}