-
Notifications
You must be signed in to change notification settings - Fork 71
/
ServiceLocator.cs
40 lines (37 loc) · 956 Bytes
/
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
using System;
using System.Collections.Generic;
namespace Chapter.ServiceLocator
{
public static class ServiceLocator
{
private static readonly
IDictionary<Type, object> Services =
new Dictionary<Type, Object>();
public static void RegisterService<T>(T service)
{
if (!Services.ContainsKey(typeof(T)))
{
Services[typeof(T)] = service;
}
else
{
throw new
ApplicationException
("Service already registered");
}
}
public static T GetService<T>()
{
try
{
return (T) Services[typeof(T)];
}
catch
{
throw new
ApplicationException
("Requested service not found.");
}
}
}
}