Skip to content

Commit 76c1bb7

Browse files
committed
first commit
0 parents  commit 76c1bb7

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.vscode
2+
/bin
3+
/obj

CSharpLibraries.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
namespace CSharpLibraries.DependencyInjector
2+
{
3+
public class DependencyInjector
4+
{
5+
private HashSet<Type> underConstruction = new();
6+
private Dictionary<Type, Type> _typesMap = new();
7+
private Dictionary<Type, object> _registeredObjects = new();
8+
9+
public void AddSingeleton<T>()
10+
where T : class
11+
{
12+
_typesMap[typeof(T)] = typeof(T);
13+
}
14+
15+
public void AddSingeleton<I, T>()
16+
where T : I
17+
where I : class
18+
{
19+
_typesMap[typeof(I)] = typeof(T);
20+
}
21+
22+
public T? get<T>() where T : class
23+
{
24+
return get(typeof(T)) as T;
25+
}
26+
27+
public void build()
28+
{
29+
foreach (var interfaceType in _typesMap.Keys)
30+
{
31+
registerObject(interfaceType);
32+
}
33+
}
34+
35+
private object registerObject(Type interfaceType)
36+
{
37+
if (underConstruction.Contains(interfaceType))
38+
{
39+
throw new InvalidOperationException($"Circular dependency detected in {interfaceType.FullName} constructor");
40+
}
41+
underConstruction.Add(interfaceType);
42+
var obj = createObject(interfaceType);
43+
_registeredObjects[interfaceType] = obj;
44+
underConstruction.Remove(interfaceType);
45+
return obj;
46+
}
47+
48+
private object createObject(Type interfaceType)
49+
{
50+
var type = _typesMap[interfaceType];
51+
List<object> parameters = new();
52+
var constructors = type.GetConstructors();
53+
if (constructors.Length != 1)
54+
{
55+
throw new InvalidOperationException($"Class {type.FullName} should have exactly one constructor");
56+
}
57+
var constructor = constructors.First();
58+
var parametersInfo = constructor.GetParameters();
59+
60+
foreach (var parameterInfo in parametersInfo)
61+
{
62+
var paramType = parameterInfo.ParameterType;
63+
parameters.Add(getParameter(paramType));
64+
}
65+
return constructor.Invoke(parameters.ToArray());
66+
}
67+
68+
private object getParameter(Type parameterType)
69+
{
70+
var obj = get(parameterType);
71+
if (obj is not null)
72+
{
73+
return obj;
74+
}
75+
else
76+
{
77+
return registerObject(parameterType);
78+
}
79+
}
80+
81+
private object? get(Type interfaceType)
82+
{
83+
if (_registeredObjects.TryGetValue(interfaceType, out var obj))
84+
{
85+
return obj;
86+
}
87+
return null;
88+
}
89+
}
90+
}

ExampleClassA.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace CSharpLibraries
2+
{
3+
public class ExampleClassA
4+
{
5+
public int id = 100;
6+
public ExampleClassA()
7+
{
8+
id = 101;
9+
}
10+
11+
public ExampleClassA(int gg)
12+
{
13+
id = 101;
14+
}
15+
}
16+
17+
public class ExampleClassB
18+
{
19+
public int id = 100;
20+
public ExampleClassA obj;
21+
public ExampleClassB(ExampleClassA exampleClass)
22+
{
23+
id = 101;
24+
obj = exampleClass;
25+
}
26+
}
27+
}

Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using CSharpLibraries.DependencyInjector;
2+
using CSharpLibraries;
3+
// See https://aka.ms/new-console-template for more information
4+
5+
var di = new DependencyInjector();
6+
7+
di.AddSingeleton<ExampleClassB>();
8+
di.AddSingeleton<ExampleClassA>();
9+
10+
di.build();
11+
12+
var objB = di.get<ExampleClassB>();
13+
var objA = objB.obj;

0 commit comments

Comments
 (0)