-
Notifications
You must be signed in to change notification settings - Fork 31
/
ServiceContainer.cs
153 lines (137 loc) · 4.18 KB
/
ServiceContainer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Copyright 2014 Xamarin Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.using System;
using System;
using System.Collections.Generic;
namespace MyExpenses.Portable.Helpers
{
/// <summary>
/// A simple service container implementation, singleton only
/// </summary>
public static class ServiceContainer
{
static readonly Dictionary<Type, Lazy<object>> Services = new Dictionary<Type, Lazy<object>>();
static readonly Stack<Dictionary<Type, object>> ScopedServices = new Stack<Dictionary<Type, object>>();
/// <summary>
/// Register the specified service with an instance
/// </summary>
public static void Register<T>(T service)
{
Services[typeof(T)] = new Lazy<object>(() => service);
}
/// <summary>
/// Register the specified service for a class with a default constructor
/// </summary>
public static void Register<T>() where T : new()
{
Services[typeof(T)] = new Lazy<object>(() => new T());
}
/// <summary>
/// Register the specified service with a callback to be invoked when requested
/// </summary>
public static void Register<T>(Func<T> function)
{
Services[typeof(T)] = new Lazy<object>(() => function());
}
/// <summary>
/// Register the specified service with an instance
/// </summary>
public static void Register(Type type, object service)
{
Services[type] = new Lazy<object>(() => service);
}
/// <summary>
/// Register the specified service with a callback to be invoked when requested
/// </summary>
public static void Register(Type type, Func<object> function)
{
Services[type] = new Lazy<object>(function);
}
/// <summary>
/// Register the specified service with an instance that is scoped
/// </summary>
public static void RegisterScoped<T>(T service)
{
Dictionary<Type, object> services;
if (ScopedServices.Count == 0)
{
services = new Dictionary<Type, object>();
ScopedServices.Push(services);
}
else
{
services = ScopedServices.Peek();
}
services[typeof(T)] = service;
}
/// <summary>
/// Resolves the type, throwing an exception if not found
/// </summary>
public static T Resolve<T>()
{
return (T)Resolve(typeof(T));
}
/// <summary>
/// Resolves the type, throwing an exception if not found
/// </summary>
public static object Resolve(Type type)
{
//Scoped services
if (ScopedServices.Count > 0)
{
var services = ScopedServices.Peek();
object service;
if (services.TryGetValue(type, out service))
{
return service;
}
}
//Non-scoped services
{
Lazy<object> service;
if (Services.TryGetValue(type, out service))
{
return service.Value;
}
else
{
throw new KeyNotFoundException(string.Format("Service not found for type '{0}'", type));
}
}
}
/// <summary>
/// Adds a "scope" which is a way to register a service on a stack to be popped off at a later time
/// </summary>
public static void AddScope()
{
ScopedServices.Push(new Dictionary<Type, object>());
}
/// <summary>
/// Removes the current "scope" which pops off some local services
/// </summary>
public static void RemoveScope()
{
if (ScopedServices.Count > 0)
ScopedServices.Pop();
}
/// <summary>
/// Mainly for testing, clears the entire container
/// </summary>
public static void Clear()
{
Services.Clear();
ScopedServices.Clear();
}
}
}