-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceLocator.cs
More file actions
40 lines (32 loc) · 1 KB
/
Copy pathServiceLocator.cs
File metadata and controls
40 lines (32 loc) · 1 KB
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
using System;
using System.Collections.Generic;
using Services;
namespace Infrastructure
{
public class ServiceLocator
{
private readonly Dictionary<Type, IService> _services = new Dictionary<Type, IService>();
public static ServiceLocator Instance { get; private set; }
public ServiceLocator()
{
Instance = this;
}
public void Register<TService>(TService service) where TService : IService
{
_services.Add(typeof(TService), service);
}
public TService Get<TService>() where TService : IService
{
return (TService)_services[typeof(TService)];
}
public void Remove<TService>() where TService : IService
{
var serviceToRemove = _services[typeof(TService)];
if (serviceToRemove is IDisposable disposableService)
{
disposableService.Dispose();
}
_services.Remove(typeof(TService));
}
}
}