-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionist.cs
108 lines (93 loc) · 3.82 KB
/
Collectionist.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
using Cysharp.Threading.Tasks;
using LLM.Serialization.Base;
namespace LLM.Serialization
{
public class Collectionist : IService
{
public ServiceCollection StaticServices;
public ServiceCollection DynamicServices;
public bool IsInitialized => StaticServices.IsInitialized;
public Collectionist(ServiceCollection staticGlobalServices)
{
StaticServices = staticGlobalServices;
DynamicServices = new ServiceCollection();
}
public void Inject(params IService[] servicesToInject) { }
/// <summary>
/// Initializes static services (global)
/// </summary>
public async UniTask Initialize() => await StaticServices.InitializeAsync();
/// <summary>
/// Build new local services and initialize them
/// <br> if you used .Add(IService).Add(IService) pattern just put .Build() in the end</br>
/// </summary>
/// <returns></returns>
public async UniTask Build() => await DynamicServices.InitializeAsync();
/// <summary>
/// Adds local service
/// </summary>
public Collectionist Add(IService service)
{
DynamicServices.Add(service);
return this;
}
/// <summary>
/// Removes local service
/// </summary>
public Collectionist Remove(IService service)
{
DynamicServices.Remove(service);
return this;
}
/// <summary>
/// Removes local service
/// </summary>
public Collectionist Remove<TService>() where TService : IService
{
DynamicServices.Remove<TService>();
return this;
}
public TService Get<TService>()
where TService : IService
{
return StaticServices.Has(typeof(TService)) ? StaticServices.Get<TService>() : DynamicServices.Get<TService>();
}
public void Get<T1>(out T1 param1)
where T1 : IService
{
param1 = StaticServices.Has(typeof(T1)) ? StaticServices.Get<T1>() : DynamicServices.Get<T1>();
}
public void Get<T1, T2>(out T1 param1, out T2 param2)
where T1 : IService
where T2 : IService
{
param1 = StaticServices.Has(typeof(T1)) ? StaticServices.Get<T1>() : DynamicServices.Get<T1>();
param2 = StaticServices.Has(typeof(T2)) ? StaticServices.Get<T2>() : DynamicServices.Get<T2>();
}
public void Get<T1, T2, T3>(out T1 param1, out T2 param2, out T3 param3)
where T1 : IService
where T2 : IService
where T3 : IService
{
param1 = StaticServices.Has(typeof(T1)) ? StaticServices.Get<T1>() : DynamicServices.Get<T1>();
param2 = StaticServices.Has(typeof(T2)) ? StaticServices.Get<T2>() : DynamicServices.Get<T2>();
param3 = StaticServices.Has(typeof(T3)) ? StaticServices.Get<T3>() : DynamicServices.Get<T3>();
}
public void Get<T1, T2, T3, T4>(out T1 param1, out T2 param2, out T3 param3, out T4 param4)
where T1 : IService
where T2 : IService
where T3 : IService
where T4 : IService
{
param1 = StaticServices.Has(typeof(T1)) ? StaticServices.Get<T1>() : DynamicServices.Get<T1>();
param2 = StaticServices.Has(typeof(T2)) ? StaticServices.Get<T2>() : DynamicServices.Get<T2>();
param3 = StaticServices.Has(typeof(T3)) ? StaticServices.Get<T3>() : DynamicServices.Get<T3>();
param4 = StaticServices.Has(typeof(T4)) ? StaticServices.Get<T4>() : DynamicServices.Get<T4>();
}
public void OnServiceDispose()
{
DynamicServices?.Dispose();
StaticServices?.Dispose();
}
}
}