Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ServiceRegistryExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 using System;
5 
6 namespace SiliconStudio.Core
7 {
8  public static class ServiceRegistryExtensions
9  {
10  /// <summary>
11  /// Gets a service instance from a specified interface contract.
12  /// </summary>
13  /// <typeparam name="T">Type of the interface contract of the service</typeparam>
14  /// <param name="registry">The registry.</param>
15  /// <returns>An instance of the requested service registered to this registry.</returns>
16  public static T GetServiceAs<T>(this IServiceRegistry registry)
17  {
18  return (T)registry.GetService(typeof(T));
19  }
20 
21  /// <summary>
22  /// Gets a service instance from a specified interface contract.
23  /// </summary>
24  /// <typeparam name="T">Type of the interface contract of the service</typeparam>
25  /// <param name="registry">The registry.</param>
26  /// <exception cref="ServiceNotFoundException">If the service was not found</exception>
27  /// <returns>An instance of the requested service registered to this registry.</returns>
28  public static T GetSafeServiceAs<T>(this IServiceRegistry registry)
29  {
30  var serviceFound = (T)registry.GetService(typeof(T));
31  if (Equals(serviceFound, default(T)))
32  {
33  throw new ServiceNotFoundException(typeof(T));
34  }
35  return serviceFound;
36  }
37 
38  /// <summary>
39  /// Gets a service instance from a specified interface contract.
40  /// </summary>
41  /// <typeparam name="T">Type of the interface contract of the service</typeparam>
42  /// <param name="registry">The registry.</param>
43  /// <param name="serviceReady">The service ready.</param>
44  /// <returns>An instance of the requested service registered to this registry.</returns>
45  /// <exception cref="ServiceNotFoundException">If the service was not found</exception>
46  public static void GetServiceLate<T>(this IServiceRegistry registry, Action<T> serviceReady)
47  {
48  var instance = GetServiceAs<T>(registry);
49  if (Equals(instance, null))
50  {
51  var deferred = new ServiceDeferredRegister<T>(registry, serviceReady);
52  deferred.Register();
53  }
54  else
55  {
56  serviceReady(instance);
57  }
58  }
59 
60  private class ServiceDeferredRegister<T>
61  {
62  private readonly IServiceRegistry services;
63  private readonly Action<T> serviceReady;
64 
65  public ServiceDeferredRegister(IServiceRegistry registry, Action<T> serviceReady)
66  {
67  services = registry;
68  this.serviceReady = serviceReady;
69  }
70 
71  public void Register()
72  {
73  services.ServiceAdded += Services_ServiceAdded;
74  }
75 
76  void Services_ServiceAdded(object sender, ServiceEventArgs args)
77  {
78  if (args.ServiceType == typeof(T))
79  {
80  serviceReady((T)args.Instance);
81  services.ServiceAdded -= Services_ServiceAdded;
82  }
83  }
84  }
85  }
86 }
A service registry is a IServiceProvider that provides methods to register and unregister services...