This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppServiceProvider.cs
More file actions
58 lines (51 loc) · 2.33 KB
/
AppServiceProvider.cs
File metadata and controls
58 lines (51 loc) · 2.33 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
namespace Cloudsdale {
/// <summary>
/// Implements IServiceProvider for the application. This type is exposed through the App.Services
/// property and can be used for ContentManagers or other types that need access to an IServiceProvider.
/// </summary>
public class AppServiceProvider : IServiceProvider {
// A map of service type to the services themselves
private readonly Dictionary<Type, object> services = new Dictionary<Type, object>();
/// <summary>
/// Adds a new service to the service provider.
/// </summary>
/// <param name="serviceType">The type of service to add.</param>
/// <param name="service">The service object itself.</param>
public void AddService(Type serviceType, object service) {
// Validate the input
if (serviceType == null)
throw new ArgumentNullException("serviceType");
if (service == null)
throw new ArgumentNullException("service");
if (!serviceType.IsInstanceOfType(service))
throw new ArgumentException("service does not match the specified serviceType");
// Add the service to the dictionary
services.Add(serviceType, service);
}
/// <summary>
/// Gets a service from the service provider.
/// </summary>
/// <param name="serviceType">The type of service to retrieve.</param>
/// <returns>The service object registered for the specified type..</returns>
public object GetService(Type serviceType) {
// Validate the input
if (serviceType == null)
throw new ArgumentNullException("serviceType");
// Retrieve the service from the dictionary
return services[serviceType];
}
/// <summary>
/// Removes a service from the service provider.
/// </summary>
/// <param name="serviceType">The type of service to remove.</param>
public void RemoveService(Type serviceType) {
// Validate the input
if (serviceType == null)
throw new ArgumentNullException("serviceType");
// Remove the service from the dictionary
services.Remove(serviceType);
}
}
}