-
Notifications
You must be signed in to change notification settings - Fork 8
/
ServiceLocator.cs
122 lines (99 loc) · 4.15 KB
/
ServiceLocator.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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Martin Bustos @FronkonGames <fronkongames@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of
// the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace FronkonGames.GameWork.Foundation
{
/// <summary> Thread-safe Service Locator implementation. </summary>
public class ServiceLocator : IServiceLocator
{
// Thread-safe dictionary.
private readonly ConcurrentDictionary<Type, IService> services = new();
internal static readonly List<Type> NoDependencies = new();
/// </inheritdoc>
public void Register<T>(T service) where T : IService => Register(service.GetType(), service);
/// </inheritdoc>
public void Register(Type type, IService service)
{
Check.IsNotNull(service);
Check.IsAssignableFrom(type, service);
bool requirements = true;
for (int i = 0; i < service.GetDependencies().Count && requirements == true; ++i)
{
Type dependence = service.GetDependencies()[i];
// It must be registered and correctly initialized.
if (IsRegistered(dependence) == false || Get(dependence).Status != ServiceStatus.Initialized)
{
Log.Warning($"Dependency '{dependence}' on service '{type}' is not fulfilled");
requirements = false;
}
}
if (requirements == true && service.Status != ServiceStatus.NotInitialized)
{
Log.Warning($"Service {type} status incorrect");
requirements = false;
}
if (requirements == true)
{
if (services.TryAdd(type, service) == true)
service.OnRegister();
else
Log.Warning($"Service of type '{type}' is already registered");
}
}
/// </inheritdoc>
public bool Unregister<T>() where T : IService => Unregister(typeof(T));
/// </inheritdoc>
public bool Unregister(Type type)
{
Check.IsNotNull(type);
bool removed = services.TryRemove(type, out var service);
if (service != null && service.Status == ServiceStatus.Initialized)
service.OnUnregister();
return removed;
}
/// </inheritdoc>
public void UnregisterAll()
{
foreach (var service in services)
{
if (service.Value.Status == ServiceStatus.Initialized)
service.Value.OnUnregister();
}
services.Clear();
}
/// </inheritdoc>
public IService Get<T>() where T : IService => (T)Get(typeof(T));
/// </inheritdoc>
public IService Get(Type type)
{
Check.IsNotNull(type);
if (services.TryGetValue(type, out var value) == false)
Log.ExceptionKeyNotFound($"Service of type '{type}' is not registered");
return value;
}
/// </inheritdoc>
public bool IsRegistered<T>() where T : IService => IsRegistered(typeof(T));
/// </inheritdoc>
public bool IsRegistered(Type type)
{
Check.IsNotNull(type);
return services.ContainsKey(type);
}
}
}