Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ViewModelServiceProvider.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 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace SiliconStudio.Presentation.ViewModel
8 {
9  /// <summary>
10  /// A service provider class for view model objects.
11  /// </summary>
13  {
14  private readonly List<object> services = new List<object>();
15  private bool DisallowRegister { get; set; }
16 
17  public static IViewModelServiceProvider NullServiceProvider = new ViewModelServiceProvider { DisallowRegister = true };
18 
19  /// <summary>
20  /// Register a new service in this <see cref="ViewModelServiceProvider"/>.
21  /// </summary>
22  /// <param name="service">The service to register.</param>
23  /// <exception cref="InvalidOperationException">A service of the same type has already been registered.</exception>
24  public void RegisterService(object service)
25  {
26  if (DisallowRegister) throw new InvalidOperationException("Unable to register a service in this service provider.");
27  if (service == null) throw new ArgumentNullException("service");
28  if (services.Any(x => x.GetType() == service.GetType()))
29  throw new InvalidOperationException("A service of the same type has already been registered.");
30 
31  services.Add(service);
32  }
33 
34  /// <inheritdoc/>
36  {
37  var provider = new ViewModelServiceProvider();
38  provider.services.AddRange(services);
39  return provider;
40  }
41 
42  /// <inheritdoc/>
43  public object TryGet(Type serviceType)
44  {
45  if (serviceType == null) throw new ArgumentNullException("serviceType");
46  object serviceFound = null;
47 
48  foreach (var service in services.Where(serviceType.IsInstanceOfType))
49  {
50  if (serviceFound != null)
51  throw new InvalidOperationException("Multiple services match the given type.");
52 
53  serviceFound = service;
54  }
55  return serviceFound;
56  }
57 
58  /// <inheritdoc/>
59  public T TryGet<T>() where T : class
60  {
61  return (T)TryGet(typeof(T));
62  }
63 
64  /// <inheritdoc/>
65  public object Get(Type serviceType)
66  {
67  var result = TryGet(serviceType);
68  if (result == null) throw new InvalidOperationException("No service matches the given type.");
69  return result;
70  }
71 
72  /// <inheritdoc/>
73  public T Get<T>() where T : class
74  {
75  var result = (T)TryGet(typeof(T));
76  if (result == null) throw new InvalidOperationException("No service matches the given type.");
77  return result;
78  }
79  }
80 }
object TryGet(Type serviceType)
Gets a service of the given type, if available. The type of service to retrieve.An instance of the se...
A service provider class for view model objects.
object Get(Type serviceType)
Gets a service of the given type. The type of service to retrieve.An instance of the service that mat...
IViewModelServiceProvider Clone()
Clones this IViewModelServiceProvider using shallow copy.
void RegisterService(object service)
Register a new service in this ViewModelServiceProvider.