Skip to content

Commit dae15b4

Browse files
author
Anton Wieslander
committed
dependency injection
1 parent cf2cd59 commit dae15b4

File tree

3 files changed

+311
-0
lines changed

3 files changed

+311
-0
lines changed

Dependency Injection/activator.linq

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
// var service = new HelloService();
6+
// var consumer = new ServiceConsumer(service);
7+
var a = typeof(HelloService).Dump();
8+
var service = (HelloService) Activator.CreateInstance(typeof(HelloService));
9+
var consumer = (ServiceConsumer) Activator.CreateInstance(typeof(ServiceConsumer), service);
10+
service.Print();
11+
consumer.Print();
12+
}
13+
14+
public class ServiceConsumer
15+
{
16+
HelloService _hello;
17+
public ServiceConsumer(HelloService hello)
18+
{
19+
_hello= hello;
20+
}
21+
22+
public void Print()
23+
{
24+
_hello.Print();
25+
}
26+
}
27+
28+
public class HelloService
29+
{
30+
public void Print(){
31+
"Hello World".Dump();
32+
}
33+
}

Dependency Injection/lifetimes.linq

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var container = new DependencyContainer();
6+
container.AddTransient<ServiceConsumer>();
7+
container.AddTransient<HelloService>();
8+
container.AddSingleton<MessageService>();
9+
10+
var resolver = new DependencyResolver(container);
11+
12+
var service1 = resolver.GetService<ServiceConsumer>();
13+
14+
service1.Print();
15+
var service2 = resolver.GetService<ServiceConsumer>();
16+
service2.Print();
17+
var service3 = resolver.GetService<ServiceConsumer>();
18+
service3.Print();
19+
}
20+
21+
public class DependencyResolver
22+
{
23+
DependencyContainer _container;
24+
public DependencyResolver(DependencyContainer container)
25+
{
26+
_container = container;
27+
}
28+
29+
public T GetService<T>()
30+
{
31+
return (T)GetService(typeof(T));
32+
}
33+
34+
public object GetService(Type type)
35+
{
36+
var dependency = _container.GetDependency(type);
37+
var constructor = dependency.Type.GetConstructors().Single();
38+
var parameters = constructor.GetParameters().ToArray();
39+
40+
if (parameters.Length > 0)
41+
{
42+
var parameterImplementations = new object[parameters.Length];
43+
44+
for (int i = 0; i < parameters.Length; i++)
45+
{
46+
parameterImplementations[i] = GetService(parameters[i].ParameterType);
47+
}
48+
49+
return CreateImplementaiton(dependency, t => Activator.CreateInstance(t, parameterImplementations));
50+
}
51+
52+
return CreateImplementaiton(dependency, t => Activator.CreateInstance(t));
53+
}
54+
55+
public object CreateImplementaiton(Dependency dependency, Func<Type, object> factory)
56+
{
57+
if (dependency.Implemented)
58+
{
59+
return dependency.Implementation;
60+
}
61+
62+
var implementation = factory(dependency.Type);
63+
64+
if (dependency.Lifetime == DependencyLifetime.Singleton)
65+
{
66+
dependency.AddImplementation(implementation);
67+
}
68+
69+
return implementation;
70+
}
71+
}
72+
73+
74+
public class DependencyContainer
75+
{
76+
List<Dependency> _dependencies;
77+
78+
public DependencyContainer()
79+
{
80+
_dependencies = new List<Dependency>();
81+
}
82+
83+
public void AddSingleton<T>()
84+
{
85+
_dependencies.Add(new Dependency(typeof(T), DependencyLifetime.Singleton));
86+
}
87+
88+
public void AddTransient<T>()
89+
{
90+
_dependencies.Add(new Dependency(typeof(T), DependencyLifetime.Transient));
91+
}
92+
93+
public Dependency GetDependency(Type type)
94+
{
95+
return _dependencies.First(x => x.Type.Name == type.Name);
96+
}
97+
}
98+
99+
public class Dependency
100+
{
101+
public Dependency(Type t, DependencyLifetime l)
102+
{
103+
Type = t;
104+
Lifetime = l;
105+
}
106+
public Type Type { get; set; }
107+
public DependencyLifetime Lifetime { get; set; }
108+
public object Implementation { get; set; }
109+
public bool Implemented { get; set; }
110+
111+
public void AddImplementation(object i){
112+
Implementation = i;
113+
Implemented = true;
114+
}
115+
}
116+
117+
public enum DependencyLifetime
118+
{
119+
Singleton = 0,
120+
Transient = 1,
121+
}
122+
123+
public class ServiceConsumer
124+
{
125+
HelloService _hello;
126+
public ServiceConsumer(HelloService hello)
127+
{
128+
_hello = hello;
129+
}
130+
131+
public void Print()
132+
{
133+
_hello.Print();
134+
}
135+
}
136+
137+
public class HelloService
138+
{
139+
MessageService _message;
140+
int _random;
141+
public HelloService(MessageService message)
142+
{
143+
_message = message;
144+
_random = new Random().Next();
145+
}
146+
147+
public void Print()
148+
{
149+
$"Hello #{_random} World {_message.Message()}".Dump();
150+
}
151+
}
152+
153+
public class MessageService
154+
{
155+
int _random;
156+
public MessageService()
157+
{
158+
_random = new Random().Next();
159+
}
160+
161+
public string Message()
162+
{
163+
return $"Yo #{_random}";
164+
}
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
//typeof(ServiceConsumer)
6+
// .GetConstructors()
7+
// .Select(x => x.GetParameters()).Dump();
8+
9+
var container = new DependencyContainer();
10+
container.AddDependency(typeof(HelloService));
11+
container.AddDependency<ServiceConsumer>();
12+
container.AddDependency<MessageService>();
13+
14+
var resolver = new DependencyResolver(container);
15+
16+
var service = resolver.GetService<ServiceConsumer>();
17+
18+
service.Print();
19+
}
20+
21+
public class DependencyResolver
22+
{
23+
DependencyContainer _container;
24+
public DependencyResolver(DependencyContainer container)
25+
{
26+
_container = container;
27+
}
28+
29+
public T GetService<T>()
30+
{
31+
return (T) GetService(typeof(T));
32+
}
33+
34+
public object GetService(Type type)
35+
{
36+
var dependency = _container.GetDependency(type);
37+
var constructor = dependency.GetConstructors().Single();
38+
var parameters = constructor.GetParameters().ToArray();
39+
40+
if (parameters.Length > 0)
41+
{
42+
var parameterImplementations = new object[parameters.Length];
43+
44+
for (int i = 0; i < parameters.Length; i++)
45+
{
46+
parameterImplementations[i] = GetService(parameters[i].ParameterType);
47+
}
48+
49+
return Activator.CreateInstance(dependency, parameterImplementations);
50+
}
51+
52+
53+
return Activator.CreateInstance(dependency);
54+
}
55+
}
56+
57+
58+
public class DependencyContainer
59+
{
60+
List<Type> _dependencies;
61+
62+
public void AddDependency(Type type)
63+
{
64+
_dependencies = new List<Type>();
65+
_dependencies.Add(type);
66+
}
67+
68+
public void AddDependency<T>()
69+
{
70+
_dependencies.Add(typeof(T));
71+
}
72+
73+
public Type GetDependency(Type type)
74+
{
75+
return _dependencies.First(x => x.Name == type.Name);
76+
}
77+
}
78+
79+
public class ServiceConsumer
80+
{
81+
HelloService _hello;
82+
public ServiceConsumer(HelloService hello)
83+
{
84+
_hello = hello;
85+
}
86+
87+
public void Print()
88+
{
89+
_hello.Print();
90+
}
91+
}
92+
93+
public class HelloService
94+
{
95+
MessageService _message;
96+
public HelloService(MessageService message)
97+
{
98+
_message = message;
99+
}
100+
101+
public void Print()
102+
{
103+
$"Hello World {_message.Message()}".Dump();
104+
}
105+
}
106+
107+
public class MessageService
108+
{
109+
public string Message()
110+
{
111+
return "Yo";
112+
}
113+
}

0 commit comments

Comments
 (0)