-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceCollection.cs
135 lines (114 loc) · 4.1 KB
/
ServiceCollection.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
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using LLM.Serialization.Base;
namespace LLM.Serialization
{
public class ServiceCollection
{
protected Dictionary<Type, IService> Services;
public bool IsInitialized { get; protected set; }
public ServiceCollection()
{
Services = new Dictionary<Type, IService>();
}
public virtual void Initialize()
{
foreach (var item in Services.Values)
{
if(item.IsInitialized) continue;
item.Initialize().Forget();
}
IsInitialized = true;
}
public virtual async UniTask InitializeAsync()
{
foreach (var item in Services.Values)
{
if(item.IsInitialized) continue;
await item.Initialize();
}
IsInitialized = true;
}
public virtual bool Has(Type service) => Services.ContainsKey(service);
public virtual bool Has(IService service) => Services.ContainsKey(service.GetType());
public bool IsServiceInitialized<TService>() where TService : IService => Has(typeof(TService)) ? Get<TService>().IsInitialized : false;
public virtual ServiceCollection Add(IService service)
{
IsInitialized = false; //just in case so developer will use Build()
InjectNeededServices(service);
Services.Add(service.GetType(), service);
return this;
}
public virtual ServiceCollection Remove(IService service)
{
if(!Has(service)) return this;
service.OnServiceDispose();
Services.Remove(service.GetType());
return this;
}
public virtual ServiceCollection Remove<TService>()
where TService : IService
{
if(!Has(typeof(TService))) return this;
Services[typeof(TService)].OnServiceDispose();
Services.Remove(typeof(TService));
return this;
}
public T Get<T>()
where T : IService
{
if(!Services.TryGetValue(typeof(T), out var service)) throw new FormatException($"[ServiceCollection] <color=yellow>{typeof(T)}</color> <color=red>NOT</color> registered.");
return (T)service;
}
public void Get<T1>(out T1 param1)
where T1 : IService
{
param1 = Get<T1>();
}
public void Get<T1, T2>(out T1 param1, out T2 param2)
where T1 : IService
where T2 : IService
{
Get<T1>(out param1);
param2 = 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
{
Get<T1, T2>(out param1, out param2);
param3 = 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
{
Get<T1, T2, T3>(out param1, out param2, out param3);
param4 = Get<T4>();
}
private void InjectNeededServices(IService service)
{
var injectionQueue = service.InjectionQueue;
if(injectionQueue.Count <= 0) return;
List<IService> servicesReadyForInjection = new List<IService>();
foreach (var serviceToInject in injectionQueue)
{
if(!Services.ContainsKey(serviceToInject)) continue;
servicesReadyForInjection.Add(Services[serviceToInject]);
UnityEngine.Debug.LogError("Injecting: "+Services[serviceToInject]);
}
service.Inject(servicesReadyForInjection.ToArray());
}
public void Dispose()
{
foreach (var service in Services.Values)
{
service?.OnServiceDispose();
}
}
}
}