-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDependencyContainer.cs
177 lines (149 loc) · 4.55 KB
/
DependencyContainer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegatesLambdasEvents
{
class DependencyContainer
{
public List<Dependency> _dependencies;
public DependencyContainer()
{
_dependencies = new List<Dependency>();
}
//public void AddDependency(Type type)
//{
// _dependencies.Add(type);
//}
//public void AddDependency<T>()
//{
// _dependencies.Add(typeof(T));
//}
public void AddSingleton<T>()
{
_dependencies.Add(new Dependency(typeof(T),DependencyLifetime.Singleton));
}
public void AddTransient<T>()
{
_dependencies.Add(new Dependency(typeof(T), DependencyLifetime.Transient));
}
public Dependency GetDependency(Type t)
{
return _dependencies.First(x => x.Type.Name == t.Name);
}
}
class Dependency
{
public Dependency(Type type, DependencyLifetime lifetime)
{
Type = type;
Lifetime = lifetime;
}
public Type Type { get; set; }
public DependencyLifetime Lifetime { get; set; }
public void AddImplementation(object i)
{
Implementation = i;
Implemented = true;
}
public object Implementation { get; set; }
public bool Implemented { get; set; }
}
class ServiceConsumer
{
public HelloService _service;
public string _name;
public ServiceConsumer(HelloService service)
{
_service = service;
}
public void Print()
{
_service.Print();
}
}
class HelloService
{
MessageService _message;
int _random;
public HelloService(MessageService message)
{
_random = new Random().Next();
_message = message;
}
public int PrintRandom()
{
return _random;
}
public void Print()
{
Console.WriteLine($"Hello world from hello service {PrintRandom()}");
Console.WriteLine($"Hello world from message service: {_message.Message()}");
}
}
class MessageService
{
int _random;
public MessageService()
{
_random = new Random().Next();
}
public string Message()
{
return "Yo #"+_random;
}
}
public enum DependencyLifetime
{
Singleton = 0,
Transient = 1,
}
class DependencyResolver
{
DependencyContainer _container;
public DependencyResolver(DependencyContainer container)
{
_container = container;
}
public T GetService<T>()
{
return (T)GetService(typeof(T));
}
public object GetService(Type type)
{
//Get type of service
var dependency = _container.GetDependency(type);
//Get ctors
var ctors = dependency.Type.GetConstructors().Single();
//Get ctors params
var parameters = ctors.GetParameters().ToArray();
if (parameters.Length > 0)
{
//Store implementation of params
var parametersImplementation = new Object[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
//Line below will returns you ParameterInfo[] type, we want to type of parameter of this array
//parametersImplementation[i] = Activator.CreateInstance(parameters[i].GetType());
parametersImplementation[i] = GetService(parameters[i].ParameterType);
}
return CreateImplementation(dependency, t=>Activator.CreateInstance(t,parametersImplementation));
}
return CreateImplementation(dependency, t => Activator.CreateInstance(t));
}
public object CreateImplementation(Dependency dependency, Func<Type,object> factory)
{
if (dependency.Implemented)
{
return dependency.Implementation;
}
var implementation = factory(dependency.Type);
if (dependency.Lifetime == DependencyLifetime.Singleton)
{
dependency.AddImplementation(implementation);
dependency.Implemented = true;
}
return implementation;
}
}
}